Results 1 to 10 of 10
  1. #1
    lyondor is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default input digits- separate

    hi!

    I have a doubt in my code.

    For example:

    Java Code:
       1. import java.util.Scanner();  
       2.   
       3. public class Example{  
       4.          public static void main (String [ ] args){  
       5.                
       6.           int dados;  
       7.            
       8.           Scanner teclado= new Scanner (System.in);  
       9.           System.out.print("Insert numbers:");   // I would like to input separate digits (9 9 9 9)  
      10.           dados=teclado.nextInt();  
      11.          
      12.     }  
      13. }
    How I make this?

    thanks

  2. #2
    noobForever is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    I think you either need to create additional variables to hold the integer values, or setup an array to hold multiple values from the user input..

  3. #3
    lyondor is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    No, I just insert the number and automatically add a space...
    Input: 2(automatically space)3(automatically space)4(automatically space)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    noobForever is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Then you would need to do something like this i suspect..

    Java Code:
    import java.util.Scanner;
    
    public class Example{
    public static void main( String args[] ){
    
    Scanner teclado = new Scanner( System.in );
    
    int num1;
    int num2;
    int num3;
    int num4;
    int num5;
    
    System.out.print( "Insert Numbers: ");
    num1 = input.nextInt();
    num2 = input.nextInt();
    num3 = input.nextInt();
    num4 = input.nextInt();
    num5 = input.nextInt();
    }
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by noobForever View Post
    Then you would need to do something like this i suspect..
    I don't think we can assume this. We certainly need to know more about the original poster's requirements before making hard recommendations.

  7. #7
    noobForever is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    I shall shut my noise hole then.

  8. #8
    lyondor is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    But what will the user have to type in? Something like:
    2<enter>
    3<enter>
    4<enter>


    ?
    +/-

    Something like: 2 <space> 3 <space> 4 <space>

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by noobForever View Post
    I shall shut my noise hole then.
    Me too, that is until the original poster (the OP) posts enough information so that his question is answerable.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    So, the display will show -
    Insert numbers:
    And then the user will type -
    Insert numbers: 2 3 4<Enter>
    And you want to take in those individual numbers separated by a space?

    Then read a String, not an int, and split the String on a space and then parseInt() the individual parts of the String[] you get back.

Similar Threads

  1. reversing digits
    By gandalf5166 in forum New To Java
    Replies: 3
    Last Post: 03-06-2010, 04:34 PM
  2. Digits of an integer.
    By Allgorythm in forum New To Java
    Replies: 8
    Last Post: 01-01-2010, 02:34 AM
  3. Should I separate my code into separate files?
    By Inks in forum New To Java
    Replies: 0
    Last Post: 03-26-2009, 12:12 AM
  4. split() by digits
    By RobertF in forum New To Java
    Replies: 2
    Last Post: 03-12-2009, 02:16 AM
  5. sum of digits depreciation
    By jleas in forum New To Java
    Replies: 13
    Last Post: 11-09-2008, 01:37 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
  •