Hi,

I'm an IT student taking up web site development. One of the courses we are taking is Java. Anyway, I have been trying to change two String arrays to Boolean's. I keep getting errors all the time, and am at my wits end! Could someone show me how to change them over???

Below is the entire application.

/**
 Project name: plane.java
 Author:       I.T.Hemmy
 Copyright:    HemmySoft 2007
 Description:
      This program uses an array to let the user select one of 4 seats in first
      or second class.

 Created on October 24, 2007, 2:03 PM
**/
 
package planeapp;

//import the JOptionPane into the program
import javax.swing.JOptionPane;

public class plane {
    public static void main(String[] args) {
        //make the variables to be used in the application.
        int whichClass = 0;
        int t = 0;
        int m = 0;
        
        //Create the First Class Array
        String class1Array[];
        class1Array = new String[4];
        
        //Put the desired data for each cell.
        class1Array[0] = "first";
        class1Array[1] = "first";
        class1Array[2] = "first";
        class1Array[3] = "first";

        
        //Create the Second Class Array
        String class2Array[]; 
        class2Array = new String[4];
        
        //Put the desired data for each cell.
        class2Array[0] = "second class";
        class2Array[1] = "second class";
        class2Array[2] = "second class"; 
        class2Array[3] = "second class";
              
        //Start the main while loop process to handle the Inputs of the Confirmation dialogues held within the loop.
        while( whichClass != 2 ){
            
            whichClass = JOptionPane.showConfirmDialog(null, "Click Yes first class. Click No for second class. Click Cancel to exit.");
                    
            switch(whichClass){
                
                //This case handles the "Yes" button, when pressed.
                case 0:
                    int first = t++;
                    //If all the first class seats are full, then say they are.
                    if(t > 4)
                    {
                        JOptionPane.showMessageDialog(null, "We're sorry, all the first class seats are taken. If you are interested, please try second class.");
                        JOptionPane.showMessageDialog(null, "Thank you, have a nice day!");                
                    }
                    //If any of the first class seats are empty, book a seat, and return to the main menu.
                    else 
                    {
                        JOptionPane.showMessageDialog(null, "You have successfully booked a seat in: " + class1Array[first]);
                    }
                    break;
               
                //This case handles the "No" button, when pressed.
                case 1:
                    int second = m++;
                    //If all second class seats are full, then tell the user they are.
                    if(m > 4)
                    {
                        JOptionPane.showMessageDialog(null, "We're sorry, all the second class seats are taken. If you are interested, please try first class.");
                        JOptionPane.showMessageDialog(null, "Thank you, have a nice day!");
                    }
                    //If any of the second class seats are empty, book a seat, and return to the main menu.
                    else {
                        JOptionPane.showMessageDialog(null, "You have successfully booked a seat in: " + class2Array[second]);
                    }
                    break;
                //If the "Cancel" button is clicked, this closes the program.
                case 2:
                    
                    JOptionPane.showMessageDialog(null, "Look behind you....");
                    JOptionPane.showMessageDialog(null, "BWA! Ha! Ha!!! You are done, have a nice day!");
                    break;
            } //ends switch.
            
        } //ends while.
        
    }// end method main
    
}//end class

Have a great day!!

Recommended Answers

All 4 Replies

Use code tags to preserve formatting of your code...
Don't use comments to tell yourself what a closing brace closes, that should be obvious from the indentation of the code.
Don't use comments to comment the blatantly obvious. In fact the only comments in your class that should remain are the ones in the class header comment block.
Observe the official Sun coding style guidelines when writing code. It's industry standard and makes it much easier to read.

Now to your question, you cannot cast an array of String to an array of boolean. They're unrelated types.
You'll have to go through each String in that String array, determine whether it represents true or false, and set the corresponding element in the boolean array accordingly.

I can't say I'm overly fond of Java. Our teacher requires commenting of everything! I need to CHANGE the String's all the Booleans. When I substitute the Strings to

Use code tags to preserve formatting of your code...
Don't use comments to tell yourself what a closing brace closes, that should be obvious from the indentation of the code.
Don't use comments to comment the blatantly obvious. In fact the only comments in your class that should remain are the ones in the class header comment block.
Observe the official Sun coding style guidelines when writing code. It's industry standard and makes it much easier to read.

Now to your question, you cannot cast an array of String to an array of boolean. They're unrelated types.
You'll have to go through each String in that String array, determine whether it represents true or false, and set the corresponding element in the boolean array accordingly.

if your teacher requires it you should do it. But also remember that if you ever do that kind of commenting when you write something as a professional you will get angry looks and likely told to strip it all out again.

Also, instead of using the numerical values here to evaluate the dialog result

//This case handles the "Yes" button, when pressed.
case 0:

use JOptionPane.YES_OPTION and JOptionPane.NO_OPTION. Those constants are there for a reason. You wouldn't need a comment to explain

case JOptionPane.YES_OPTION:
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.