Page 1 of 1

## 10 Replies - 1191 Views - Last Post: 19 April 2009 - 10:18 AMRate 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=98832&amp;s=be98fa34ca536aa14d6637682e016709&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mauribarri

Reputation: 0
• Posts: 8
• Joined: 12-April 09

Posted 12 April 2009 - 08:26 PM

I am working on a grading program

I must Utilize the Math class and round the number to zero decimal places, then use the int cast method to convert to an integer.(ensure you round it before type casting it)
Utilize a switch statement to determine the corresponding letter grade. Use the grading criteria from this course for the numbers. (ensure you accept all allowable numbers i.e. 70-79 and all in between)
Output the corresponding letter grade along with the initial number entered in decimal format utilizing the printf method and format specifiers. Output the number in only 3 decimal places.

in my switch
I must Get input for the grade letter in String format.
Utilize a switch statement to determine the corresponding output for the letter entered. (ensure you accept both upper and lower case letters.
Use the grading criteria from this course for the numbers. (you must use charAt( ) so the string will work with your switch)
Output the corresponding numbers values for the letter entered along with the letter initially entered.

Questions I have:
Is my code in the right direction?
Line 15 is giving me problems what I can I do to correct this but still be able create my rounding to the nearest whole number?
Am I missing anything?

```public class UribarriMiguelProg4
{
public static void main(String[] args)
{

Scanner stdIn = new Scanner (System.in);

System.out.print("Will you be using numbers or letters?");

{
System.out.println("Enter a number grade in double format. (Ex. 74.25463)");

else
else
else
else

break;
}

{
letters = stdIn.nextLine ();

{
case 'A':
System.out.println("90-100 = A");
break;
case 'B':
System.out.println("80-89.9 = B");
break;
case 'C':
System.out.println("70-79.9 = C");
break;
case 'D':
System.out.println("60-69.9 = D");
break;
case 'F':
System.out.println("< 60 = F");
break;
default:
break;
}
}
}
}

```

Is This A Good Question/Topic? 0

### #2 krizjaz

Reputation: 20
• Posts: 98
• Joined: 07-October 07

Posted 13 April 2009 - 12:43 AM

mauribarri, on 12 Apr, 2009 - 07:26 PM, said:

I am working on a grading program

I must Utilize the Math class and round the number to zero decimal places, then use the int cast method to convert to an integer.(ensure you round it before type casting it)
Utilize a switch statement to determine the corresponding letter grade. Use the grading criteria from this course for the numbers. (ensure you accept all allowable numbers i.e. 70-79 and all in between)
Output the corresponding letter grade along with the initial number entered in decimal format utilizing the printf method and format specifiers. Output the number in only 3 decimal places.

in my switch
I must Get input for the grade letter in String format.
Utilize a switch statement to determine the corresponding output for the letter entered. (ensure you accept both upper and lower case letters.
Use the grading criteria from this course for the numbers. (you must use charAt( ) so the string will work with your switch)
Output the corresponding numbers values for the letter entered along with the letter initially entered.

Questions I have:
Is my code in the right direction?
Line 15 is giving me problems what I can I do to correct this but still be able create my rounding to the nearest whole number?
Am I missing anything?

```public class UribarriMiguelProg4
{
public static void main(String[] args)
{

Scanner stdIn = new Scanner (System.in);

System.out.print("Will you be using numbers or letters?");

{
System.out.println("Enter a number grade in double format. (Ex. 74.25463)");

else
else
else
else

break;
}

{
letters = stdIn.nextLine ();

{
case 'A':
System.out.println("90-100 = A");
break;
case 'B':
System.out.println("80-89.9 = B");
break;
case 'C':
System.out.println("70-79.9 = C");
break;
case 'D':
System.out.println("60-69.9 = D");
break;
case 'F':
System.out.println("< 60 = F");
break;
default:
break;
}
}
}
}

```

This post has been edited by krizjaz: 13 April 2009 - 12:56 AM

### #3 krizjaz

Reputation: 20
• Posts: 98
• Joined: 07-October 07

Posted 13 April 2009 - 12:54 AM

```import java.util.*;

public class UribarriMiguelProg4
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner (System.in);

System.out.print("Will you be using numbers or letters?");

{
System.out.println("Enter a number grade in double format. (Ex. 74.25463)");
numericGrade = Double.parseDouble(stdIn.next());//original double (with decimal).
numericFinalGrade = Integer.parseInt("" + Math.round(numericGrade));// the round method is used from the math class.

else
else
else
else

break;
}

{

{
case 'A':
System.out.println("90-100 = A");
break;
case 'B':
System.out.println("80-89.9 = B");
break;
case 'C':
System.out.println("70-79.9 = C");
break;
case 'D':
System.out.println("60-69.9 = D");
break;
case 'F':
System.out.println("< 60 = F");
break;
default:
break;
}
}
}
//public static RoundingMode valueOf(numericGrade); i think. we won't be needing this.
}

```

I think this would be the code. The "letter input" side (while(grade == 'Y')), the loop does'nt end. Edit it if you wish. This is still your code.

This post has been edited by krizjaz: 13 April 2009 - 01:20 AM

### #4 mauribarri

Reputation: 0
• Posts: 8
• Joined: 12-April 09

Posted 14 April 2009 - 06:43 PM

krizjaz, on 12 Apr, 2009 - 11:54 PM, said:

```import java.util.*;

public class UribarriMiguelProg4
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner (System.in);

System.out.print("Will you be using numbers or letters?");

{
System.out.println("Enter a number grade in double format. (Ex. 74.25463)");
numericGrade = Double.parseDouble(stdIn.next());//original double (with decimal).
numericFinalGrade = Integer.parseInt("" + Math.round(numericGrade));// the round method is used from the math class.

else
else
else
else

break;
}

{

{
case 'A':
System.out.println("90-100 = A");
break;
case 'B':
System.out.println("80-89.9 = B");
break;
case 'C':
System.out.println("70-79.9 = C");
break;
case 'D':
System.out.println("60-69.9 = D");
break;
case 'F':
System.out.println("< 60 = F");
break;
default:
break;
}
}
}
//public static RoundingMode valueOf(numericGrade); i think. we won't be needing this.
}

```

I think this would be the code. The "letter input" side (while(grade == 'Y')), the loop does'nt end. Edit it if you wish. This is still your code.

krizjaz,
Thanks for the help. on the while 'y' statement doesn't the break at the end stop the loop?

### #5 krizjaz

Reputation: 20
• Posts: 98
• Joined: 07-October 07

Posted 16 April 2009 - 12:00 AM

No, it doesn't stop the loop. The break there is for ending the Switch statement. So, You should end the while statement if the user wants to end it. Here's the code:
```while (grade == 'Y')

{

{
case 'A':
System.out.println("90-100 = A\nWant to try again? Y to start again or any key to end:");
break;
case 'B':
System.out.println("80-89.9 = B\nWant to try again? Y to start again or any key to end:");
break;
case 'C':
System.out.println("70-79.9 = C\nWant to try again? Y to start again or any key to end:");
break;
case 'D':
System.out.println("60-69.9 = D\nWant to try again? Y to start again or any key to end:");
break;
case 'F':
System.out.println("< 60 = F\nWant to try again? Y to start again or any key to end:");
break;
default:
System.out.println("Entered An Incorrect Grade:\nWant to try again? Y to start again or any key to end:");
break;
}
}

```

I still used the character variable which is grade. So, after every case, the computer will ask the user if he/she wants to try again. So, there will be an input. The value of the grade variable is then changed so that after the switch statement, if the user inputs a 'Y', the condition in the while statement will still be true and the while loop will execute but if the user inputs any key, the condition in the while statement will now be false because it is != 'Y' and the while loop will not execute.

This post has been edited by krizjaz: 16 April 2009 - 12:08 AM

### #6 mauribarri

Reputation: 0
• Posts: 8
• Joined: 12-April 09

Posted 16 April 2009 - 06:37 PM

krizjaz, on 15 Apr, 2009 - 11:00 PM, said:

No, it doesn't stop the loop. The break there is for ending the Switch statement. So, You should end the while statement if the user wants to end it. Here's the code:
```while (grade == 'Y')

{

{
case 'A':
System.out.println("90-100 = A\nWant to try again? Y to start again or any key to end:");
break;
case 'B':
System.out.println("80-89.9 = B\nWant to try again? Y to start again or any key to end:");
break;
case 'C':
System.out.println("70-79.9 = C\nWant to try again? Y to start again or any key to end:");
break;
case 'D':
System.out.println("60-69.9 = D\nWant to try again? Y to start again or any key to end:");
break;
case 'F':
System.out.println("< 60 = F\nWant to try again? Y to start again or any key to end:");
break;
default:
System.out.println("Entered An Incorrect Grade:\nWant to try again? Y to start again or any key to end:");
break;
}
}

```

I still used the character variable which is grade. So, after every case, the computer will ask the user if he/she wants to try again. So, there will be an input. The value of the grade variable is then changed so that after the switch statement, if the user inputs a 'Y', the condition in the while statement will still be true and the while loop will execute but if the user inputs any key, the condition in the while statement will now be false because it is != 'Y' and the while loop will not execute.

Krizjaz,
Thanks for the info. This makes more sense now. so this is where logical operators are used and are crucial right(&& != ||)?

### #7 mauribarri

Reputation: 0
• Posts: 8
• Joined: 12-April 09

Posted 16 April 2009 - 07:12 PM

Krizjaz,
Hey as I made these changes when i compiled the java file I have 1 error. I tried looking to see what was wrong. I couldn't find it. maybe I'm just blind. Thanks for the help
[
Scanner stdIn = new Scanner (System.in);

System.out.print("Will you be using numbers or letters?");

{
System.out.println("Enter a number grade in double format. (Ex. 74.25463): ");
numericGrade = Double.parseDouble(stdIn.next());//original double (with decimal).
numericFinalGrade = Integer.parseInt("" + Math.round(numericGrade));//the round method is used from the math class

else
else
else
else

break;
}
]

this is the error log
[/
^
C:\Documents and Settings\Home\Desktop\programming\UribarriMiguelProg4.java:51: cannot find symbol
symbol : method println(java.lang.String,double)
location: class java.io.PrintStream
^
errors

Tool completed with exit code 1
]

This post has been edited by mauribarri: 16 April 2009 - 08:43 PM

### #8 mauribarri

Reputation: 0
• Posts: 8
• Joined: 12-April 09

Posted 16 April 2009 - 08:38 PM

Krisjaz,
disregard the last post. it was a typo on my part for the if else if content. Now error 6 is still showing up. if I changed it to printf i then get an error stating that i have not initialized the variable numericGrade in the while statement while (numericGrade == 'X')...kinda lost here because i thought the variable had been initialized.

### #9 mauribarri

Reputation: 0
• Posts: 8
• Joined: 12-April 09

Posted 18 April 2009 - 12:12 PM

I was able to fix my errors and can now compile the java file and run it. It asks me to enter letters or numbers and you can type either of the two, but once you do and hit enter it does not go to either of my while statements. It just goes to press any key to continue and exits. Some guidance is greatly appreciated. Here's the code:

```/**********************************************************************
* UribarriMiguelProg4.java
* Miguel Uribarri
*
* This program will convert number grades to letter or
* convert letter grades to numbers
**********************************************************************/

import java.util.Scanner;

public class UribarriMiguelProg4
{
public static void main(String[] args)
{

Scanner stdIn = new Scanner (System.in);

System.out.print("Will you be using numbers or letters?");

{
System.out.println("Enter a number grade in double format. (Ex. 74.25463): ");
numericGrade = Double.parseDouble(stdIn.next());//original double (with decimal).
numericFinalGrade = Integer.parseInt("" + Math.round(numericGrade));//the round method is used from the math class

else
else
else
else

break;
}

{

{
case 'A':
System.out.println("90-100 = A\nWant to try again? Y to start again or any key to end:");
break;
case 'B':
System.out.println("80-89.9 = B\nWant to try again? Y to start again or any key to end:");
break;
case 'C':
System.out.println("70-79.9 = C\nWant to try again? Y to start again or any key to end:");
break;
case 'D':
System.out.println("60-69.9 = D\nWant to try again? Y to start again or any key to end:");
break;
case 'F':
System.out.println("< 60 = F\nWant to try again? Y to start again or any key to end:");
break;
default:
System.out.println("Entered An Incorrect Grade:\nWant to try again? Y to start again or any key to end:");

break;
}
}
}
}
```

This post has been edited by mauribarri: 18 April 2009 - 12:14 PM

### #10 Fuzzyness

• Comp Sci Student

Reputation: 669
• Posts: 2,438
• Joined: 06-March 09

Posted 18 April 2009 - 11:00 PM

== for char is case sensitive. If you enter 'x' it will equal it to 'X' and it wil return false. So it will skip it.
change it to

And breaking to exist a loop is structurally incorrect. yes, it does work, but depending on how your teacher is they may cont off for it. I would simply make it so that the loop wont meet the conditions. I would recommend adding a boolean to our while loops condition.

So at the top make a boolean variable for each of the while loops. then set it to be while it is true. and at the end of the code, set it to false so it doesnt make an ifinite loops.

```boolean go1 = true;
boolean go2 = true;
while ((numericGrade == 'Y' || numericGrade == 'y')&& go2) // Use an and so that BOTH qualifications have to be met
go2= false; // will exist the loop once all of the code is finished
}
while ((numericGrade == 'X' || numericGrade == 'x') && go1) //saying go1 means the same as go1== true
go1 = false;
}

```

I fixed the while statement problem in that code as well. That is an effecient and Structurally correct way of doing it.

### #11 krizjaz

Reputation: 20
• Posts: 98
• Joined: 07-October 07

Posted 19 April 2009 - 10:18 AM

Fuzzyness, on 18 Apr, 2009 - 10:00 PM, said:

== for char is case sensitive. If you enter 'x' it will equal it to 'X' and it wil return false. So it will skip it.
change it to

And breaking to exist a loop is structurally incorrect. yes, it does work, but depending on how your teacher is they may cont off for it. I would simply make it so that the loop wont meet the conditions. I would recommend adding a boolean to our while loops condition.

So at the top make a boolean variable for each of the while loops. then set it to be while it is true. and at the end of the code, set it to false so it doesnt make an ifinite loops.

```boolean go1 = true;
boolean go2 = true;
while ((numericGrade == 'Y' || numericGrade == 'y')&& go2) // Use an and so that BOTH qualifications have to be met
go2= false; // will exist the loop once all of the code is finished
}
while ((numericGrade == 'X' || numericGrade == 'x') && go1) //saying go1 means the same as go1== true
go1 = false;
}

```

I fixed the while statement problem in that code as well. That is an effecient and Structurally correct way of doing it.

Yes, it is better to use boolean in while loops than the strings.

Take a look at some revisions of the code:
```/**********************************************************************
* UribarriMiguelProg4.java
* Miguel Uribarri
*
* This program will convert number grades to letter or
* convert letter grades to numbers
**********************************************************************/

import java.util.Scanner;

public class UribarriMiguelProg4
{
public static void main(String[] args)
{

Scanner stdIn = new Scanner (System.in);

System.out.print("Will you be using numbers or letters?");
boolean go1 = false;
boolean go2 = false;

go1 = true;
go2 = true;
}else{
System.out.println("Please enter only 'X' and 'Y'");
boolean go3 = true;
while(go3 == true){
try{
go3 = false;
go1 = true;
go3 = false;
go1 = true;
}else{
System.out.println("Please enter only 'X' and 'Y'");
go3 = true;
}
}catch(Exception ex){
System.out.println("Please enter only 'X' and 'Y'");
go3 = true;
}
}
}

while (go1 == true)
{
System.out.println("Enter a number grade in double format. (Ex. 74.25463): ");
numericGrade = Double.parseDouble(stdIn.next());//original double (with decimal).
numericFinalGrade = Integer.parseInt("" + Math.round(numericGrade));//the round method is used from the math class

else
else
else
else

System.out.println("Want to try again? Y to start again or any key to end:");
go1 = true;
}else{
go1 = false;
}
}

while (go2 == true)

{

{
case 'A':
System.out.println("90-100 = A");
break;
case 'B':
System.out.println("80-89.9 = B");
break;
case 'C':
System.out.println("70-79.9 = C");
break;
case 'D':
System.out.println("60-69.9 = D");
break;
case 'F':
System.out.println("< 60 = F");
break;
default:
break;
}
System.out.println("Want to try again? Y to start again or any key to end:");
go2 = true;
}else{
go2 = false;
}
}
}
}

```

Edit it if you want the user not to input nothing so that there would be no exceptions. . =)