Results 1 to 4 of 4
- 01-05-2012, 02:45 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 24
- Rep Power
- 0
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."); } }
- 01-05-2012, 03:02 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: Rewrote an example program using the Scanner class instead of TextIO
Those conditions will be recognised alright. But you should check that units is not what you expect (and intend).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.
Make sure you understand what the two Scanner methods you use are doing by reading their API documentation.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
- 01-05-2012, 04:49 AM #3
Member
- Join Date
- Jan 2012
- Posts
- 24
- Rep Power
- 0
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
- 01-05-2012, 05:23 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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 05:25 AM.
Similar Threads
-
Using the Scanner Class with jdb
By flubbernugget in forum New To JavaReplies: 4Last Post: 07-05-2011, 02:17 AM -
TextIO class not working
By trudy in forum New To JavaReplies: 4Last Post: 12-30-2009, 07:48 PM -
Using the scanner class
By danielwestjr in forum New To JavaReplies: 1Last Post: 03-13-2009, 10:49 AM -
Scanner Class...
By TheRocket in forum New To JavaReplies: 5Last Post: 12-05-2008, 09:48 AM -
Scanner class
By ajaymenon.k in forum Advanced JavaReplies: 1Last Post: 11-26-2007, 07:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks