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:

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

Recommended Answers

All 11 Replies

Member Avatar for iamthwee

Erm can't you just compare it as a string instead of converting it to an integer:

For example

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

or something akin to this?

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?

Member Avatar for iamthwee

Oh I see what you mean, didn't read it properly.

Member Avatar for DaSogo

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 < 1 ]...etc...
Even if you do add what I suggested, now it looks like you are comparing a String object to an int [ seatno < 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); ] 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:

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) 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:

Member Avatar for iamthwee

Just a quick one, not sure...

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:

Integer.parseInt(seatNo[i]);

you're still comparing the actual String?


try this...

//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.

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.

Member Avatar for DaSogo

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); ] 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) before the comment part and it prints out
1
2

1 and 2 are strings in the String array seatNo, 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) 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?

stupid me. just another line and it works!

int k= Integer.parseInt(seatNo);

like what IAMTHWEE said " Although you've converted seatNo to an integer I am still comparing the actual String!!!"

I missed out that part!
omg... i'm such a dork.

haha... actually i know these are basics of java... but I had clean forgotten about them! me in private uni yr 2.

thanks manz! I understand why my coding couldnt work! Yippee!! Thanks all!!!!!!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.