Ignoring characters using Scanner in Java

妖精的绣舞 提交于 2021-02-10 22:10:27

问题


I want to take a multiple coordinate points, say (35,-21) (55,12)... from standard input and put them into respective arrays.

Let's call them x[] and y[].

x[] would contain {35, 55, ...} and y[] would contain {-21, 12, ...} and so forth.

However, I can't seem to find a way to get around the parenthesis and commas.

In c I was using the following:

for(i = 0; i < SIZE; i++) {
    scanf("%*c%d%*c%d%*c%*c",&x[i],&y[i]);
}

However in Java I can not seem to find a way to get around the non-numeric characters.

I currently have the following in Java, as I am stuck.

double[] x = new double[SIZE];
double[] y = new double[SIZE];
Scanner sc = new Scanner(System.in);

for(int i=0; i < SIZE; i++) {
    x[i] = sc.nextDouble();
}

So the question: How would I ignore characters when reading in doubles from scanner?

A quick edit:

My goal is to keep the strict syntax (12,-55) on user input, and being able to enter multiple rows of coordinate points such as:

(1,1) (2,2) (3,3) ...


回答1:


I would do it in multiple steps for improved readability. First it's the System.in retrieving using a scanner and then you split in order to get each group of coordinates separately and then you can work on them later, for whatever purposes.

Something similar to this:

Scanner sc = new Scanner(System.in);
String myLine = sc.nextLine();

String[] coordinates = myLine.split(" ");
//This assumes you have a whitespace only in between coordinates 

String[] coordArray = new String[2];
double x[] = new double[5];
double y[] = new double[5];
String coord;

for(int i = 0; i < coordinates.length; i++)
{
  coord = coordinates[i];
  // Replacing all non relevant characters
  coord = coord.replaceAll(" ", "");
  coord = coord.replaceAll("\\(", ""); // The \ are meant for escaping parenthesis
  coord = coord.replaceAll("\\)", "");
  // Resplitting to isolate each double (assuming your double is 25.12 and not 25,12 because otherwise it's splitting with the comma)
  coordArray = coord.split(",");
  // Storing into their respective arrays
  x[i] = Double.parseDouble(coordArray[0]);
  y[i] = Double.parseDouble(coordArray[1]);
}

Keep in mind that this is a basic solution assuming the format of the input string is strictly respected.

Note that I actually cannot fully test it but there should remain only some light workarounds.




回答2:


nextDouble() tries to fetch a double number from the input. It is simply not meant to parse the input stream and figure by itself how to interpret that string to somehow extract a number.

In that sense: a scanner alone simply doesn't work here. You could look into using a tokenizer - or you use scanner.next() to return the full string; to then do either manual splitting/parsing, or you turn to regular expressions to do that.




回答3:


Mentioned that the user input is strictly restricted to (12,-55) or (1,1) (2,2) (3,3) ... formats the below code works fine

    Double[] x = new Double[5];
    Double[] y = new Double[5];

    System.out.println("Enter Input");
    Scanner scanner = new Scanner(System.in);
    String input = scanner.nextLine();
    input = input.trim();

    int index = 0;

    while(input != null && input != "" && input.indexOf('(') != -1) {
        input = input.trim();
        int i = input.indexOf('(');         
        int j = input.indexOf(',');
        int k = input.indexOf(')');
        x[index] = Double.valueOf(input.substring(i+1, j));
        y[index] = Double.valueOf(input.substring(j+1, k)); 
        System.out.println(x[index] + " " + y[index]);
        input = input.substring(k+1);           
        index++;            
    }

Here I have got the user input in string format and then trim method is called on it to remove the leading and tailing white spaces.

In the while loop the substring between '(' and ',' is taken into x[] and the substring between ',' and ')' is taken into y[].

In the loop the index is incremented and the input string is modified to the substring after the first occurrence of ')' and till the end of the string.

The loop is repeated till there is no occurrence of ')' or the input is null.



来源:https://stackoverflow.com/questions/45563067/ignoring-characters-using-scanner-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!