Results 1 to 6 of 6
Like Tree3Likes
  • 1 Post By Sierra
  • 1 Post By Wnt2bsleepin
  • 1 Post By Sierra

Thread: How do you compare strings (see program)

  1. #1
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default How do you compare strings (see program)

    There seems to be a problem with my while loop, I'm trying to use a sentinel value but when I do put in "-1" (flag to exit loop) there is some kind of error

    now I've used sentinel values with integers, is there something wrong with comparing a string in this way? whats the solution

    Java Code:
    package chapter_v;
    
    import java.util.Scanner; 
    
    public class CalculatingSales_5_17 
    {
    	public static void main(String[] args)
    	{
    		Scanner input = new Scanner(System.in);
    		String pair;
    		double retail = 0;
    		double total = 0;
    		
    		System.out.println("Please input product number and quantity of form (x-y)");
    		pair = input.nextLine();
    		while(pair != "-1")
    		{
    			String values[] = pair.split("-");
    		
    			switch(Integer.parseInt(values[0]))
    			{
    				case 1:
    					retail = 2.98; 
    					break;
    				case 2:
    					retail = 4.50;
    					break;
    				case 3:
    					retail = 9.98;
    					break;
    				case 4:
    					retail = 4.49;
    					break;
    				case 5:
    					retail = 6.87;
    					break;
    			}
    		
    			for (int i = 1 ; i <= Integer.parseInt(values[1]) ; i++)
    			{
    				total = (total + retail);
    			}
    			
    			System.out.println("Please input another product number and quantity of form (x-y)");
    			pair = input.nextLine();
    		}
    		
    		System.out.println("Total is " + total);
    	}
    }
    thanks

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: How do you compare strings (see program)

    Wrong:
    while(pair != "-1")

    Correct:
    while(!pair.trim().equals("-1"))
    Or better:
    while(Integer.parseInt(pair) != -1 )

    And before you access "values[1]" make sure there is actually a "values[1]"... else you get an error.
    And it would be great if you POST the error and do not just say "some kind of error"
    There are more errors in the code but I guess some you need to find yourself now.
    Last edited by Sierra; 05-03-2012 at 08:36 PM.
    EscSequenceAlpha likes this.
    I like likes!

  3. #3
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: How do you compare strings (see program)

    yeah some kind of error sorry. but I didnt point to the position of the error.

    anyway thanks for your help but I want to know whats going on here with the strings. I see there is a .equals method for comparing them the way I would like to.

    but what does it mean the way I did it? (difference between equals method and equality with Strings

    thanks)

  4. #4
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: How do you compare strings (see program)

    I tried both your suggestions and
    while(!pair.trim().equals("-1"))
    works fine, it will sum it up and print the result after -1 is entered

    this however
    while(Integer.parseInt(pair) != -1 )
    doesnt work, when I put, say, 1-3 I get this error (eclipse)

    Exception in thread "main" java.lang.NumberFormatException: For input string: "1-3"
    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at chapter_v.CalculatingSales_5_17.main(CalculatingSa les_5_17.java:16)

  5. #5
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: How do you compare strings (see program)

    Quote Originally Posted by EscSequenceAlpha View Post
    I tried both your suggestions and
    while(!pair.trim().equals("-1"))
    works fine, it will sum it up and print the result after -1 is entered

    this however
    while(Integer.parseInt(pair) != -1 )
    doesnt work, when I put, say, 1-3 I get this error (eclipse)

    Exception in thread "main" java.lang.NumberFormatException: For input string: "1-3"
    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at chapter_v.CalculatingSales_5_17.main(CalculatingSa les_5_17.java:16)
    It's throwing that error because it's trying to turn something that is not a number into a number. You need to use the equals method with Strings because unlike the primitive types: int, double etc., they are objects. When you do ==, you are in turn asking the computer to determine if they are the same objects. Since they are unique and occupy different spots in memory, that will return false. The equals method actually checks the content of the String object to see if it contains the same as another String object.
    EscSequenceAlpha likes this.

  6. #6
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: How do you compare strings (see program)

    Right, it fails because it is not always a valid integer you try to read in. So probably what you want is the equal() method. Note for later applications that there is also a method that ignores the case of the statement. Also be aware of having to use the method instead of the comparator.
    EscSequenceAlpha likes this.
    I like likes!

Similar Threads

  1. Compare two strings
    By Proshitness in forum JDBC
    Replies: 1
    Last Post: 10-12-2011, 10:06 PM
  2. Compare two strings
    By roud9 in forum New To Java
    Replies: 1
    Last Post: 11-05-2010, 12:57 AM
  3. Compare between 2 Strings
    By ChaosINC in forum New To Java
    Replies: 3
    Last Post: 01-17-2010, 12:39 PM
  4. Replies: 15
    Last Post: 11-06-2009, 11:31 AM
  5. how to compare two strings
    By elizabeth in forum New To Java
    Replies: 7
    Last Post: 08-06-2007, 04:57 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
  •