1. Member
Join Date
Apr 2011
Posts
5
Rep Power
0

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

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

3. Member
Join Date
Apr 2011
Posts
5
Rep Power
0
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

4. Java Code:
```Ask for number of points to read
Loop
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.

5. Member
Join Date
Apr 2011
Posts
5
Rep Power
0
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?

6. The pseudocode provided by sunde is what you need. You just have to fine tune it.

7. Member
Join Date
Apr 2011
Posts
5
Rep Power
0
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();

}
Last edited by edprog; 04-16-2011 at 07:36 AM.

8. What's the problem with that code exactly? There is one big error when you create the point object.

9. Member
Join Date
Apr 2011
Posts
5
Rep Power
0
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

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

Java 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```

Sunde is correct, the way to accomplish your goal is to store the input to a string. Split the string and parse them into new variables. If this for a school project? If it is then you might not be able to use these tools depending on if you've learned them or not.
Last edited by Dark; 04-16-2011 at 08:19 AM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•