1

I am working on a program that will ask users for an integer and then another and put them as graph points. It will then tell them the distance between them. The user must input an integer or press "Q" to exit. If it is anything else (not an integer or the letter "Q"), it will tell them that is incorrect and to please try again. I thought this is how I could accomplish that but it returns the error cannot find symbol. Help is appreciated greatly!

import java.util.Scanner;

public class PointCheck{

   public static void main(String[] args){

      try {

         System.out.println("Welcome");
         System.out.println("To quit at anytime please type \"Q\". Enter point:");

         char Q = 'Q';
         char q = 'q';
         Scanner scan = new Scanner (System.in);
          input = scan.next();

         while (input.hasNext()) {
            if (input.hasNextInt()) {
               System.out.println("Int input");
            }
            else if (input.equals("Q")) {
               System.out.println("Exiting");

            }
            /*else {
               System.out.println("You did not enter a valid value. Please enter a number or \"Q\" to quit.");
            }*/

         }

      }

      catch(Exception e){
         System.out.println("Exiting Program.");

      }

   }

}

If I don't comment out the last else statement it just defaults to that and loops my error message forever.

Ahmet Emre Kilinc
  • 5,489
  • 12
  • 30
  • 42
  • At the line `input = scan.next()` input doesn't have a type declared. Also after this point you're trying to use `input` instead of `scan` as the `Scanner`. – Kevin Dec 03 '17 at 05:12
  • you havn't declared input , and if you define it as String than hasnext and hasintnext not gonna work with it – Lalit Verma Dec 03 '17 at 05:21

3 Answers3

1

I have made some modification of your code, Hope it helps :)

public class PointCheck {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    try {

        System.out.println("Welcome");
        System.out.println("To quit at anytime please type \"Q\". Enter point:");
        Scanner scan = new Scanner(System.in);

        while (scan.hasNext()) {
            if (scan.hasNextInt()) {
                System.out.println("Int input" + scan.nextInt());
            } else {
                String input = scan.next();
                if (input.equalsIgnoreCase("Q")){
                    System.out.println("Exiting");
                    break;

                }else {
                    System.out.println("You did not enter a valid value. Please enter a number or \"Q\" to quit.");
                }
            }

        }

    } catch (Exception e) {
        System.out.println("Exiting Program.");

    }
}

}
Sohel0415
  • 9,523
  • 21
  • 30
0

You can try this... The input will take only integers, if you enter anything other than an Integer, it will throw an exception and move to catch block where the program will terminate.

import java.util.Scanner;

public class demo {
    public static void main(String[] args) {
        System.out.println("Welcome");
        System.out.println("Press any key to exit.. Enter point:");
        Scanner scanner= new Scanner(System.in);

        while (true){
            try {
                int number = scanner.nextInt();
                System.out.println(number);
            }
            catch (Exception e){
                System.exit(0);
            }
        }
    }
}
iamvinitk
  • 165
  • 3
  • 15
0

Something like this may be to your liking. Read the comments in code:

System.out.println("Welcome");
Scanner scanner= new Scanner(System.in);
int number = 0;
while (true){
    System.out.println("Enter point (q to quit): ");
    String strg = scanner.nextLine();
    // Get out of loop if q or Q or quit, 
    // or QUIT, or Quit, etc is entered.
    // providing the input actually contains
    // something.
    if (!strg.equals("") && strg.toLowerCase().charAt(0) == 'q') {
        break;
    }
    // Use the String.matches() method with regex to 
    // determine if an integer number was supplied.
    if (!strg.matches("\\d+")) {
        System.err.println("Invalid Entry - Integer values only! Try again.\n");
        continue;   // Start loop from beginning.
    }
    // Convert string number to Integer.
    number = Integer.parseInt(strg);
    break;  // Get outta loop      
}
String msg = "The number entered is: " + number;
if (number == 0) { msg = "Nothing Entered!"; }
System.out.println(msg);
DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22