1. Member Join Date
Jun 2007
Posts
92
Rep Power
0

## Algorithm problem

This code is supposed to allow the user to enter a 5 student IDs and their results and calculate the total and show the grades all the information should be shown in a table. For some reason the first studentID is indented - does anyone know why?

Java Code:
```import java.util.*;
public class Assignment
{
public static void main(String[] args)
{
//Declares variables and arrays
int i = 0;
int j = 0;
String [] studentIDArray = new String ;
int [][] studentArray = new int ;
Scanner kybd = new Scanner(System.in);

//Welcome message to user
System.out.println("Welcome");
System.out.println("~~~~~~~");
System.out.print ("\n");

//User is prompted to enter student ID and the results
for (i = 0; i < 5; ++i)
{
System.out.print("Please Enter the Student ID: ");
studentIDArray[i] = kybd.next();
for (j = 0; j < 8; ++j)
{
System.out.println("Please Enter the Result for Question " + (j+1));
studentArray[i][j] = kybd.nextInt();
}
}

//Calculates the students total
while (j < 8)
{
j = 0;
while (i < 5)
{
studentArray[i] += studentArray[i][j];
j+=1;
}
i+=1;
}

int k = 0;
String [] gradeArray = new String ;	//Declares new array to store grades

//Calculates the grades for each student
while (i < 5)
{
studentArray[i] += 3;
if (studentArray[i] > 70)
{
}
else if (studentArray[i] > 60)
{
}
else if (studentArray[i] > 40)
{
}
else if (studentArray[i] >= 40)
{
}
k+=1;
i+=1;
}

System.out.print ("\n");
System.out.println ("StudentID Q1   Q2     Q3     Q4     Q5     Q6     Q7     Q8     Total   Grade");
System.out.print ("  ");

i=0;
j=0;
k=0;

//Output students results
while (i < 5)
{
j = 0;
System.out.printf(studentIDArray[i]);
while (j < 9)
{
System.out.printf ("%7d", studentArray[i][j]);
j+=1;
}

System.out.print ("\n");
i+=1;
k+=1;
}
}
}```
Marcus:cool:  Reply With Quote

2. Member Join Date
Jun 2007
Posts
95
Rep Power
0

## Well, one thing at a time:

Java Code:
```System.out.println ("StudentID Q1   Q2     Q3     Q4     Q5     Q6     Q7     Q8     Total   Grade");
System.out.print ("  ");```
Why are you printing two spaces - without a newline after the header? It will simply add these two spaces to the next line - like it does.

As far as the other issues - I'd strongly encourage you to add some System.out.prinln() statements here and there to understand what is going on.

Felissa:p  Reply With Quote

3. Senior Member Join Date
Jun 2007
Posts
111
Rep Power
0

## Can I make a suggestion? - print out the value of "i" and "j" before you enter the while loops. It looks like you're incrementing but never resetting them.

Eric  Reply With Quote

#### Posting Permissions

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