0

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.");   
    }

    }
}
3
  • if (!input.hasNextInt()) {/* no integers here */} Commented Dec 14, 2014 at 14:20
  • What are you trying to do by comparing integer and double? Double will always be floating point number hence you dont need to check. if you are after decimal point then clarify Commented Dec 14, 2014 at 14:22
  • "//asks user the amount of characters they would like their password to be" It doesn't make sense to call nextDouble() after asking this. The length of the password will always be a positive integer. Commented Dec 14, 2014 at 14:26

2 Answers 2

2

Use try..catch():

while (true) {
    System.out.println("Enter number of characters you would like your password to be:");
    try{
        int passwordcharacters=input.nextInt(); //Why do you use nextDouble?
    } catch(NumberFormatException e) {
        System.out.println("Invalid input");
        input.nextLine();
        continue;//loop again
   }
   break;
}

Or as @BackSlash suggested,You can loop until a valid integer was entered using

while(!input.hasNextInt())
{
  System.out.println("Invalid input!");
  input.nextLine();
}
//Once the execution of the program reaches here,an integer was entered.So scan it
int passwordcharacters=input.nextInt();
Sign up to request clarification or add additional context in comments.

3 Comments

Actually, it's better to use an if, since there is that possibility. It's much lighter and makes the code much more readable.
You mean if(!(passwordcharacters=input.nextInt()))/*Integer scanning failed*/?
No, I mean if (!input.hasNextInt()) {/* no integers here */}. In your specific example, it would be while (!input.hasNextInt()) {System.out.println("Invalid input"); input.nextLine();} int passwordcharacters = input.nextInt();
0

Here is your code revised a little bit:

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.0) { //tests for if number is less than 0.0
        System.out.println("The number you have entered is less than 0");           

    }
    else if (input.nextDouble() == 0.0); { //tests for if number is 0.0
        System.out.println("The number you have entered is 0");     

    }
    else if(input.hasNextInt()) {
        System.out.println("No integers please!");
    }
}

Else/If Statements are handy in situations where just if statements and regular else statements wont work. They work by adding more conditions to "else".

Integers and Doubles look different. 0 is the integer version of "zero" and 0.0 is the double precious version of "zero. (Very simplified explanation)

I hope that helps. Happy coding!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.