Results 1 to 9 of 9
  1. #1
    lbgladson is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default for loop not working properly

    I have an assignment to get user input for a students name, assignment, number score and letter grade. I have to use a for loop to determine how many students to enter information for and then output all of the information for each student. I cannot figure out where to put the line to output all of the information so that it doesn't output it after entering the information for each student.

    Java Code:
    public class Grader
    {
    	String last;
    	String assign;
    	int score;
    	String letterGrade;
    	
    	public Grader(String lastName, String assignment, int grade, String letterGrade)
    	{
    		last = lastName;
    		assign = assignment;
    		score = grade;
    	}
    	
    	public String getStudent()
    	{
    		return last;
    	}
    	
    	public String getAssignment()
    	{
    		return assign;
    	}
    	
    	public int getScore()
    	{
    		return score;
    	}
    	
    	public String getGrade()
    	{
    		if (score <= 59)
    			letterGrade = "F";
    		else if (score >= 60 && score <= 69)
    			letterGrade = "D";
    		else if (score >= 70 && score <= 72)
    			letterGrade = "C-";
    		else if (score >= 73 && score <= 76)
    			letterGrade = "C";
    		else if (score >= 77 && score <= 79)
    			letterGrade = "C+";
    		else if (score >= 80 && score <= 82)
    			letterGrade = "B-";
    		else if (score >= 83 && score <= 86)
    			letterGrade = "B";
    		else if (score >= 87 && score <= 89)
    			letterGrade = "B+";
    		else if (score >= 90 && score <= 92)
    			letterGrade = "A-";
    		else if (score >= 93 && score <= 96)
    			letterGrade = "A";
    		else if (score >= 97 && score <= 100)
    			letterGrade = "A+";
    			
    		return letterGrade;
    		
    	}
    	
    
    }
    Java Code:
    import java.util.Scanner;
    
    public class GradeTester
    {
    	public static void main(String[] args) 
    	{
    		
    		Scanner in = new Scanner(System.in);
    		
    		int x = 0;
    		System.out.print("Enter number of students: ");
    		x = in.nextInt();
    		
    
    		
    		for (int i = 0; i < x; i++)
    		{	
    			String letterGrade = "";
    			
    			System.out.print("Enter the students last name: ");
    			String lastName = in.next();
    			
    			System.out.print("Enter the assignment: ");
    			String assignment = in.next();
    			
    			System.out.print("Enter the grade: ");
    			int grade = in.nextInt();
    			
    
    			Grader grade1 = new Grader(lastName, assignment, grade, letterGrade);
    			System.out.print(grade1.getStudent() + grade1.getAssignment() + grade1.getScore() + grade1.getGrade());
    			
    			
    		}
    		
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: for loop not working properly

    If you want to enter all the information for each student, and then after you're done doing this, display all the results, then you'll need two for loops, one to input the information and one after the first one to display the results.

  3. #3
    lbgladson is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: for loop not working properly

    I added this for loop but now it is not displaying any information.

    Java Code:
    for (int j = 0; i < j; j++)
    			{		
    
    			Grader grade1 = new Grader(lastName, assignment, grade, letterGrade);
    			System.out.print(grade1.getStudent() + grade1.getAssignment() + grade1.getScore() + grade1.getGrade());
    			
    			
    		}

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: for loop not working properly

    Sorry, I needed to look closer at your code. You'll need an array of Grader of size x, fill it with Grader objects in the first for loop, and then display the results held by the Grader items in the array in the second for loop. If you're still stuck, please post your latest code.

  5. #5
    lbgladson is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: for loop not working properly

    I thought about using an array because that would make sense however we have not covered arrays yet so is there another way to do it without using an array?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: for loop not working properly

    Quote Originally Posted by lbgladson View Post
    I thought about using an array because that would make sense however we have not covered arrays yet so is there another way to do it without using an array?
    You could use an ArrayList, but I consider that even more advanced that using arrays. The problem is if you want to store the input obtained in the first array, you'll need a data structure with which to do this.

  7. #7
    lbgladson is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: for loop not working properly

    The assignment says:
    Ask the user for a student last name and a score with a Scanner using its next() method and its nextIn() method. It should then create a Grader object using the appropriate information calling the appropriate constructor. It should then display the information obtained from the Grader object and display the assignment name, student name, score, and grade. So is there any way to make this work without an array or would I have to display the information after each person is entered?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: for loop not working properly

    Quote Originally Posted by lbgladson View Post
    The assignment says:
    Ask the user for a student last name and a score with a Scanner using its next() method and its nextIn() method. It should then create a Grader object using the appropriate information calling the appropriate constructor. It should then display the information obtained from the Grader object and display the assignment name, student name, score, and grade. So is there any way to make this work without an array or would I have to display the information after each person is entered?
    My take on the assignment is that it looks like you will want to display the result of each student immediately after entering it. So that suggests that you use only one for loop, no array or ArrayList, and I think that the flow in pseudocode will be:

    Java Code:
    for x number of students:
       enter one student's information
       display that same student's information
    end for loop.
    This looks like what you already have posted in your original post. One thing you might need to add is a System.out.println() statement or two so that you don't print everything on the same line.

  9. #9
    lbgladson is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: for loop not working properly

    Ok thank you...that is what I will do since it doesn't state that you have to print them all at the end and we haven't learned arrays.

Similar Threads

  1. TCP/IP client not working properly
    By Chrillz in forum Networking
    Replies: 2
    Last Post: 07-20-2011, 07:39 PM
  2. Why this block is not working properly?
    By juhiswt in forum New To Java
    Replies: 2
    Last Post: 03-13-2011, 11:08 AM
  3. date is not working properly
    By newnewgen in forum New To Java
    Replies: 1
    Last Post: 10-12-2010, 09:04 AM
  4. event handler not working properly
    By H3rtaherta in forum Java 2D
    Replies: 3
    Last Post: 11-24-2008, 02:39 AM
  5. Log4j not working properly....
    By prakash_dev in forum Advanced Java
    Replies: 0
    Last Post: 03-17-2008, 12:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •