Results 1 to 11 of 11
Thread: Reading numbers
- 04-15-2011, 07:28 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
Reading numbers
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 #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.
- 04-15-2011, 07:48 AM #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
- 04-15-2011, 09:17 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.Java Code:Ask for number of points to read Loop Read x Read y End loop
- 04-16-2011, 04:11 AM #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)
instead of
3
4
2
1
Any ideas?
- 04-16-2011, 04:15 AM #6
The pseudocode provided by sunde is what you need. You just have to fine tune it.
- 04-16-2011, 05:28 AM #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();
mypoints.add(new Points(X,Y));
}Last edited by edprog; 04-16-2011 at 06:36 AM.
- 04-16-2011, 06:08 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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 #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)
instead of
3
4
2
3
- 04-16-2011, 07:14 AM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
- 04-16-2011, 07:17 AM #11
Edit: Nvm, I didn't fully read your last post. Lol.
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 07:19 AM.
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
Similar Threads
-
Problem getting numbers from user and finding smallest two numbers
By radhi16 in forum New To JavaReplies: 11Last Post: 01-14-2011, 06:36 PM -
Reading strings from the user but dont want to accept numbers ,!
By javanew in forum New To JavaReplies: 1Last Post: 09-24-2010, 07:08 PM -
Sum of 4 numbers...!?
By xmenus in forum New To JavaReplies: 7Last Post: 02-14-2010, 03:36 PM -
problem with reading excel sheet data reading using poi libraries
By sandeepsai17 in forum New To JavaReplies: 5Last Post: 08-21-2009, 11:03 AM -
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10-14-2008, 11:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks