Reputation: 161
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);
Upvotes: 0
Views: 800
Reputation: 425448
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.
Upvotes: 1
Reputation:
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));
Upvotes: 2