Results 1 to 5 of 5

Thread: improper inputs

  1. #1
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default 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.");
             }
          }
       }

  2. #2
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default 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 
    }
    Oh wait, my code is wrong xD hold on a minute..
    Last edited by Lionlev; 10-19-2012 at 01:03 AM.
    WARNING I am Russian so it's possible that I wont understand you correctly...

  3. #3
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: improper inputs

    Ok there is easier way:
    Java Code:
    if (a.matches("[0-9]+")) {
    			//if it has ONLY NUMBERS
    		} else {
    			//has at least 1 non-number
    		}
    WARNING I am Russian so it's possible that I wont understand you correctly...

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default 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")){
    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.

  5. #5
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: improper inputs

    Read my second post
    WARNING I am Russian so it's possible that I wont understand you correctly...

Similar Threads

  1. Replies: 3
    Last Post: 09-06-2012, 06:30 PM
  2. Same inputs, different outputs.
    By werner291 in forum New To Java
    Replies: 4
    Last Post: 12-15-2011, 08:36 PM
  3. Help with 2-D Arrays and Inputs
    By CJ1031 in forum New To Java
    Replies: 2
    Last Post: 04-16-2011, 03:56 AM
  4. How to create this if many inputs?
    By sarahannel123 in forum New To Java
    Replies: 3
    Last Post: 05-18-2008, 05:22 PM
  5. Date Inputs
    By hiranya in forum AWT / Swing
    Replies: 3
    Last Post: 11-06-2007, 06:11 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •