Results 1 to 4 of 4
  1. #1
    atac57 is offline Member
    Join Date
    Jan 2012
    Posts
    24
    Rep Power
    0

    Default Rewrote an example program using the Scanner class instead of TextIO

    I rewrote it using the Scanner class instead of TextIO (which gets an error when I do use it for some reason). I'm not getting any errors but when I run the program, its not recognizing arguments (I think?) inside the if... else if statements where I put "inches", " yards" etc. Instead its going down to the last else statement every single time. I've looked over and over and I can't find the problem. The example came from Javanotes 6.0, Section 3.5 -- The if Statement. Appreciate the help!


    Java Code:
    import java.util.Scanner;
    
    public class peaches{
    	public static void main (String args[]){
    		Scanner variable = new Scanner (System.in);
    		
    		double measurement;
    		double inches, feet, yards, miles;
    		String units;
    		
    		System.out.println("Enter measurements in inches, feet, yards, or miles.");
    		System.out.println("For example: 1 inch   17 feet   2.75 miles");
    		System.out.println("You can use abbreviation: in  ft  yd  mi");
    		System.out.println("I will convert your input into the other units of measure");
    		System.out.println();
    		
    		while (true){
    			System.out.println("Enter your measurement, or 0 to end: ");
    			measurement = variable.nextDouble();
    			
    			if (measurement == 0)
    				break;
    			units = variable.nextLine();
    			units = units.toLowerCase();
    			
    			if (units.equals("inch") || units.equals("inches")
    											|| units.equals("in")){
    				inches = measurement; 
    			}
    			else if (units.equals("foot") || units.equals("feet")
    												|| units.equals("ft")) {
    				inches = measurement * 12; 
    			}
    			else if (units.equals("yard") || units.equals("yards")
    												|| units.equals("yd")){
    				inches = measurement * 36;
    			}
    			else if (units.equals("mile") || units.equals("miles")
    												|| units.equals("mi")){
    				inches = measurement * 12 * 5280;
    			}
    			else{
    				System.out.println("Sorry, but I don't understand \"" + units + "\".");
    				continue;
    			}
    			
    			feet = inches / 12;
    			yards = inches / 36;
    			miles = inches / (12 * 5280);
    			
    			System.out.println();
    			System.out.println("That's equivalent to: ");
    			System.out.printf("%12.5g", inches);
    			System.out.println("inches");
    			System.out.printf("%12.5g", feet);
    			System.out.println("feet");
    			System.out.printf("%12.5g", yards);
    			System.out.println("yards");
    			System.out.printf("%12.5g", miles);
    			System.out.println("miles");
    			System.out.println();					
    		}
    		System.out.println();
    		System.out.println("Ok! Bye for now.");	
    		
    	}
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Rewrote an example program using the Scanner class instead of TextIO

    I'm not getting any errors but when I run the program, its not recognizing arguments (I think?) inside the if... else if statements where I put "inches", " yards" etc. Instead its going down to the last else statement every single time.
    Those conditions will be recognised alright. But you should check that units is not what you expect (and intend).

    Java Code:
    while (true){
        System.out.println("Enter your measurement, or 0 to end: ");
        measurement = variable.nextDouble();
                 
        if (measurement == 0)
            break; //<-- brackets would be a good idea here
        units = variable.nextLine();
        units = units.toLowerCase();
    
        System.out.println("Using units=-->" + units + "<--\n");
    
        if (units.equals("inch") || // etc
    Make sure you understand what the two Scanner methods you use are doing by reading their API documentation.

  3. #3
    atac57 is offline Member
    Join Date
    Jan 2012
    Posts
    24
    Rep Power
    0

    Default Re: Rewrote an example program using the Scanner class instead of TextIO

    Sorry, I'm not really following. You're referring to lines 7 and 8 as the two Scanner methods? If so, I don't understand how that is allowing the if and else if statements to be skipped. I've only been at this for about 2 weeks, so this is all really new to me.

    Java Code:
    while (true){
        System.out.println("Enter your measurement, or 0 to end: ");
        measurement = variable.nextDouble();
                  
        if (measurement == 0)
            break; //<-- brackets would be a good idea here
        units = variable.nextLine();
        units = units.toLowerCase();
     
        System.out.println("Using units=-->" + units + "<--\n");
     
        if (units.equals("inch") || // etc

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Rewrote an example program using the Scanner class instead of TextIO

    You are checking the value of units in all those if statements. What I'm suggesting is that you print the value of units so you can see what it is. That is what line 10 is doing.

    Clearly units has a value that is not "inch", "inches", "feet" and all the rest. Otherwise one of the if blocks would have executed. So the next step would seem to be finding out what its value is by printing it.

    -----

    [edit] The point about braces is that all if statements should use braces: it's just good style. It has nothing to do with the problem you're facing.
    Last edited by pbrockway2; 01-05-2012 at 06:25 AM.

Similar Threads

  1. Using the Scanner Class with jdb
    By flubbernugget in forum New To Java
    Replies: 4
    Last Post: 07-05-2011, 03:17 AM
  2. TextIO class not working
    By trudy in forum New To Java
    Replies: 4
    Last Post: 12-30-2009, 08:48 PM
  3. Using the scanner class
    By danielwestjr in forum New To Java
    Replies: 1
    Last Post: 03-13-2009, 11:49 AM
  4. Scanner Class...
    By TheRocket in forum New To Java
    Replies: 5
    Last Post: 12-05-2008, 10:48 AM
  5. Scanner class
    By ajaymenon.k in forum Advanced Java
    Replies: 1
    Last Post: 11-26-2007, 08:01 AM

Posting Permissions

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