Results 1 to 8 of 8
- 10-06-2012, 10:51 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 54
- Rep Power
- 0
Need help finding my mistake/fixing my problem
I have created a program that should convert back and forth as you will see, so basically if the input was higher for test purposes the print should say higher but it always says lower and I am unable to work out why, so could someone please help!
Java Code:import java.util.Scanner; import java.util.Random; public class MainMenu { public static boolean win = false; public static String answer; public static String trys; public static int ansint; public static int input; public static String inputst; public static void main(String[] args){ Random random = new Random(); Scanner scanner = new Scanner(System.in); ansint = random.nextInt(+13) +1; System.out.println(ansint); System.out.println(answer); System.out.println("Higher or Lower?"); inputst = scanner.next(); inputst = scanner.nextLine(); if(inputst.equalsIgnoreCase("Higher")){ input = ansint + 1; } else if(inputst.equalsIgnoreCase("Lower")){ input = ansint - 1; } if(input > ansint){ answer = "Higher"; } else if(input < ansint){ answer = "Lower"; } System.out.println(answer); scanner.close(); } }
-
Re: Need help finding my mistake/fixing my problem
You seem to be ignoring the user's input by over-writing it with an empty String. To see what I mean, change this:
to this:Java Code:inputst = scanner.next(); inputst = scanner.nextLine();
This is a very simple debugging technique where you print out the values held your variables to see what they're doing and why your code is misbehaving. Just remember to delete the debug lines before handing in your assignment.Java Code:inputst = scanner.next(); System.out.println("Debug: inputst is: " + inputst); // TODO: delete this later inputst = scanner.nextLine(); System.out.println("Debug: inputst is: " + inputst); // TODO: delete this laterLast edited by Fubarable; 10-07-2012 at 12:13 AM.
- 10-06-2012, 11:33 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 54
- Rep Power
- 0
-
Re: Need help finding my mistake/fixing my problem
You're welcome. So you've fixed your problem then?
- 10-06-2012, 11:57 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 54
- Rep Power
- 0
Re: Need help finding my mistake/fixing my problem
not yet, appears to be a problem with the Scanner input. the program doesn't seem to recognise when higher or lower is being used!
-
Re: Need help finding my mistake/fixing my problem
Have you changed your code based on what the println messages were telling you? If not, then you'll want to do so. If so, and you're still having problems, then please post your latest version of your program then. Let's take another look at it.
- 10-07-2012, 06:27 AM #7
Re: Need help finding my mistake/fixing my problem
TANKDS, I thnk you've been around long enough that you should have by now gone through the Forum Rules. Please read that page, particularly the third paragraph.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-07-2012, 12:42 PM #8
Member
- Join Date
- Feb 2012
- Posts
- 54
- Rep Power
- 0
Similar Threads
-
Another problem that needs fixing : )
By Mirr0ge in forum New To JavaReplies: 3Last Post: 03-28-2011, 10:23 PM -
problem with finding imported classes
By frx08 in forum Java AppletsReplies: 1Last Post: 01-19-2011, 11:51 AM -
Problem finding SQLite JDBC driver
By PrinceSendai in forum JDBCReplies: 9Last Post: 08-09-2010, 01:24 AM -
Problem with finding the max element
By SMHouston in forum New To JavaReplies: 39Last Post: 09-13-2009, 10:34 PM -
ArrayList problem (finding largest no)
By bugger in forum New To JavaReplies: 3Last Post: 12-12-2007, 12:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks