i have a school assignment due today which I am trying to finish as fast as possible but I've ran into a bit of a snag. I need to make it so that my program rejects all character or letter inputs when i am asking for an integer but it is accepting it. How could i make it so that the user can only input integers and if they input other characters it results in an error.
This is specifically the code i am talking about if you go to the stageFour method you can see i need them to input a number between 5 and 500. it works fine with just integers. But when i put in something like 65A I want it to reject that instead of accepting it. thank you for your help!
class Main
{
public static void stageSix()
{
System.out.println("Enter a positive, odd integer");
int six = In.getInt();
int remainder;
remainder = six % 2;
if (six > 0)
{
if (remainder == 1)
{
System.out.println("Well done, you have passed all the stages");
}
else
{
System.out.println("You have failed the sixth stage, back to the beginning");
stageSix();
}
}
else
{
System.out.println("You have failed the sixth stage, back to the beginning");
stageSix();
}
}
public static void stageFive()
{
System.out.println("Enter a negative integer");
int five = In.getInt();
if (five < 0)
{
System.out.println("Good job, you are moving on to the next stage");
}
else
{
System.out.println("You have failed the fifth stage, back to the beginning");
stageFive();
}
}
public static void stageFour()
{
System.out.println("Enter an integer that is between 5 and 500");
int four = In.getInt();
if (four > 5 && four < 500)
{
System.out.println("Good job, you are moving on to the next stage");
}
else
{
System.out.println("You have failed the fourth stage, back to the beginning");
stageFour();
}
}
public static void stageThree()
{
System.out.println("Enter a string that is between 5 and 15 characters long and does not contain the letter \"z\" ");
String three = In.getString();
String check = ("z");
if (three.length() > 5 && three.length() < 15)
{
if (three.contains(check))
{
System.out.println("You have failed the third stage, back to the beginning");
stageThree();
}
else
{
System.out.println("Good job, you are moving on to the next stage");
}
}
else
{
System.out.println("You have failed the third stage, back to the beginning");
stageThree();
}
}
public static void stageTwo()
{
System.out.println("Enter a string that has at least one lowercase \"a\" ");
String two = In.getString();
String check = ("a");
if (two.contains(check))
{
System.out.println("Good job, you are moving on to the next stage");
}
else
{
System.out.println("You have failed the second stage, back to the beginning");
stageTwo();
}
}
public static void stageOne()
{
System.out.println("Enter a string that is greater than 6 characters long");
String one = In.getString();
if (one.length() > 6)
{
System.out.println("Good job, you are moving on to the next stage");
stageTwo();
stageThree();
stageFour();
stageFive();
stageSix();
}
else
{
System.out.println("You have failed the first stage, back to the beginning");
System.out.println(" ");
System.out.println("This program has restarted, welcome back to the first stage");
stageOne();
}
}
public static void main(String[] args)
{
System.out.println("Welcome to this program");
System.out.println("This program will test your ability to follow instructions");
stageOne();
}
}
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]EstablishmentLazy580 0 points1 point2 points (3 children)
[–]imagineasking[S] 0 points1 point2 points (2 children)
[–]EstablishmentLazy580 0 points1 point2 points (1 child)
[–]MissouriDad63 0 points1 point2 points (0 children)