Results 1 to 3 of 3
- 09-14-2010, 01:15 AM #1
Member
- Join Date
- Sep 2010
- Location
- Oklahoma
- Posts
- 14
- Rep Power
- 0
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+"");
}
}
- 09-14-2010, 01:22 AM #2
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.
- 09-14-2010, 01:24 AM #3
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
-
Changing Scanner input type
By mlad in forum New To JavaReplies: 13Last Post: 03-18-2010, 04:22 PM -
Count number of digits in string using scanner
By wendysbiggy in forum New To JavaReplies: 35Last Post: 01-20-2010, 05:11 AM -
problem with Scanner in Getting users input
By kliane in forum New To JavaReplies: 8Last Post: 01-17-2010, 04:37 PM -
how to use Scanner with a number
By cew27 in forum New To JavaReplies: 10Last Post: 04-03-2009, 06:23 PM -
Scanner input problem
By slayer_azure in forum New To JavaReplies: 3Last Post: 05-26-2008, 10:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks