Jump to content

JAVA How to add an error MSG.

Sergio45

The program goes through and ask for an input of 5 numbers. It puts the numbers in a array and if a number is entered twice it skips it not showing it as a unique number. I am fairly new to Java and having issues on starting an error message. I want the error message to pop up when the user types in a number less than 10 and larger than 100. I was wondering also if its possible when the error comes up and the user can still input a number for the current placement of the array it was on before the error. If a user inputs 9 when the program was asking for the third number it won't skip to the 4th input it will still accept a third input. Thanks again.

import java.util.Scanner;
import java.util.ArrayList;

public class duplicationClass
{
	public static void main(String[]args)
	{
		Scanner userInput = new Scanner(System.in);
		ArrayList<Integer>userNumbers=new ArrayList<>();
	
		
		System.out.println("Please Enter First Integer:");
		userNumbers.add(userInput.nextInt());
		System.out.println(userNumbers.get(0));
		
		System.out.println("Please Enter Second Integer:");
		check(userNumbers,userInput.nextInt());
		
		
		System.out.println("Please Enter Third Integer:");
		check(userNumbers,userInput.nextInt());

		
		System.out.println("Please Enter Fourth Integer:");
		check(userNumbers,userInput.nextInt());
		
		System.out.println("Please Enter Fifth Integer:");
		check(userNumbers,userInput.nextInt());
		
		
		////////////////////////
		userInput.close();
	}
	
	public static void check(ArrayList<Integer>userNumbers,int input)
	{
		if(userNumbers.contains(input))
		{
			
		
		}
		else
		{
			userNumbers.add(input);
			for(int i=0; i<userNumbers.size();i++)
			{
				int value=userNumbers.get(i);
				System.out.printf("%s","*");
				System.out.println(value);
			}
		}
		
		
		
		
		
		
	
	}
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

uhm.. just read the input number to a variable, check if the variable is within bounds, if it is continue, if it isnt ask again.

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, manikyath said:

uhm.. just read the input number to a variable, check if the variable is within bounds, if it is continue, if it isnt ask again.

I see what you mean but if I do that wouldn't I have to make another scanner or can I use the "userInput" and assign it to a variable call "int check=userInput;" ? Thanks for the feedback.

Link to comment
Share on other sites

Link to post
Share on other sites

21 minutes ago, Sergio45 said:

I see what you mean but if I do that wouldn't I have to make another scanner or can I use the "userInput" and assign it to a variable call "int check=userInput;" ? Thanks for the feedback.

why store the input in a variable then move that to another one? Waste of memory.

 

int userinput = input ()

if userinput in array

skip

else add to array

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×