# Problem with if and else's - probably really simple?

Page 1 of 1

## 7 Replies - 485 Views - Last Post: 10 February 2013 - 02:42 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=311832&amp;s=b564a5d0c866073835ac7ad073888673&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 apeterson

Reputation: 0
• Posts: 4
• Joined: 10-February 13

# Problem with if and else's - probably really simple?

Posted 10 February 2013 - 01:21 PM

So i've got to make a program for my Basic Computer Science class where, when you input a number between 0-100 it gives you a letter grade based on a grade scale and if you enter a number outside that range it gives you an invalid number message.

I've gotten my code so that it can go through the grade scale and it runs fine, but when I try to put in the code I've written for my invalid number message, it either 1) only runs the invalid message no matter where I place it in the code 2) will run and put both the letter grade and then follow with the invalid number message or 3) tell me that my "letterGrade" variable may have not been initialized.

Here is the code I have for just the grades:

``` import java.util.Scanner;

public static void main(String[] args) {

Scanner scanInput;
scanInput = new Scanner ( System.in );

int userNumber;

System.out.print("Enter a number: ");
userNumber = scanInput.nextInt();

if ((userNumber >= 92)&&(userNumber <= 100)) {
} else if ((userNumber >= 80)&&(userNumber <= 91)) {
} else if ((userNumber >= 70)&&(userNumber <= 79)) {
} else if ((userNumber >= 60)&&(userNumber <= 69)) {
} else {
}

}
}

```

and here is the code I have written for the invalid number message:

```if ((userNumber <0)||(userNumber >100)) {
} else {
System.out.println("The number " +userNumber+ " is not valid. Only integers between 0 and 100 are valid.");
}
```

I don't know if I'm just putting them together in the wrong places or what but I can't seem to figure out how to get them to work. Like I said the user should put in a value 0-100 and it should pop out a letter grade, and if the value isn't in the 0-100 range it should give an error message. I can't seem to figure out how to get the parameters set up so that I can get the printed error message if the values are out of range and only the letter grade when it's in range. Any tips?

I originally had the code for the "F" grade as a parameter between 0 and 59, but it kept giving me the error about not having an initialized variable for "letterGrade."

Is This A Good Question/Topic? 0

## Replies To: Problem with if and else's - probably really simple?

### #2 baseball435

Reputation: 4
• Posts: 68
• Joined: 30-November 09

## Re: Problem with if and else's - probably really simple?

Posted 10 February 2013 - 01:32 PM

You could just do

```if (userNumber >= 0 && userNumber <= 100) {
if (userNumber >= 92)
else if (userNumber >= 80)
else if (userNumber >= 70)
else if (userNumber >= 60)
else
} else {
System.out.println("The number " +userNumber+ " is not valid. Only integers between 0 and 100 are valid.");
}

```

### #3 apeterson

Reputation: 0
• Posts: 4
• Joined: 10-February 13

## Re: Problem with if and else's - probably really simple?

Posted 10 February 2013 - 01:50 PM

baseball435, on 10 February 2013 - 01:32 PM, said:

You could just do

```if (userNumber >= 0 && userNumber <= 100) {
if (userNumber >= 92)
else if (userNumber >= 80)
else if (userNumber >= 70)
else if (userNumber >= 60)
else
} else {
System.out.println("The number " +userNumber+ " is not valid. Only integers between 0 and 100 are valid.");
}

```

It gives me an error on the line:
``` System.out.println("Grade: " +letterGrade);
```

saying that the variable is not initialized. Without that line, the code you supplied does work, but it has to print the grade/message.

Sorry I feel pretty silly dunno why I'm having such an issue.

### #4 baseball435

Reputation: 4
• Posts: 68
• Joined: 30-November 09

## Re: Problem with if and else's - probably really simple?

Posted 10 February 2013 - 02:02 PM

apeterson, on 10 February 2013 - 01:50 PM, said:

baseball435, on 10 February 2013 - 01:32 PM, said:

You could just do

```if (userNumber >= 0 && userNumber <= 100) {
if (userNumber >= 92)
else if (userNumber >= 80)
else if (userNumber >= 70)
else if (userNumber >= 60)
else
} else {
System.out.println("The number " +userNumber+ " is not valid. Only integers between 0 and 100 are valid.");
}

```

It gives me an error on the line:
``` System.out.println("Grade: " +letterGrade);
```

saying that the variable is not initialized. Without that line, the code you supplied does work, but it has to print the grade/message.

Sorry I feel pretty silly dunno why I'm having such an issue.

when you declare it just set it equal to 0 or something.

### #5 apeterson

Reputation: 0
• Posts: 4
• Joined: 10-February 13

## Re: Problem with if and else's - probably really simple?

Posted 10 February 2013 - 02:17 PM

letterGrade is a character though, not an int or any type of number? If i set letterGrade = '0' and enter, for example, -1, the program runs and prints: "The number -1 is invalid.
-1 is a

This post has been edited by macosxnerd101: 10 February 2013 - 02:24 PM
Reason for edit:: Removed quote. No need to quote the person above you.

### #6 Razakel

Reputation: 3
• Posts: 5
• Joined: 05-February 10

## Re: Problem with if and else's - probably really simple?

Posted 10 February 2013 - 02:27 PM

apeterson, on 10 February 2013 - 02:17 PM, said:

letterGrade is a character though, not an int or any type of number? If i set letterGrade = '0' and enter, for example, -1, the program runs and prints: "The number -1 is invalid.
-1 is a

Then set letterGrade to F, a default value, and then you just test for whether the user got a D, C, B or A through the if/else logic.

### #7 apeterson

Reputation: 0
• Posts: 4
• Joined: 10-February 13

## Re: Problem with if and else's - probably really simple?

Posted 10 February 2013 - 02:35 PM

Razakel, on 10 February 2013 - 02:27 PM, said:

apeterson, on 10 February 2013 - 02:17 PM, said:

letterGrade is a character though, not an int or any type of number? If i set letterGrade = '0' and enter, for example, -1, the program runs and prints: "The number -1 is invalid.
-1 is a

Then set letterGrade to F, a default value, and then you just test for whether the user got a D, C, B or A through the if/else logic.

Gives the printed message:

"
Enter a number: -1
The number -1 is invalid.
-1 is a F
"

Can't figure out how to keep it from saying "-1 is an F." It has to print the grade at the bottom, and it works perfectly for any number and grade that is within the range 0-100 but not for the invalid numbers.

### #8 andrewsw

Reputation: 5481
• Posts: 21,569
• Joined: 12-December 12

## Re: Problem with if and else's - probably really simple?

Posted 10 February 2013 - 02:42 PM

```char letterGrade = 'F'; // or
char letterGrade = '\0'; // the empty character literal
```

note, this requires apostrophes rather than quotes "".

This post has been edited by andrewsw: 10 February 2013 - 02:43 PM