3 Replies - 2713 Views - Last Post: 22 February 2012 - 08:29 PM Rate Topic: -----

#1 hypefear11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-February 12

Simple java program using nested if loops

Posted 22 February 2012 - 07:59 PM

I'm just learning java so yeaa..I need to make a program that's a very very simple "gradebook". It needs to have a nested if-loop and needs to take input of 10 names, and 8 grades for each name. Then output each name with the average of the 8 grades. Ex. Joe - 88 Mike - 92 etc. I have two different "attempts" I guess you could call them, it needs to be more simple like the second coding if possible, but of course none of them work properly.
import java.util.Scanner;

public class gradebook2{
	public static void main(String[] args)
{
int numStudents = 10;
int numGrades = 8;
String name[] = new String[numStudents];
int grade[] = new int[numGrades];

	for(int j = 0; j <= 9; j++)
		{
		Scanner kbReader1 = new Scanner(System.in);
		System.out.print("Enter the student name: ");
		name[j]= kbReader1.nextLine();

	for(int g = 0; g <= 7; g++)
		{
		Scanner kbReader2 = new Scanner(System.in);
		System.out.print("Enter the grade: ");
		grade[g] = kbReader2.nextInt();
		}
		}
	int grades = grade[0] +	grade[1] + grade[2] + grade[3] + grade[4] + grade[5] + grade[6] + grade[7];
	int gradeavg = grades / 8;

	System.out.println(name[0] + "\n" + name[1] + "\n" + name[2] + "\n" + name[3] + "\n" + name[4] + "\n" + name[5]+ "\n" + name[6] + "\n" +name[7] + "\n" + name[8] + "\n" + name[9]);
	System.out.println(gradeavg);
 }
}

Second:
import java.util.Scanner;

public class gradebook3{
	public static void main(String[] args)
{
int numStudents = 10;
int numGrades = 8;
String name[] = new String[numStudents];
int grade[] = new int[numGrades];

	for(int j = 0; j <= 9; j++)
		{
		Scanner kbReader1 = new Scanner(System.in);
		System.out.print("Enter the student name: ");
		name[j]= kbReader1.nextLine();

	for(int g = 0; g <= 7; g++)
		{
		Scanner kbReader2 = new Scanner(System.in);
		System.out.print("Enter the grade: ");
		grade[g] = kbReader2.nextInt();
		}
		}
	int grades = grade[g];
	int gradeavg = grades / 8;

	System.out.println(name[j]);
	System.out.println(gradeavg);
 }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Simple java program using nested if loops

#2 hypefear11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-February 12

Re: Simple java program using nested if loops

Posted 22 February 2012 - 08:14 PM

Forgot another major part, must use arrays! In case you couldn't tell...
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Simple java program using nested if loops

Posted 22 February 2012 - 08:15 PM

First indent your code properly you will then see obvious error
Also only define only one Scanner, when you do a phone call you do not hang up the phone and call back at each phrase

import java.util.Scanner;

public class Gradebook3{
	public static void main(String[] args)
	{
		int numStudents = 10;
		int numGrades = 8;
		String name[] = new String[numStudents];
		int grade[] = new int[numGrades];
		Scanner kbReader = new Scanner(System.in);   // one Scanner

		for(int j = 0; j <= 9; j++)
		{
			System.out.print("Enter the student name: ");
			name[j]= kbReader.nextLine();
// reinitialize total of grades  before the loop
			int grades = 0;
			for(int g = 0; g <= 7; g++)
			{
				System.out.print("Enter the grade: ");
				grade[g] = kbReader.nextInt();
				grades += grade[g];      // you can add the grade[] here
			}
			// average has to be compute for each students so within the for(j loop
			int gradeavg = grades / 8;
			// the print for each J within the loop
			System.out.println(name[j]); 
			System.out.println(gradeavg);
		}
//		int grades = grade[g];      // you are out of the for(g loop. g is not defined anymore
//		int gradeavg = grades / 8;

//		System.out.println(name[j]);  // you are out of the for(int j loop, j is not defined anymore
//		System.out.println(gradeavg);
	}
}



You see out good indentation shows that you are outside the good loops

Happy coding
Was This Post Helpful? 1
  • +
  • -

#4 hypefear11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-February 12

Re: Simple java program using nested if loops

Posted 22 February 2012 - 08:29 PM

Thanks so much, great teacher! The only thing else I needed was to print the names with grade average at the end and not in the loop right after each entry, but I was able to figure out that part pretty easily.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1