?
Solved

Java - Loop

Posted on 2008-11-18
6
Medium Priority
?
3,358 Views
Last Modified: 2012-05-05
I add this part to the code.
int i;
   for (i = 1; i <=10; i++)
but it is not doing what I need.  
I just repeats the answer 10 times.  

How do you determine the numeric grade for one single letter grade, it repeatedly calculates and displays numeric grades for a sequence of exactly 10 grades?

Any help would greatly be appreciated.

package charatmethod;
import java.util.Scanner;
 
public class CharAtMethod
{
 
public static void main(String[] args) 
{
Scanner in = new Scanner(System.in);
 
System.out.println("Enter a letter grade:");
String input = in.nextLine();
 
Grade g = new Grade(input);
 
double grade = g.getNumericGrade();
 
int i;
   for (i = 1; i <=10; i++)
System.out.println("Numeric value: " + grade);
}
 
}
************************************
package charatmethod;
public class Grade
{
    public Grade(String initGrade)
    {
        letterGrade = initGrade;
    }
public double getNumericGrade()
{
    double grade = 0;
switch(letterGrade.charAt(0)) 
{
case 'A': grade = 4.0;break;
case 'B': grade = 3.0; break;
case 'C': grade = 2.0; break;
case 'D': grade = 1.0; break;
default: grade = 0.0; break;
 
}
 
for (int i = 0; i < letterGrade.length(); i++)
{
   if (letterGrade.charAt(i) == '-')   
       grade = grade - 0.3;
     else if (letterGrade.charAt(i) == '+')
        grade = grade + 0.3;
}
    return grade;
            
}  
    private String letterGrade;
}

Open in new window

0
Comment
Question by:ca1358
6 Comments
 
LVL 12

Expert Comment

by:marklorenz
ID: 22990735
I'm not totally sure what you're trying to do.  If you take out the for loop in your main method, you will ask for one letter grade and output the related numeric grade.  If you want a loop, it would have to encompass the part where you ask for a grade.  You'd probably want to go until nothing is entered, to indicate it's done (versus a for loop).
0
 

Author Comment

by:ca1358
ID: 22990767
I am not sure what it asking.

It states
Alter the main class of the project so that, instead of just determining the numeric grade for one single letter grade, it repeatedly calculates and displays numeric grades for a sequence of exactly 10 grades.  

You should add a loop to the main method that will ensure that the process of "retrieve letter grade, then calculate and display the numeric grade, will be repeated 10 times.

Of course it gives no explanation of how to do this.
0
 
LVL 15

Expert Comment

by:quincydude
ID: 22990783
do you actually want something like
public static void main(String[] args) 
{
Scanner in = new Scanner(System.in);
String input;
Grade g;
double grade;
 
 
int i;
   for (i = 1; i <=10; i++)
	{
	   System.out.println("Enter a letter grade:");
		input = in.nextLine();
		g = new Grade(input);
		grade = g.getNumericGrade();
		System.out.println("Numeric value: " + grade);
	}
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 22990821
Think you want something more like this so that you keep prompting for letter grade and displaying for 10 times and then stops.
public class CharAtMethod {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
 
        Grade g;
        double grade;
        
        for (int i = 1; i <= 10; i++) {
            System.out.println("Enter a letter grade:");
            g = new Grade(in.nextLine());
            grade = g.getNumericGrade();
            System.out.println("Numeric value: " + grade);
        }
    }
}

Open in new window

0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22990838
That is what I get for not refreshing, what quincydude is pretty much what I was trying to show you.  You just needed to move the code for input and conversion from letter grade to inside the for loop.

This is just hte basics of the solution and should get you started.  You will have to ensure that you get valid input each time if they want 10 good executions.  Other interpretation/implementation could be to allow entry of 10 grades in a row and then calculate and display all ten at once.  Again that is all based on the above code as a start so good luck.

Regards,
Kevin
0
 

Author Closing Comment

by:ca1358
ID: 31518089
Thanks!!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question