Results 1 to 5 of 5
Thread: improper inputs
 10182012, 10:57 PM #1Member
 Join Date
 Aug 2012
 Posts
 11
 Rep Power
 0
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
Java 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."); } } }
 10182012, 11:48 PM #2Senior Member
 Join Date
 May 2012
 Posts
 172
 Rep Power
 8
Re: improper inputs
I am not sure but I would write:
Java 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 }
Last edited by Lionlev; 10192012 at 12:03 AM.
WARNING I am Russian so it's possible that I wont understand you correctly...
 10192012, 12:14 AM #3Senior Member
 Join Date
 May 2012
 Posts
 172
 Rep Power
 8
Re: improper inputs
Ok there is easier way:
Java Code:if (a.matches("[09]+")) { //if it has ONLY NUMBERS } else { //has at least 1 nonnumber }
WARNING I am Russian so it's possible that I wont understand you correctly...
 10192012, 02:49 AM #4
Re: improper inputs
Java 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")){
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.
 10192012, 12:16 PM #5Senior Member
 Join Date
 May 2012
 Posts
 172
 Rep Power
 8
Similar Threads

get three inputs at one time, then perform the conversions on the three inputs!
By niloufar in forum New To JavaReplies: 3Last Post: 09062012, 05:30 PM 
Same inputs, different outputs.
By werner291 in forum New To JavaReplies: 4Last Post: 12152011, 07:36 PM 
Help with 2D Arrays and Inputs
By CJ1031 in forum New To JavaReplies: 2Last Post: 04162011, 02:56 AM 
How to create this if many inputs?
By sarahannel123 in forum New To JavaReplies: 3Last Post: 05182008, 04:22 PM 
Date Inputs
By hiranya in forum AWT / SwingReplies: 3Last Post: 11062007, 05:11 PM
Bookmarks