Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Comparing Strings?

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Comparing Strings?

    I'm in an introductory Java class (just started) so I'm pretty bad at it, any advice or help would be greatly appreciated.
    I am having trouble comparing two string variables. I am using .equals() for the two variables "batteryOld" and "batteryCompareYes" .
    Given that the user inputs "Yes" they should be considered equal and therefore the statement "true" right?
    Problem is that instead of going into the if , it fails the condition and goes straight to the else where a system.out prints an error message....
    Any way to fix this so that if both are Yes, it goes into the if loop setting the batteryPenalty variable to true?

    Thanks for any help!

    Here's a the excerpt from the program:
    public static double computeChargeRate(int robotPosition){
    		Scanner input = new Scanner(System.in);
     
    		double robotWeight = 1;
    		String batteryOld = "Null";
    		String batteryCompareYes = "Yes";
    		String batteryCompareNo= "No";
    		boolean batteryPenalty = true;
    		double chargeRate = 0;
     
    		//ask the user the robot's weight (assume weight is measured in kg since not otherwise stated)
    		System.out.print("What is the robot's weight in kg? ");
    		robotWeight = input.nextDouble();
     
    		//ask the user whether the robot's battery is old or not
    		System.out.print("Is the robot's battery old? Please answer Yes or No. ");
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		try {
             		batteryOld = br.readLine();
         	} 
         	catch (IOException ioe) {
             		System.exit(1);
          	}
     
    		System.out.println(batteryOld);
     
    		//preform check to see if the robots's battery is old
    		if((batteryOld.equals(batteryCompareYes))){
    			batteryPenalty = true;
    		}
    		if((batteryOld.equals(batteryCompareNo))){
    			batteryPenalty = false;
    		}
    		else{
    			System.out.println("The input for the robot's battery is not applicable- Error.");
    			return 0;
    		}


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Comparing Strings?

    Hello and welcome to the forums.

    Why have you used a combination of the Scanner and BufferedReader? Just the Scanner will do the job..

    You need to bare in mind the case of what is entered. If you enter yes it will not give the same result as if you enter Yes.

    Also, try updating the code to this:

                //preform check to see if the robots's battery is old
                if((batteryOld.equals(batteryCompareYes))){
                    batteryPenalty = true;
                }
                else if((batteryOld.equals(batteryCompareNo))){
                    batteryPenalty = false;
                }
                else{
                    System.out.println("The input for the robot's battery is not applicable- Error.");
                    return 0;
                }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,181
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: Comparing Strings?

    Just to explain why JavaPF recommended you change this code:

    //preform check to see if the robots's battery is old
            if((batteryOld.equals(batteryCompareYes))){
                batteryPenalty = true;
            }
            if((batteryOld.equals(batteryCompareNo))){
                batteryPenalty = false;
            }
            else{
                System.out.println("The input for the robot's battery is not applicable- Error.");
                return 0;
            }

    Go through this in your head with batteryOld being equal to batteryCompareYes.

    It will reach the first if statement, which evaluates to true, so it enters that block and sets batteryPenalty to true. It then reaches the next if statement (because it's a completely different if statement that it will reach regardless of how the first one evaluates), which evaluates to false, so it enters the else block and prints out the error.

    Changing that second if statement to an "else if" will cause the second if statement to only be checked if the first if statement evaluates to false.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. The Following User Says Thank You to KevinWorkman For This Useful Post:

    wandertheverse (February 4th, 2011)

  5. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Comparing Strings?

    Thank you. That makes sense.

    Another quick question: how do you use the scanner to read a String input?
    input.nextString() ?

    Thanks again for the help.

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Comparing Strings?

    Usually I use input.nextLine() to capture a whole line of text. However, if you want to capture a single "word" (loosely defined as anything separated by spaces), use input.next().

    String inputString = "hello world\nsecond line";
    Scanner input = new Scanner(inputString);
    System.out.println(input.next()); // only prints out hello
    input = new Scanner(inputString);
    System.out.println(input.nextLine()); // prints out hello world

  7. The Following User Says Thank You to helloworld922 For This Useful Post:

    wandertheverse (February 4th, 2011)

Similar Threads

  1. Hangman help. Comparing arrays and conditions.
    By javanewbie1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 18th, 2011, 08:26 AM
  2. Comparing Strings only using the .length() method - possible?
    By Ryker in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 16th, 2010, 05:52 PM
  3. Comparing two files and printing out matching words
    By sport10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 3rd, 2010, 09:10 PM
  4. [SOLVED] Java program to convert and compare integers
    By luke in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 18th, 2009, 06:26 PM
  5. Comparing hash functions and collision resolutions
    By dansongarcia in forum Collections and Generics
    Replies: 0
    Last Post: November 11th, 2008, 09:50 AM

Tags for this Thread