Results 1 to 8 of 8
  1. #1
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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:
    Java Code:
    inputst = scanner.next();
    inputst = scanner.nextLine();
    to this:

    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 later
    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.
    Last edited by Fubarable; 10-07-2012 at 01:13 AM.

  3. #3
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: Need help finding my mistake/fixing my problem

    Quote Originally Posted by Fubarable View Post
    You seem to be ignoring the user's input by over-writing it with an empty String. To see what I mean, change this:]
    inputst = scanner.next();
    inputst = scanner.nextLine();
    [/code]

    to this:

    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 later
    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.

    It's not an assignment lol, I like to learn this myself just something I aim working on so that I have an aim, and thanks.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  5. #5
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default 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!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: Need help finding my mistake/fixing my problem

    Quote Originally Posted by DarrylBurke View Post
    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.

    db
    Sorry about that, completely forgot! i do apologise.

Similar Threads

  1. Another problem that needs fixing : )
    By Mirr0ge in forum New To Java
    Replies: 3
    Last Post: 03-28-2011, 11:23 PM
  2. problem with finding imported classes
    By frx08 in forum Java Applets
    Replies: 1
    Last Post: 01-19-2011, 12:51 PM
  3. Problem finding SQLite JDBC driver
    By PrinceSendai in forum JDBC
    Replies: 9
    Last Post: 08-09-2010, 02:24 AM
  4. Problem with finding the max element
    By SMHouston in forum New To Java
    Replies: 39
    Last Post: 09-13-2009, 11:34 PM
  5. ArrayList problem (finding largest no)
    By bugger in forum New To Java
    Replies: 3
    Last Post: 12-12-2007, 01:47 PM

Posting Permissions

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