Java programming question

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

Nevermind, everyone. I found my bugs. Mods, do whatever you wish with this thread.

Thanks.

I’m not sure about your GregorianCalendar problem. Have you tried adding the following?
include java.util.GregorianCalendar;

As for the if statements, your code is wrong. There should be brackets. They’re only optional if only one line follows the if or else if statement. For example:


if( labNum = 1 ) labInfo = "Lab #1: Sphere Problem";

if( labNum = 1 ) 
{
     labInfo = "Lab #1: Sphere Problem";
}

Are both valid. In your case, you have two statements, so the brackets are necessary.

This is more a question of style, but for what you’re doing, I’d use a switch statement:



switch(labNum)
{
     case 1:
          //Do stuff
          break;
     case 2:
          //Do stuff
          break;
     case 3:
          //Do stuff
          break;
     default:
          //Do stuff
          break;
}


Well, GregorianCalendar is a class that exists in the java.util package, but yes, I have tried importing java.util.GregorianCalendar. Still doesn’t work.

I like switch, now that you mention it. Think I’ll research that a bit more and adjust my code.

Thanks. :slight_smile:

Your real problem was using ‘=’ instead of ‘==’ for comparison, wasn’t it?

In addition, you need the curlies (’ { } ') for multiple-statement clauses within ‘if’ or ‘else’ blocks; and I find it good practice to use them for single-statement blocks as well (tomorrow, I or a co-worker may need to add a line. I don’t really trust her to remember to add the curlies if that happens, and I **certainly **don’t trust myself to do it!

**jovan **-- GregorianCalendar is covered by the “import java.util.*;”
Although in my experience, I’d usually write the lines as
Calendar cName = new GregorianCalendar(<whatever…>);

(unless you’re using a method that is specific to GregorianCalendar() and doesn’t exist on the Calendar interface.)

Eh, I can’t believe I missed that. I even c&p it into my “corrected” code.

mypost.Gaudere(true);

Yeah, I noticed I used “=” instead of “==” after I posted. Good looking out.
Adam