For my Java programming class, I have a lab due every three days. Instead of writing the header information for each lab over and over again, I decided to write a class that asks me for the lab number and puts the appropriate information in for me. I feel like my code is right, but the compiler is giving me some strange errors. If someone would take a look and let me know what’s wrong, I’d appreciate it.
Disclaimer: This is not an assignment and this code is completely original. This is something I’m doing for my own convience, interest in computers, and overconsumption of caffeine.
My code:
import java.util.*;
import javax.swing.JOptionPane;
public class headerInfo
{
public static void main( String [] args )
{
byte labNum = JOptionPane.showInputDialog( null, "Lab number:" );
String labInfo;
if( labNum = 1 ) // Asks user for lab number; stores dueDate with
// approprate date and lab info
GregorianCalendar dueDate = new GregorianCalendar( 2007, 5, 29 );
labInfo = "Lab #1: Sphere Problem";
else if( labNum = 2 )
GregorianCalendar dueDate = new GregorianCalendar( 2007, 6, 3 );
labInfo = "Lab #2: Pre-Defined Classes";
else if( labNum = 3 )
GregorianCalendar dueDate = new GregorianCalendar( 2007, 6, 7 );
labInfo = "Lab #3: Functions/Methods with No Parameters";
else if( labNum = 4 )
GregorianCalendar dueDate = new GregorianCalendar( 2007, 6, 10 );
labInfo = "Lab #4: Function/Methods WITH Parameters";
else if( labNum = 5 )
GregorianCalendar dueDate = new GregorianCalendar( 2007, 6, 13 );
labInfo = "Lab #5: For/While and If/Else Loops";
else if( labNum = 6 )
GregorianCalendar dueDate = new GregorianCalendar( 2007, 6, 26 );
labInfo = "User-Defined Classes";
else if( labNum = 7 )
GregorianCalendar dueDate = new GregorianCalendar( 2007, 7, 2 );
labInfo = "GUI: Buttons, Fields, Layout Manager";
else
JOptionPane.showMessageDialog( null, "Lab does not exist." );
return;
System.out.println( "Programmer: Agent Foxtrot" );
System.out.println( "Class: CSI-161/Section: 875" );
System.out.println( labInfo );
System.out.println( "Due Date: " + dueDate.get( Calendar.MONTH )
+ "/" + dueDate.get( Calendar.DAY_OF_MONTH )
+ "/" + dueDate.get( Calendar.YEAR ) );
}
}
And the compiler errors:
headerInfo.java:13: not a statement
GregorianCalendar dueDate = new GregorianCalendar( 2007, 5, 29 );
^
headerInfo.java:13: ';' expected
GregorianCalendar dueDate = new GregorianCalendar( 2007, 5, 29 );
^
headerInfo.java:16: 'else' without 'if'
else if( labNum = 2 )
^
headerInfo.java:20: 'else' without 'if'
else if( labNum = 3 )
^
headerInfo.java:24: 'else' without 'if'
else if( labNum = 4 )
^
headerInfo.java:28: 'else' without 'if'
else if( labNum = 5 )
^
headerInfo.java:32: 'else' without 'if'
else if( labNum = 6 )
^
headerInfo.java:36: 'else' without 'if'
else if( labNum = 7 )
^
headerInfo.java:40: 'else' without 'if'
else
^
9 errors
My textbook has the if, else if, and else statements in curly { } brackets in one example, and without the curly { } brackets in another, but doesn’t explain if they’re necessary or not. Why does it protest when I call the first instance of GregorianCalendar, but not the rest? I am using jGRASP.
Thanks for any help given.
Adam