user3240125
user3240125

Reputation: 31

How to input a lot of data until you type in an invalid number in java

User inputs numbers one by one and then once they type in an invalid number (has to be from 1-200) the program calculates the average of the numbers that were inputted.

I'm just wondering what would the code be for this. I know the one for inputting one piece of data. Example would be:

 `Scanner in = new Scanner(System.in);
 String numberOfShoes = "";

 System.out.println("Enter the number of shoes you want: (0-200) ");
 numberOfShoes = in.nextLine();`

this is just an example, but this time I want the user to input a lot of numbers. I know I'm going to include a loop somewhere in this and I have to stop it once it contains an invalid number (using a try catch block).

* I would also like to add that once the user inputs another number it always goes to the next line.

Upvotes: 1

Views: 576

Answers (4)

Michael Yaworski
Michael Yaworski

Reputation: 13483

Just use a while loop to continue taking input until a condition is met. Also keep variables to track the sum, and the total number of inputs.

I would also suggest having numberOfShoes be an int and use the nextInt() method on your Scanner (so you don't have to convert from String to int).

System.out.println("Enter your number of shoes: ");
Scanner in = new Scanner(System.in);

int numberOfShoes = 0;
int sum = 0;
int numberOfInputs = 0;

do {
    numberOfShoes = in.nextInt();

    if (numberOfShoes >= 1 && numberOfShoes <= 200) { // if valid input
        sum += numberOfShoes;
        numberOfInputs++;
    }

} while (numberOfShoes >= 1 && numberOfShoes <= 200); // continue while valid

double average = (double)sum / numberOfInputs;

System.out.println("Average: " + average);

Sample:

Enter your number of shoes: 
5
3
7
2
0
Average: 4.25

It added 5 + 3 + 7 + 2 to get the sum of 17. Then it divided 17 by the numberOfInputs, which is 4 to get 4.25

Upvotes: 1

Alvin Bunk
Alvin Bunk

Reputation: 7764

I think you need to do something like this (which @Takendarkk suggested):

import java.util.Scanner;

public class shoes {

    public void main(String[] args){
        int input = 0;
        do{
            Scanner in = new Scanner(System.in);
            String numberOfShoes = "";

            System.out.println("Enter the number of shoes you want: (0-200) ");
            numberOfShoes = in.nextLine();
            input = Integer.parseInt(numberOfShoes);
        }while((input>=0) && (input<=200));

    }
}

Upvotes: 1

spiderman
spiderman

Reputation: 11122

you are almost there.

Logic is like this,

   Define array

    Begin Loop
     Accept the number
     check if its invalid number [it is how u define a invalid number]
       if invalid, Exit Loop
       else  put it in the array

    End Loop

    Add all numbers in your array 

Upvotes: 1

karan
karan

Reputation: 8853

you can use for loop like this

for(::)
{
    //do your input and processing here

    if(terminating condition satisified)
    {
        break;
    }
}

Upvotes: 0

Related Questions