-
improper inputs
i am working on a triangle detecting program, that is supposed to take in 3 user inputs for the length of the sides
to ensure that the program doesnt crash or anything, our teacher asked us to put in a loop or something to make sure that every time the user inputs a number less than 0 or any letters, the program is supposed to ask them for a proper input
so i am able to get the users to input a number greater than 0 but the problem i am having is that i cant figure out a way to stop them from entering in any letters
Code:
import java.util.Scanner;
public class Triangle
{
public static void main (String [] args)
{
Scanner reader = new Scanner (System.in);
double a;
double b;
double c;
double largest;
double middle;
double smallest;
System.out.print("Please enter the length of side #1: ");
a = reader.nextDouble();
while( a <= 0 )
{
System.out.println("");
System.out.print("Please enter a number greater than zero: ");
a = reader.nextDouble();
}
System.out.println("");
System.out.print("Please enter the length of side #2: ");
b = reader.nextDouble();
while( b <= 0 )
{
System.out.println("");
System.out.print("Please enter a number greater than zero: ");
b = reader.nextDouble();
}
System.out.println("");
System.out.print("Please enter the length of side #3: ");
c = reader.nextDouble();
while( c <= 0 )
{
System.out.println("");
System.out.print("Please enter a number greater than zero: ");
c = reader.nextDouble();
}
largest = Math.max(a,Math.max(b,c));
smallest = Math.min(a,Math.min(b,c));
middle = a + b + c - largest - smallest;
if ((Math.pow(smallest,2) + Math.pow(middle,2) == Math.pow(largest,2)))
{
System.out.println("");
System.out.println("It is a right triangle.");
}
else
{
System.out.println("");
System.out.println("It is not a right triangle.");
}
if (a == b && a == c && b == c)
{
System.out.println("");
System.out.println("It is an equilateral triangle.");
}
else if (a == b || a == c || b == c)
{
System.out.println("");
System.out.println("It is an isosceles triangle.");
}
else if (a != b && a != c && b != c)
{
System.out.println("");
System.out.println("It is an scalene triangle.");
}
}
}
-
Re: improper inputs
I am not sure but I would write:
Code:
if(!a.contains("1") |! a.contains("2") |!a.contains("3") |!a.contains("4") |!a.contains("5") |!a.contains("6") |!a.contains("7") |!a.contains("8") |!a.contains("9")){
System.out.println("Use only numbers please");
} else {
//put all your code in here from the line 19 to 46 and do the same for the others
}
Oh wait, my code is wrong xD hold on a minute..
-
Re: improper inputs
Ok there is easier way:
Code:
if (a.matches("[0-9]+")) {
//if it has ONLY NUMBERS
} else {
//has at least 1 non-number
}
-
Re: improper inputs
Code:
if(!a.contains("1") |! a.contains("2") |!a.contains("3") |!a.contains("4") |!a.contains("5") |!a.contains("6") |!a.contains("7") |!a.contains("8") |!a.contains("9")){
Arrrrrrrrrrgggggggggggh! My eyes.
A quick and dirty way is to read the input as a String, try parsing to int/double and catch exception. But since you are using the Scanner class try using the hasNext... methods.
Also, whenever you see repeated code you should be thinking about using a loop or a method.
-
Re: improper inputs