Something about String.split("-"); problem

Reply

Join Date: May 2006
Posts: 14
Reputation: blurstudent is an unknown quantity at this point 
Solved Threads: 0
blurstudent's Avatar
blurstudent blurstudent is offline Offline
Newbie Poster

Something about String.split("-"); problem

 
0
  #1
May 26th, 2006
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:

  1. String validateSeatType() {
  2. String seat = jTextSeatNo.getText();
  3. String[] seatNo = seat.split("-");
  4.  
  5. // this is where the error occurs and I just can't figure out how to solve it
  6. for (int i = 0; i < seatNo.length; i++) {
  7. Integer.parseInt(seatNo[i]);
  8. if (jRBSeatBiz.isSelected() && (seatNo < 1 || seatNo > 6)) {
  9. return "- Business class seat no should be 1- 6\n";
  10. }
  11. else if (jRBSeatEcon.isSelected() && (seatNo < 7 || seatNo > 20)) {
  12. return "- Economy class seat no should be 7-20\n";
  13. }
  14. return ""; // no error
  15. }
  16. }

Any help is greatly appreciated. :o
I am who I am. People don't call me blur for nothing. :rolleyes:
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Something about String.split("-"); problem

 
0
  #2
May 26th, 2006
Erm can't you just compare it as a string instead of converting it to an integer:

For example

  1. if(seatNo.equals("7-20"))
  2. {
  3. print("you have chosen economy class")
  4. }

or something akin to this?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Something about String.split("-"); problem

 
0
  #3
May 26th, 2006
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?
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Something about String.split("-"); problem

 
0
  #4
May 26th, 2006
Oh I see what you mean, didn't read it properly.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 25
Reputation: DaSogo is an unknown quantity at this point 
Solved Threads: 2
DaSogo DaSogo is offline Offline
Light Poster

Re: Something about String.split("-"); problem

 
0
  #5
May 26th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 14
Reputation: blurstudent is an unknown quantity at this point 
Solved Threads: 0
blurstudent's Avatar
blurstudent blurstudent is offline Offline
Newbie Poster

Re: Something about String.split("-"); problem

 
0
  #6
May 27th, 2006
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:

  1. String validateSeatType() {
  2. String seat = jTextSeatNo.getText();
  3. String[] seatNo = seat.split("-");
  4.  
  5. // this is where the error occurs and I just can't figure out how to solve it
  6. for (int i = 0; i < seatNo.length; i++) {
  7. Integer.parseInt(seatNo[i]);
  8. if (jRBSeatBiz.isSelected() && (seatNo < 1 || seatNo > 6)) {
  9. return "- Business class seat no should be 1- 6\n";
  10. }
  11. else if (jRBSeatEcon.isSelected() && (seatNo < 7 || seatNo > 20)) {
  12. return "- Economy class seat no should be 7-20\n";
  13. }
  14. return ""; // no error
  15. }
  16. }

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:
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Something about String.split("-"); problem

 
0
  #7
May 27th, 2006
Just a quick one, not sure...

  1. 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:

  1. Integer.parseInt(seatNo[i]);

you're still comparing the actual String?


try this...
  1. //this is where the error occurs and I just can't figure out how to solve it
  2. for (int i = 0; i < seatNo.length; i++) {
  3. int k;
  4. k= Integer.parseInt(seatNo[i]);
  5. if (jRBSeatBiz.isSelected() && (k < 1 || k > 6)) {
  6. return "- Business class seat no should be 1- 6\n";
  7. }

I have not tested this so it may not work.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Something about String.split("-"); problem

 
0
  #8
May 27th, 2006
So this means that i cant use < or > operators to compare the INTEGERS in the STRING array!
You're trying to compare integers to strings, that's not going to work.
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 25
Reputation: DaSogo is an unknown quantity at this point 
Solved Threads: 2
DaSogo DaSogo is offline Offline
Light Poster

Re: Something about String.split("-"); problem

 
0
  #9
May 27th, 2006
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:
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Something about String.split("-"); problem

 
0
  #10
May 27th, 2006
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.
You're converting the string to an int, but then try to compare the string to your boundary conditions.
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC