Results 1 to 3 of 3
  1. #1
    ftrengnr is offline Member
    Join Date
    Sep 2010
    Location
    Oklahoma
    Posts
    14
    Rep Power
    0

    Question how to input unspecified number if ints with a scanner

    i have to get input using the scanner class which is no big deal but it has to read an unspecified number of digits and am not sure how to do that and haven't had any luck finding out how.... this program runs on a loop and i dont think my problem is the loop maybe the input scanner? here is what i have and it is just my first draft....

    //import scanner
    import java.util.Scanner;

    public class NumberLoop {

    //main method.
    public static void main(String[] args) {

    //variables and user prompts
    System.out.println("Please enter your numbers.");

    Scanner inputScanner = new Scanner(System.in);
    int input = inputScanner.nextInt();
    int countPlus = 0;
    int countNegative = 0;
    int total = 0;


    //loop
    while(input != 0){
    if(input < 0){
    countNegative++;
    }
    else if (input > 0){
    countPlus++;
    }
    total++;


    }
    System.out.println(countNegative+" "+countPlus+" "+total+"");


    }

    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Inside your while loop, you need to reassign input, otherwise it will use the same variable over and over and the loop will never end.

    Note the line int input = inputScanner.nextInt();--you will want to use something similar to this within your while loop.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    You will have a problem when you execute the loop in your program.The variable that controls the looping is not changed in the loop. It will loop forever.
    You need to change the variable's value inside the loop, probably by reading in a new number from the user.

    Please use code tags when posting your code. Read this: Java Forums - BB Code List

Similar Threads

  1. Changing Scanner input type
    By mlad in forum New To Java
    Replies: 13
    Last Post: 03-18-2010, 05:22 PM
  2. Count number of digits in string using scanner
    By wendysbiggy in forum New To Java
    Replies: 35
    Last Post: 01-20-2010, 06:11 AM
  3. problem with Scanner in Getting users input
    By kliane in forum New To Java
    Replies: 8
    Last Post: 01-17-2010, 05:37 PM
  4. how to use Scanner with a number
    By cew27 in forum New To Java
    Replies: 10
    Last Post: 04-03-2009, 07:23 PM
  5. Scanner input problem
    By slayer_azure in forum New To Java
    Replies: 3
    Last Post: 05-26-2008, 11:49 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
  •