Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If Else statements making System.out.print(); not work? 2

Status
Not open for further replies.

GT500FOMOCO

Programmer
Jul 5, 2001
143
US
Here is my code:

Code:
public class Letter2 {

	public static void main(String[] args) {
		ConsoleReader keyboard = new ConsoleReader(System.in);

		System.out.print("\n\nEnter a letter grade: ");
		String letterGrade = keyboard.readLine();
		String numericGrade = "";

		if (letterGrade.equalsIgnoreCase("a+")) {
			numericGrade = "\n\nThe numeric letter value is 4";
		}

		else {
			if (letterGrade.equalsIgnoreCase("a")) {
				numericGrade = "\n\nThe numeric letter value is 4";
			}

			else {

				if (letterGrade.equalsIgnoreCase("a-")) {
					numericGrade = "\n\nThe numeric letter value is 3.7";
				}

				else {

					if (letterGrade.equalsIgnoreCase("b+")) {
						numericGrade = "\n\nThe numeric letter value is 3.3";
					}

					else {

						if (letterGrade.equalsIgnoreCase("b")) {
							numericGrade = "\n\nThe numeric letter value is 3";
						}

						else {

							if (letterGrade.equalsIgnoreCase("b-")) {
								numericGrade = "\n\nThe numeric letter value is 2.7";
							}

							else {

								if (letterGrade.equalsIgnoreCase("c+")) {
									numericGrade = "\n\nThe numeric letter value is 2.3";
								}

								else {

									if (letterGrade.equalsIgnoreCase("c")) {
										numericGrade = "\n\nThe numeric letter value is 2";
									}

									else

										if (letterGrade.equalsIgnoreCase("c-")) {
											numericGrade = "\n\nThe numeric letter value is 1.7";
										}

										else {

											if (letterGrade.equalsIgnoreCase("d+")) {
												numericGrade = "\n\nThe numeric letter value is 1.3";
											}

											else {

												if (letterGrade.equalsIgnoreCase("d")) {
													numericGrade = "\n\nThe numeric letter value is 1";
												}

												else {

													if (letterGrade.equalsIgnoreCase("d-")) {
														numericGrade = "\n\nThe numeric letter value is 0.7";
													}

													else {

														if (letterGrade.equalsIgnoreCase("f")) {
															numericGrade = "\n\nThe numeric letter value is 0";
														}

														else {
															numericGrade = "Error: " + letterGrade + " is an illegal grade.";
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}

		System.out.print(numericGrade + ".\n\n\n");
	}
}

What is wrong? My compiler says:

C:\Lesson6\Letter2.java:101: Identifier expected.
System.out.print(numericGrade + ".\n\n\n");
^
1 error
"and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
Uneven number of closing brackets. One too many }. Move the System.out.print up above the third to last } and drop the last }.

You need to focus on formatting your code so that you don't run into these types of problems. A different technique would have worked much better here. Nested if statements are okay but this is Nested if HELL!
 
Yea, I know. Heck it is.

"and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
Thanks. And your right, it's If HELL. "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
As an alternative approach, how about the following:

import java.util.Hashtable;

Hashtable hList = new Hashtable();
hList.put( "A+", "\n\nThe numeric letter value is 4" );
hList.put( "A" , "\n\nThe numeric letter value is 4" );
hList.put( "A-", "\n\nThe numeric letter value is 3.7" );
etc...

numericGrade = (String)hList.get( letterGrade.toUpperCase() );

 
I have to use the If statements. :-( "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
um, this may just be my imagination, but i've been able to do what you're doing above without nesting the if statements, but rather chaining them. in cases like this, an 'else if' should really be considered as being on the same level as the original 'if'. here is what i mean:[tt]
if (...)
{
...
}
else if (...)
{
...
}
else if (...)
{
...
}
//etc.
[/tt]
and another formatting change you could make is removing the curly braces altogether. if an 'if' statement only has one line of code in it, you don't need braces around that code. example:[tt]
if (...)
do_something();
else if (...)
do_something_else();
[/tt] "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
I think I'll ask my teacher about that. :) "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top