• 04-15-2011, 07:28 AM
edprog
Hello,

How can I read from the points x and y? but first providing the number of points so for example

3
3 4
2 1
2 3

where 3 is the number of points and the left column is the x's and right the y's I can do it using the Scanner x= new Scanner(System.in) but i can only read this way

3
X:3
Y:4
X:2
Y:1
X:2
Y:3

How can i get make it so that it can be read as mentioned at the beggining, I ppareaciate your help

Regards
• 04-15-2011, 07:33 AM
Junky
Make a single read to get how many x y pairs there are. Then use a loop to read the rest. you can read the entire line and then split and parse the values. Or for each loop iteration read 2 values.
• 04-15-2011, 07:48 AM
edprog
I am sorry i am kind of a newb, would you be able to show me maybe like a pseudo code...?, because everytime when i use Scanner, it prompts on the next line... and i would like to do it something like

2
1 2
2 5

I am sorry
• 04-15-2011, 09:17 AM
sunde887
Code:

```Ask for number of points to read Loop   Read x   Read y End loop```
I don't know what you are wanting to do with the data so I can't offer much more advice. A for loop is always a good choice for looping x times.
• 04-16-2011, 04:11 AM
edprog
Hello,

I do not have a problem reading x and y, i have a problem not being able to read it in the following format

3 4 (where 3 is x and 4 is y)
2 1 (where 2 is x and 1 is y)

3
4
2
1

Any ideas?
• 04-16-2011, 04:15 AM
Junky
The pseudocode provided by sunde is what you need. You just have to fine tune it.
• 04-16-2011, 05:28 AM
edprog
I have this but it wont do it :(

it reads in a new line

for(int i =0;i < pointsnum;i++)
{

System.out.print("X"+i+":");
inputX = scan.nextInt();

System.out.print("Y"+i+":");
inputY = scan.nextInt();

}
• 04-16-2011, 06:08 AM
sunde887
What's the problem with that code exactly? There is one big error when you create the point object.
• 04-16-2011, 06:35 AM
edprog
No the point object is fine, is the way I want to read the variables, i want to read

3 4 (where 3 is x and 4 is y)
2 3 (where 2 is x and 3 is y)

3
4
2
3
• 04-16-2011, 07:14 AM
sunde887
Your code stores the user input in the variables inputX and inputY and then uses X and Y to create a point object. That's incorrect.

To do what you want is a bit more lines of code. You will be using nextLine from the scanner class to get a string, then strings split method and finally parse the values

Code:

```loop   Declare and initialize string with nextLine   Declare string array and initialize with split   Create point object with parsed values from string array End loop```
• 04-16-2011, 07:17 AM
Dark