Results 1 to 4 of 4
  1. #1
    trishtren is offline Member
    Join Date
    Feb 2011
    Posts
    53
    Rep Power
    0

    Smile nextInt() repeat

    Hello, im having a rather odd repetition problem when using nextInt. I request a user to input data using :
    Java Code:
    private int getInput(int input) {
    Scanner b = new Scanner(System.in);
    		System.out.print("Input " + input + ", please enter input: ");
    
    return b.nextInt();
    }
    The above works fine however when i try to use b.next int as a parameter in a function it seems to repeat the input process twice, only it doesent output text, it justs prints a blank line on which if i input a number into it carrys on as normal.

    Java Code:
    private int getInput(int input) {
    Scanner b = new Scanner(System.in);
    		System.out.print("Input " + input + ", please enter input: ");
    printdata(b.nextInt());
    return b.nextInt();
    The function it is calling does not use b.nextint for anything other than a number, it simply takes the number and prints it. The program is fully functional but i would like it not to request 2 inputs everytime. Im pretty sure it must be from the nextint function but cant figure why?

    Any help would be nice :)

    Java Code:
    //simple print data function causing the problem
    private int printdata(int input) {
    saveFile();
    System.out.print("saved to file" + input )
    }

    }
    Last edited by trishtren; 10-09-2011 at 05:46 PM.

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

    Default Re: nextInt() repeat

    You're calling it twice here:

    Java Code:
    private int getInput(int input) {
       Scanner b = new Scanner(System.in);
       System.out.print("Input " + input + ", please enter input: ");
       printdata(b.nextInt());
       return b.nextInt();
    }
    So it shouldn't be surprising that you're seeing this behavior. The solution is to call it only once and save the result.
    e.g.,

    Java Code:
    private int getInput(int input) {
       Scanner b = new Scanner(System.in);
       System.out.print("Input " + input + ", please enter input: ");
       int result = b.nextInt();
       printdata(result);
       return result;
    }

  3. #3
    trishtren is offline Member
    Join Date
    Feb 2011
    Posts
    53
    Rep Power
    0

    Default Re: nextInt() repeat

    massively helpful and a speedy reply, thanks tons!

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

    Default Re: nextInt() repeat

    You're welcome. Note that most methods like that wouldn't print out to the console but rather would simply return the result obtained.

Similar Threads

  1. Replies: 1
    Last Post: 09-30-2011, 12:13 AM
  2. nextint() Method !
    By M7MD in forum Threads and Synchronization
    Replies: 6
    Last Post: 10-25-2010, 06:13 AM
  3. Very Bad problem with nextInt()
    By usiyalla in forum New To Java
    Replies: 2
    Last Post: 07-25-2010, 01:44 AM
  4. Replies: 2
    Last Post: 05-23-2010, 11:05 PM
  5. nextInt(unknown source)
    By babyburger in forum New To Java
    Replies: 3
    Last Post: 03-18-2010, 04:03 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
  •