# Java programming question

**URL:** <https://boards.straightdope.com/t/java-programming-question/406800>\
**Category:** Factual Questions\
**Created:** [June 5, 2007, 5:26am UTC](https://boards.straightdope.com/t/java-programming-question/406800 "2007-06-05T05:26:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Agent\_Foxtrot](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@Agent\_Foxtrot](https://boards.straightdope.com/u/Agent_Foxtrot)\
**Post date:** [June 5, 2007, 5:26am UTC](https://boards.straightdope.com/t/java-programming-question/406800/1 "2007-06-05T05:26:20Z")

</div>

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:

```auto

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:

```auto

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

---

<div class="post-metadata">

**Author:** ![Agent\_Foxtrot](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@Agent\_Foxtrot](https://boards.straightdope.com/u/Agent_Foxtrot)\
**Post date:** [June 5, 2007, 5:42am UTC](https://boards.straightdope.com/t/java-programming-question/406800/2 "2007-06-05T05:42:43Z")

</div>

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

Thanks.

---

<div class="post-metadata">

**Author:** ![jovan](https://avatars.discourse-cdn.com/v4/letter/j/0ea827/32.png) [@jovan](https://boards.straightdope.com/u/jovan)\
**Post date:** [June 5, 2007, 5:45am UTC](https://boards.straightdope.com/t/java-programming-question/406800/3 "2007-06-05T05:45:25Z")

</div>

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:

```auto

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:

```auto

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

```

---

<div class="post-metadata">

**Author:** ![Agent\_Foxtrot](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@Agent\_Foxtrot](https://boards.straightdope.com/u/Agent_Foxtrot)\
**Post date:** [June 5, 2007, 5:51am UTC](https://boards.straightdope.com/t/java-programming-question/406800/4 "2007-06-05T05:51:52Z")

</div>

[QUOTE=jovan]  
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:

```auto

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:

```auto

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

```

[/QUOTE]

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. 🙂

---

<div class="post-metadata">

**Author:** ![Noone\_Special](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/noone_special/32/2863_2.png) [@Noone\_Special](https://boards.straightdope.com/u/Noone_Special)\
**Post date:** [June 5, 2007, 5:54am UTC](https://boards.straightdope.com/t/java-programming-question/406800/5 "2007-06-05T05:54:11Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![jovan](https://avatars.discourse-cdn.com/v4/letter/j/0ea827/32.png) [@jovan](https://boards.straightdope.com/u/jovan)\
**Post date:** [June 5, 2007, 6:22am UTC](https://boards.straightdope.com/t/java-programming-question/406800/6 "2007-06-05T06:22:14Z")

</div>

[QUOTE=Noone Special]  
Your **real** problem was using ‘=’ instead of ‘==’ for comparison, wasn’t it?  
[/QUOTE]

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

mypost.Gaudere(true);

---

<div class="post-metadata">

**Author:** ![Agent\_Foxtrot](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@Agent\_Foxtrot](https://boards.straightdope.com/u/Agent_Foxtrot)\
**Post date:** [June 5, 2007, 6:33am UTC](https://boards.straightdope.com/t/java-programming-question/406800/7 "2007-06-05T06:33:17Z")

</div>

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