Results 1 to 6 of 6
- 05-03-2012, 06:59 PM #1
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
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
thanksJava 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); } }
- 05-03-2012, 07:33 PM #2
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
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 07:36 PM.
I like likes!.gif)
- 05-03-2012, 08:40 PM #3
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
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)
- 05-03-2012, 08:52 PM #4
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
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)
- 05-03-2012, 09:24 PM #5
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: How do you compare strings (see program)
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.
- 05-04-2012, 12:22 PM #6
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
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.
I like likes!.gif)
Similar Threads
-
Compare two strings
By Proshitness in forum JDBCReplies: 1Last Post: 10-12-2011, 09:06 PM -
Compare two strings
By roud9 in forum New To JavaReplies: 1Last Post: 11-04-2010, 11:57 PM -
Compare between 2 Strings
By ChaosINC in forum New To JavaReplies: 3Last Post: 01-17-2010, 11:39 AM -
Need help writing program to compare 2 strings using a loop
By kornwheat in forum New To JavaReplies: 15Last Post: 11-06-2009, 10:31 AM -
how to compare two strings
By elizabeth in forum New To JavaReplies: 7Last Post: 08-06-2007, 03:57 AM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks