Results 1 to 11 of 11

Thread: Reading numbers

  1. #1
    edprog is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default 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

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

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

    Default

    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. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

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

  5. #5
    edprog is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    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?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

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

  7. #7
    edprog is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    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.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

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

  9. #9
    edprog is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    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

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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

  11. #11
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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

  1. Replies: 11
    Last Post: 01-14-2011, 06:36 PM
  2. Replies: 1
    Last Post: 09-24-2010, 07:08 PM
  3. Sum of 4 numbers...!?
    By xmenus in forum New To Java
    Replies: 7
    Last Post: 02-14-2010, 03:36 PM
  4. Replies: 5
    Last Post: 08-21-2009, 11:03 AM
  5. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM

Posting Permissions

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