I’m having problems with my code and hope someone can see what I’m missing. My code is as follows:
import java.io.IOException;
class Boat{
String boatName = (" ");
boolean sailUp = false;
}
public class Project2{
public static void main(String[] args){
System.out.println("\n");
Boat[] boatArray;
boatArray = new Boat[args.length];
for(int i = 0 ; i < args.length ; ++i){
boatArray[i] = new Boat();
}
for(int j = 0 ; j < args.length ; ++j){
boatArray[j].boatName = args[j];
}
for(int k = 0 ; k < args.length ; ++k){
String firstLetter = boatArray[k].boatName.substring(0, 1);
if(firstLetter == ("B")){
boatArray[k].sailUp = true;
}else if(firstLetter == ("C")){
boatArray[k].sailUp = true;
}else if(firstLetter == ("N")){
boatArray[k].sailUp = true;
}else{
boatArray[k].sailUp = false;
}
}
for(int l = 0 ; l < args.length ; ++l){
System.out.println("\nThe " + boatArray[l].boatName + " is ready to sail...");
if(boatArray[l].sailUp == false){
System.out.println("\n\tbut the sail is down, raise the sail!");
}else if(boatArray[l].sailUp == true){
System.out.println("\n\tthe sail is up, ahead full!");
}
}
}
}
I want the following output:
C:\Documents and Settings>java Project2 Enterprise Challenger Discovery Nimitz
The Enterprise is ready to sail…
but the sail is down, raise the sail!
The Challenger is ready to sail…
the sail is up, ahead full!
The Discovery is ready to sail…
but the sail is down, raise the sail!
The Nimitz is ready to sail…
the sail is up, ahead full!
But I get this:
C:\Documents and Settings>java Project2 Enterprise Challenger Discovery Nimitz
The Enterprise is ready to sail…
but the sail is down, raise the sail!
The Challenger is ready to sail…
but the sail is down, raise the sail!
The Discovery is ready to sail…
but the sail is down, raise the sail!
The Nimitz is ready to sail…
but the sail is down, raise the sail!
Why isn’t the sail state being reset by the third loop?
Change
to
etc
==checks for reference equality, where as.equalswill check value equality