| | |
Something about String.split("-"); problem
![]() |
Ok folks, sorry to keep asking questions in this forum, but i am just tidying up the nitty gritty issues in my program and i encountered many problems. :cry:
my issue here is i can allow user to key in seat number for this booking program. For example, if there are 2 person, then they have to key in 1-2. Different set of numbers belong to different type of seats. 1-5 is business class, 6-20 is economy class.
validation is already done for those keying in only one person (e.g: seat no 1). Now I want to validate the seat numbers that they keyed in (e.g: 1-2, when they select economy class show return an error) belongs to the correct type of seat.
Below is my code:
Any help is greatly appreciated. :o
my issue here is i can allow user to key in seat number for this booking program. For example, if there are 2 person, then they have to key in 1-2. Different set of numbers belong to different type of seats. 1-5 is business class, 6-20 is economy class.
validation is already done for those keying in only one person (e.g: seat no 1). Now I want to validate the seat numbers that they keyed in (e.g: 1-2, when they select economy class show return an error) belongs to the correct type of seat.
Below is my code:
Java Syntax (Toggle Plain Text)
String validateSeatType() { String seat = jTextSeatNo.getText(); String[] seatNo = seat.split("-"); // this is where the error occurs and I just can't figure out how to solve it for (int i = 0; i < seatNo.length; i++) { Integer.parseInt(seatNo[i]); if (jRBSeatBiz.isSelected() && (seatNo < 1 || seatNo > 6)) { return "- Business class seat no should be 1- 6\n"; } else if (jRBSeatEcon.isSelected() && (seatNo < 7 || seatNo > 20)) { return "- Economy class seat no should be 7-20\n"; } return ""; // no error } }
Any help is greatly appreciated. :o
I am who I am. People don't call me blur for nothing. :rolleyes:
Erm can't you just compare it as a string instead of converting it to an integer:
For example
or something akin to this?
For example
Java Syntax (Toggle Plain Text)
if(seatNo.equals("7-20")) { print("you have chosen economy class") }
or something akin to this?
*Voted best profile in the world*
That won't work, iamthwee, because you're comparing only the entire range and not subranges thereof.
Think about what you're actually doing here. What's happening with the result of Integer.parseInt()?
What are you comparing with those hardcoded limits?
Think about what you're actually doing here. What's happening with the result of Integer.parseInt()?
What are you comparing with those hardcoded limits?
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
•
•
Join Date: May 2006
Posts: 25
Reputation:
Solved Threads: 2
blurstudent, What type of error are you receiving? Is it a logic or syntax error. Posting that may expedite the solution! I never knew the string class to have a method named [ split(); ] I'm trying best to interpret the code. Looks to me that you are comparing the bit pattern of an array reference to an int [ seatno < 1 ]. You will need to add this: [ seatno[i] < 1 ]...etc...
Even if you do add what I suggested, now it looks like you are comparing a String object to an int [ seatno[i] < 1 ]. I believe this may generate a compile error along the lines of: I have a String and was looking for another String, but I found a primitive int instead...what gives!' The reason: Looks like you tried to parse a string to and int value [ Inter.parseInt(seatNo[i]); ] but you didn't assign it to an int variable that should be used in a boolean comparison expression. The value is lost, I think.
Even if you do add what I suggested, now it looks like you are comparing a String object to an int [ seatno[i] < 1 ]. I believe this may generate a compile error along the lines of: I have a String and was looking for another String, but I found a primitive int instead...what gives!' The reason: Looks like you tried to parse a string to and int value [ Inter.parseInt(seatNo[i]); ] but you didn't assign it to an int variable that should be used in a boolean comparison expression. The value is lost, I think.
Oh welll... Guess I need to learn how to make myself clearer. :cheesy:
The main objective of this function that I coded earlier is to make sure that the seat numbers that the user keyed in belongs to the correct type that they selected.
1-6 --> Business Class
7-20 --> Economy Class
An example:
So if they select business class, the "boundary" of seat numbers will be 1,2,3,4,5 or 6. If there are more than 1 person, then they will input in, for instance, 1-3.
To validate that, I need to split this string "1-3" by the delimiter "-" right? Then after that, I need to verify that 1 and 3 is smaller than 6, so that they are rightfully in the Business class. Otherwise, an error will be shown.
By splitting it, I need to store the values in an array. These values are in STRING format. To compare that they are smaller than 6, I need parse them into INTEGER right? The logic seems right to me! But apparently, I have the following errors:
Operator < cannot be applied to (java.lang.String[], int)
Operator > cannot be applied to (java.lang.String[], int)
So this means that i cant use < or > operators to compare the INTEGERS in the STRING array!
Erm... I don't know if you guys know what I am talking about, but I hope this lets you have a better understanding.
I append the coding here again:
I tried to do a System.out.println(seatNo[i]) before the comment part and it prints out
1
2
when I entered in 1-2 in the seat number, so this means the split works, but i have issues with validating the seat type. :mad:
The main objective of this function that I coded earlier is to make sure that the seat numbers that the user keyed in belongs to the correct type that they selected.
1-6 --> Business Class
7-20 --> Economy Class
An example:
So if they select business class, the "boundary" of seat numbers will be 1,2,3,4,5 or 6. If there are more than 1 person, then they will input in, for instance, 1-3.
To validate that, I need to split this string "1-3" by the delimiter "-" right? Then after that, I need to verify that 1 and 3 is smaller than 6, so that they are rightfully in the Business class. Otherwise, an error will be shown.
By splitting it, I need to store the values in an array. These values are in STRING format. To compare that they are smaller than 6, I need parse them into INTEGER right? The logic seems right to me! But apparently, I have the following errors:
Operator < cannot be applied to (java.lang.String[], int)
Operator > cannot be applied to (java.lang.String[], int)
So this means that i cant use < or > operators to compare the INTEGERS in the STRING array!
Erm... I don't know if you guys know what I am talking about, but I hope this lets you have a better understanding.
I append the coding here again:
Java Syntax (Toggle Plain Text)
String validateSeatType() { String seat = jTextSeatNo.getText(); String[] seatNo = seat.split("-"); // this is where the error occurs and I just can't figure out how to solve it for (int i = 0; i < seatNo.length; i++) { Integer.parseInt(seatNo[i]); if (jRBSeatBiz.isSelected() && (seatNo < 1 || seatNo > 6)) { return "- Business class seat no should be 1- 6\n"; } else if (jRBSeatEcon.isSelected() && (seatNo < 7 || seatNo > 20)) { return "- Economy class seat no should be 7-20\n"; } return ""; // no error } }
I tried to do a System.out.println(seatNo[i]) before the comment part and it prints out
1
2
when I entered in 1-2 in the seat number, so this means the split works, but i have issues with validating the seat type. :mad:
I am who I am. People don't call me blur for nothing. :rolleyes:
Just a quick one, not sure...
Would tell me you're trying to compare a string against an int.
Can't do that. So make sure this -> java.lang.String[] is actually an integer.
Although you've converted in to an integer here:
you're still comparing the actual String?
try this...
I have not tested this so it may not work.
Java Syntax (Toggle Plain Text)
Operator < cannot be applied to (java.lang.String[], int)
Would tell me you're trying to compare a string against an int.
Can't do that. So make sure this -> java.lang.String[] is actually an integer.
Although you've converted in to an integer here:
Java Syntax (Toggle Plain Text)
Integer.parseInt(seatNo[i]);
you're still comparing the actual String?
try this...
Java Syntax (Toggle Plain Text)
//this is where the error occurs and I just can't figure out how to solve it for (int i = 0; i < seatNo.length; i++) { int k; k= Integer.parseInt(seatNo[i]); if (jRBSeatBiz.isSelected() && (k < 1 || k > 6)) { return "- Business class seat no should be 1- 6\n"; }
I have not tested this so it may not work.
*Voted best profile in the world*
•
•
•
•
So this means that i cant use < or > operators to compare the INTEGERS in the STRING array!
An integer isn't a string after all, and the compilers tells you just that.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
•
•
Join Date: May 2006
Posts: 25
Reputation:
Solved Threads: 2
IAMTHWEE has written out what I explained in my last post. Take away with you more than just the solution. Try to really understand what is happening in your code. I'll try to point out key parts to help you.
Operator < cannot be applied to (java.lang.String[], int)
Operator > cannot be applied to (java.lang.String[], int)
A string object can't be compared to an int value
if("blurstudent" > 18)
So this means that i cant use < or > operators to compare the INTEGERS in the STRING array!
Interger is a wrapper class and int is a primitive value. Arrays can only store values of its declared type.
[ String[] seatNo = "only string values for string objects allowed" ]
You do not have int values stored in [ seatNo[i]); ] because you have not assigned the parsed int to an int variable.
[ int k = parsed int value ], therefore the parsed value is lost and destroyed once execution leaves the invoked method validateSeatType().
I tried to do a System.out.println(seatNo[i]) before the comment part and it prints out
1
2
1 and 2 are strings in the String array seatNo[i], they will print to the screen. However, if you try to perform numerical operations on them, you'll get errors.
when I entered in 1-2 in the seat number, so this means the split works, but i have issues with validating the seat type.
Since you are using strings, you could use the compareTo() method or the equals() method. But never, never use == to compare strings.
Personally, I would find away to capture int values from user keyed events. You can control what they input (select), just make them! What year in school are you?:lol:
Operator < cannot be applied to (java.lang.String[], int)
Operator > cannot be applied to (java.lang.String[], int)
A string object can't be compared to an int value
if("blurstudent" > 18)
So this means that i cant use < or > operators to compare the INTEGERS in the STRING array!
Interger is a wrapper class and int is a primitive value. Arrays can only store values of its declared type.
[ String[] seatNo = "only string values for string objects allowed" ]
You do not have int values stored in [ seatNo[i]); ] because you have not assigned the parsed int to an int variable.
[ int k = parsed int value ], therefore the parsed value is lost and destroyed once execution leaves the invoked method validateSeatType().
I tried to do a System.out.println(seatNo[i]) before the comment part and it prints out
1
2
1 and 2 are strings in the String array seatNo[i], they will print to the screen. However, if you try to perform numerical operations on them, you'll get errors.
when I entered in 1-2 in the seat number, so this means the split works, but i have issues with validating the seat type.
Since you are using strings, you could use the compareTo() method or the equals() method. But never, never use == to compare strings.
Personally, I would find away to capture int values from user keyed events. You can control what they input (select), just make them! What year in school are you?:lol:
•
•
•
•
I tried to do a System.out.println(seatNo[i]) before the comment part and it prints out
1
2
when I entered in 1-2 in the seat number, so this means the split works, but i have issues with validating the seat type.
Why? Aren't you missing something there?
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: javac doesn't work
- Next Thread: 2nd shot please help guys
| Thread Tools | Search this Thread |
6 @param actuate android api applet application arc array arrays automation balls binary bluetooth bold business byte c++ chat class client code codesnippet collections compare component coordinates database defaultmethod detection doctype dragging ebook eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework hql html ide ideas image ingres input integer internet intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list map method methods mobile mysql netbeans newbie nextline parameter php pong problem program programming project recursion recursive scanner sell server set sms sort sql string sun swing swt terminal threads tree web websites windows






