1

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) ...

GhostCat
  • 127,190
  • 21
  • 146
  • 218
Paul
  • 13
  • 4

3 Answers3

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.

GhostCat
  • 127,190
  • 21
  • 146
  • 218
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.

Mumrau
  • 62
  • 7
  • Note that the format TO is askijng for is `(1,1) (2,2) (3,3)`, so your splited strings should ideally be resplited. – Turtle Aug 08 '17 at 08:42
  • Sure but once you have the splitted string, you can remove the whitespaces, the parenthesis, resplit by the comma and then store them into their respective arrays. It might not be the cleanest way to do it but it's probably the easiest without using complicated regexp using look-aheads or look-behinds. For learning java as a side project I assume it makes sense. – Mumrau Aug 08 '17 at 08:44
  • With this solution would myMethod contain something along the lines of String[] coordinates = myLine.split("(,) "); in order to remove the remaining non-numeric characters? pass in the array list, loop through to split each individual and sort them into their respective arrays in that method and pass back to main? – Paul Aug 08 '17 at 08:46
  • @Paul No. Type String split on internet for the documentation, they have some pretty good examples. With Mumrau answer (before edit), you would've got an array of Stings looking like `[0] -> (1,1), [1] ->(2,2)`. So to get your coordinates, you would need to parse it after with something like `String[] coord = coordinates[i].split(","); double c1 = Double.parseDouble(coord[0]); double c2 = Double.parseDouble(coord[1]);` (the parenthesis are ignored). – Turtle Aug 08 '17 at 08:51
  • Thank you that's very helpful! – Paul Aug 08 '17 at 08:53
  • I didn't know the parenthesis were ignored by `Double.parseDouble`, you can remove the `replaceAll' then. – Mumrau Aug 08 '17 at 09:02
  • Is there any way to make the first whitespace split optional? For example if (1,1)(2,2) were input, but also allow for (1,1) (2,2) input? Other than that everything is working great this solved my problem! – Paul Aug 08 '17 at 09:16
  • Check this post out, it will explain the look-ahead and look-behind expressions that are your solution here: https://stackoverflow.com/questions/4416425/how-to-split-string-with-some-separator-but-without-removing-that-separator-in-j (Please mind upvoting the answer, I'm trying to reach the reputation for commenting) – Mumrau Aug 08 '17 at 09:49
  • Haha yeah I feel you. Marking resolved etc are probably locked too – Mumrau Aug 08 '17 at 10:17
  • @Mumrau Nice answer, but you could note that: 1) You don't have to delete spaces, they're trimmed with the parser anyway. If you really want to remove them, you should use the `trim()` function instead. 2) With the parser again, it will look in the string for a double (`aer24` will give `24`), so you don't have to remove the brackets (tho it's more clear). +1 anyway, it solved the problem. – Turtle Aug 08 '17 at 14:44
  • @Paul Even if you can't upvote the answers, you can accept it ;) This way the topic is closed, and the answerer gets some rep. – Turtle Aug 08 '17 at 14:45
0

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.