I have just started programming and I have a question. Any help will be greatly appreciated. So I want to test if input received from scanner is not an integer, and if it isn't an integer I would like to System.out.println("The number you have entered is not valid"); It is for a password generator and if they do not enter a valid number for how many characters they would like, it will return by saying that it is not valid.
Unfortunately I cannot use else statement, because there are
Here is my code:
import java.util.Random;
import java.util.Scanner;
public class pGenerator {
public static void main(String[] args) {
final String characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; //gives possible character selections
final int N = characters.length();
Random r = new Random(); //creates a new random function
Scanner input = new Scanner(System.in); //scanner enables user to input something
System.out.println("Enter number of characters you would like your password to be:"); //asks user the amount of characters they would like their password to be
int passwordcharacters = (int) input.nextDouble(); //
while (passwordcharacters >0) { //while the number of characters is more that 0, continue with following code
System.out.print(characters.charAt(r.nextInt(N))); //prints out a random character from the String characters
--passwordcharacters; //minuses 1 from number inputed and continues with code if it is more that 0
}
if (input.nextDouble() < 0) { //tests for if number is less than 0
System.out.println("The number you have entered is less than 0");
}
if (input.nextDouble() == 0); { //tests for if number is 0
System.out.println("The number you have entered is 0");
}
}
}
Is there a possible way to do something like this (I know this is wrong, but just an example)
if (input.nextDouble() != integer); { //tests for if number is not an integer
System.out.println("The number you have entered is invalid");
Thank you for your time :)
EDIT:// Hey all :D Thanks for all your amazing help! It works 100% correctly now :)
Here is the code:
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
public class pGenerator {
public static void main(String[] args) {
final String characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; //gives possible character selections
final int N = characters.length();
Random r = new Random(); //creates a new random function
Scanner input = new Scanner(System.in); //scanner enables user to input something
System.out.println("Enter number of characters you would like your password to be:"); //asks user the amount of characters they would like their password to be
int passwordcharacters = input.nextInt(); //
try{
if (passwordcharacters > 0){
while (passwordcharacters >0) { //while the number of characters is more that 0, continue with following code
System.out.print(characters.charAt(r.nextInt(N))); //prints out a random character from the String characters
--passwordcharacters; //minuses 1 from number inputed and continues with code if it is more that 0
}
}else{
System.out.println("The number you have entered is invalid.");
}
}
catch(InputMismatchException ex){
System.out.println("The character you have entered is invalid.");
}
}
}
if (!input.hasNextInt()) {/* no integers here */}nextDouble()after asking this. The length of the password will always be a positive integer.