0

This is my main method, I need a way to check if the input isn't a integer, for when they are Strings i can enter a value such as "12345" and it would still be a string. Although i am not sure how to differentiate between a string, which can be anything, versus something just a integer?

import java.math.*;
import java.text.DecimalFormat;
import java.io.*;
import java.util.*;


 public class labBookFortyEight
{
     public static void main(String[] args)
     { 
    String make = null;
    String model = null;
    String color = null;
    String lNum = null;
    int min = 0;
    int quarters = 0;

    Scanner myInput = new Scanner(System.in);

        while(make == null && model == null && color == null && lNum == null && min == 0 && quarters == 0)
        {
            System.out.println("Please enter make, model, color, lisense num and mins spent ");

              if (myInput.hasNext()) {
                  System.out.println("you entered make");
                make = myInput.nextLine();
              }
                if(myInput.hasNext()){
                    System.out.println("you entered model");
                  model = myInput.nextLine();
              }
              if (myInput.hasNext()) {
                  System.out.println("you entered color");
                color = myInput.nextLine();
              }
              if (myInput.hasNext()) {
                  System.out.println("you entered lNum");
                lNum = myInput.nextLine();
             }
            if(myInput.hasNextInt()){
                System.out.println("you entered mins spent");
                min = myInput.nextInt();
                myInput.nextLine(); //consumes newline char from nextLine
            }
            if(myInput.hasNextInt()){
                System.out.println("you enter quarters entered");
                quarters = myInput.nextInt();
            }
              else if (myInput.hasNext()) {
                System.out.println("Please enter a proper value");
                myInput.next();
              } else {
                System.err.println("No more input");
                System.exit(1);
              }
        }
                System.out.println("the values you entered are " + make + " " + model + " " + color + " " + lNum + " " + min);
3
  • 1
    Float have isNumber method you can check it by using this. Commented Aug 10, 2014 at 14:04
  • Do you want the user to type an integer, and if none provided, ask him again until he provide one valid ? Commented Aug 10, 2014 at 14:25
  • 1
    Check out the following post for a few solutions: stackoverflow.com/questions/237159/… Commented Aug 10, 2014 at 17:22

2 Answers 2

2

For this sort of thing you may need to look into regex or regular expressions which you can check if it matches.

String regex = "^[0-9]";
String data = "23343453";
System.out.println(data.matches(regex));
Sign up to request clarification or add additional context in comments.

Comments

1

Use regex:

if (str.matches("\\d+"))

The regex \d+ means "one or more digits", and matches() only returns true if the whole String matches the regex.

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.