Results 1 to 4 of 4
  1. #1
    cs3
    cs3 is offline Member
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Question Counting number of user input

    My program reads input from a user until '0' is entered. How do I count the number of inputs given by the user? My code gives '0' whenever I ask for it.

    Java Code:
    import java.util.Scanner;
    public class NumberLab3 
    {
    	public static void main(String[] args) 
    	{
    		Scanner input = new Scanner (System.in);
    		System.out.print ("Please enter a positive number (0 to terminate): ");
    		
    		int count;
    		count = input.nextInt();
    	
    		while (count != 0) 
    		{
    			
    			if (count != 0)
    				System.out.print ("Please enter another number (0 to terminate): ");
    					count++;
    					count = input.nextInt();
    						
    		}
    		if (count == 0);
    			System.out.println ("Program terminated.");
    			System.out.println ("Number of numbers entered: " + count);
    	}			
    }
    Last edited by cs3; 10-24-2012 at 10:27 AM.

  2. #2
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default Re: Counting number of user input

    You're using count for two things, storing the numbers typed by the user and storing the amount of times it went through the loop.

    Make a second variable.

  3. #3
    cs3
    cs3 is offline Member
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Default Re: Counting number of user input

    How exactly would the second variable operate? I don't know how I'd make it count the number of user inputs

  4. #4
    cs3
    cs3 is offline Member
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Default Re: Counting number of user input

    Never mind, I got it!

    Thank you!

Similar Threads

  1. Replies: 14
    Last Post: 09-13-2011, 12:54 AM
  2. Random Number Generator with User Input
    By person287 in forum New To Java
    Replies: 14
    Last Post: 07-10-2011, 11:49 AM
  3. Counting number of entries in a file
    By lenny0 in forum New To Java
    Replies: 3
    Last Post: 05-10-2011, 04:40 AM
  4. Creating a number of objects based on user input.
    By Kevinius in forum New To Java
    Replies: 21
    Last Post: 04-03-2011, 09:53 AM
  5. geting user input (number)
    By kliane in forum New To Java
    Replies: 9
    Last Post: 01-17-2010, 01:25 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
  •