The Artima Developer Community
Sponsored Link

Java Answers Forum
String to char... or another feasable solution

3 replies on 1 page. Most recent reply: Mar 19, 2002 6:52 PM by Eduardo

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Eduardo

Posts: 21
Nickname: eduardo
Registered: Feb, 2002

String to char... or another feasable solution Posted: Mar 19, 2002 5:36 AM
Reply to this message Reply
Advertisement
I was working on a GUI version of a rapid prototype and created some dialog boxes to get some data required. So, I ended up using String, instead of char, as I thought it would be easy to convert it... but I got stuck in this part:

private String mealType;
public String getMealType () { return mealType; }
public void setMealType (String m) { mealType = m; }

String inputMealTemp = JOptionPane.showInputDialog(
"Type the character for the SPECIAL MEAL \nassociated with this reservation:\n"+
"A. "+mealTypeValues[0]+'\n'+
"B. "+mealTypeValues[1]+'\n'+
"C. "+mealTypeValues[2]+'\n'+
"D. "+mealTypeValues[3]+'\n'+
"E. "+mealTypeValues[4]+'\n'+
"F. "+mealTypeValues[5]+'\n'+
"G. "+mealTypeValues[6]+'\n'+
"H. "+mealTypeValues[7]+'\n'+
"I. "+mealTypeValues[8]+'\n'+
"J. "+mealTypeValues[9]+'\n'+
"K. "+mealTypeValues[10]+'\n'+
"L. "+mealTypeValues[11]+'\n'+
"M. "+mealTypeValues[12]+'\n');

AirGourmetData.fltRecs[AirGourmetData.fltRecCount].setMealType(
inputMealTemp);

\\up to here is fine... the problem is below:

if ((AirGourmetData.fltRecs.getMealLoaded () == true)
&& (AirGourmetData.fltRecs.getMealType ().equals("J"))
&& ((AirGourmetData.fltRecs.getFlightDate ().after (fromDate))
|| (AirGourmetData.fltRecs.getFlightDate ().equals (fromDate)))
&& ((AirGourmetData.fltRecs.getFlightDate ().before (toDate))
|| (AirGourmetData.fltRecs.getFlightDate ().equals (toDate))) )


JOptionPane.showMessageDialog(GetReservation.jaddress,
"PASSENGER: "
+ AirGourmetData.fltRecs.getPassengerID ()+'\n'+
" SEAT: " + AirGourmetData.fltRecs.getSeatNum ()+'\n'+
" MEAL TYPE: " + CFlightRecord.mealTypeValues[AirGourmetData.fltRecs.getMealType ()- 'A']
);

Well... in those lines where it uses getMealType(), it really doesn't work. Does anybody have an idea about the best solution for that?

Thank you!


Eduardo

Posts: 21
Nickname: eduardo
Registered: Feb, 2002

Ah, ha... Posted: Mar 19, 2002 11:46 AM
Reply to this message Reply
Looks like it was a difficult question! Nobody replied so far! LOL LOL

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Ah, ha... Posted: Mar 19, 2002 6:45 PM
Reply to this message Reply
I don't think the question in your subject is difficult:
    static char stringToChar( String s )
    {
        if( s.length() != 1 )
            throw new IllegalArgumentException("Only one-character strings, if you please!");
 
        return s.charAt(0);
    }

But I must admit the code you posted is a bit bewildering. Why are you not using radio buttons or checkboxes for entry? Also, even if you have free-form entry, why do you hard-code all the letters and indexes in the array? A simple loop would give you much cleaner code. You may also want to consider using a case-insensitive comparison, too.

Eduardo

Posts: 21
Nickname: eduardo
Registered: Feb, 2002

Yes, you are right... Posted: Mar 19, 2002 6:52 PM
Reply to this message Reply
Thank you Matt!

Actually, I've tried to use radio buttons first, but I don't know why it was not working properly. Instead of freezing the panel to allow me to check one button, it was just passing to the next part of the code right away. As this is a rapid prototype and the important is the functionality, I just hardcoded. But then I got stuck somewhere else, of course (this is what lack of experience does! LOL).

Flat View: This topic has 3 replies on 1 page
Topic: urgent help needed please Previous Topic   Next Topic Topic: how to read write update a xml file.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use