Hi I am new in this forum. I need some help with this code and is for my intro to programming class. I did it following a pseudocode I found in other website but I am totally confused now. It is not working. Do nothing. Thanks

This is the assigment "Watson Elementary School contains 10 classrooms numbered 1 through 10. Each classroom can contain any number of students up to 35. Each student takes an achievement test at the end of the school year and receives a score from 0 through 100.

a. Write a Java program that accepts data for each student in the school—student ID, classroom number, and score on the achievement test then prints the classroom's average of the test scores. This program should use one or more arrays. Submit your code."
This is my code:

import javax.swing.JOptionPane; 
public class Watson 
{
    public static void main(String args[])
    { 

    String StringID, StringClassNum, StringScore = null;
    int stuID; 
    int classNum = 0; 
    int score=0;
    int avg = 0;
    int SIZE = 10;
    int QUIT = 999;
    int numStud=35;
    int totalScore []= new int [numStud] ;
    int countClass[] = new int [SIZE];

    StringID = JOptionPane.showInputDialog("Enter a student ID or " +
                    QUIT + " to quit ");
    stuID = Integer.parseInt(StringID); 

    while(stuID != QUIT)
    {
        StringClassNum= JOptionPane.showInputDialog("Enter the classroom number for student");
        classNum= Integer.parseInt(StringClassNum);

        if (classNum >= 1 && classNum <= SIZE )
        {
            countClass[classNum-1] = countClass[classNum-1] + 1;
            StringScore= JOptionPane.showInputDialog("Enter the score for student");
            score= Integer.parseInt(StringScore); 
            totalScore[classNum-1] = totalScore[classNum-1] + score;

            StringID = JOptionPane.showInputDialog("Enter a student ID or " +
                QUIT + " to quit ");
        stuID = Integer.parseInt(StringID);
        }
        else 
            System.out.println("Invalid classroom number");
    }


    while (classNum < SIZE)
    {
        if (countClass[classNum] == 0)
                { avg= 0;}

        else
        { 
        avg =totalScore[classNum]/countClass[classNum];
        System.out.println(classNum ); 
        System.out.println(avg); 
        }
    }
    classNum = classNum + 1;
    }
}

Do nothing. Thanks

something tells me you may want to rephrase some parts of your post above.

  • School contains 10 classrooms numbered 1 through 10
  • classroom can contain any number of students up to 35

to me, that looks a bit like:

/* First class */
class Student{
  private int result;
  // rest of code
}

/* Second class */
class ClassRoom{
  private Student[] students;

  public ClassRoom(){
    students = new Student[35];
  }

  public ClassRoom(int studNr) throws Exception{ //yes, should be more specific, but this is just an example
    if ( studNr > 35 || studNr < 0 )
      throw new Exception("Invalid number of students");
    students = new Student[studNr];
  }
  // rest of code
}

/* Third class */
class School{
  private ClassRoom[] classes = new ClassRoom[10];
  // rest of code
}

could be more complex than you need it, I don't really know where you are in your course/education, but this seems like the logical approach to me.

It is not working. Do nothing.

Put some print statements into your code, starting at the first line of main so you can see what code is being executed, for how many times, and what isn't.

ps: I agree with stultuske - classes like that is how you would do this in real life - but especially the bit where he says this advice may be a bit ahead of where you are in your course.

nice coding for programming so it is better fourm.

I picked something that may be causing your code to not work. On line 38 after the Else statement, you don't have braces. Try putting opening brace and a closing one and see how you go. Yeah, I agree with the other guys on putting the code in classes which is the beauty of Object Oriented Java programming.

since his else contains only one statement, that won't change anything.

Thanks!
this exercise is using arrays (chapter 6 introduction to programming -farrell)
I changed my code. Now it starts with the classNumber and not with the student Id. It is working but print all no just the total. Some feedback?

import javax.swing.JOptionPane; 
public class Watson2 
{
    public static void main(String args[])
    { 
    String StringID, StringClassNum, StringScore = null;
    int stuID; 
    int classNum = 0; 
    int score=0;
    double avg = 0;
    int SIZE = 11;
    int QUIT = 999;
    int numStud=35;
    int totalScore []= new int [numStud] ;
    int countClass[] = new int [SIZE];

        StringClassNum= JOptionPane.showInputDialog("Enter the classroom number or "+
                    QUIT + " to quit ");
        classNum= Integer.parseInt(StringClassNum);
        while(classNum != QUIT)
        {

        if (classNum >= 1 && classNum <= SIZE )
        {
            countClass[classNum-1] = countClass[classNum-1] + 1;
            StringID = JOptionPane.showInputDialog("Enter a student ID");
            stuID = Integer.parseInt(StringID); 
            StringScore= JOptionPane.showInputDialog("Enter the score for student");
            score= Integer.parseInt(StringScore); 
            totalScore[classNum-1] = totalScore[classNum-1] + score;
            avg =totalScore[classNum-1]/countClass[classNum-1] ;

            System.out.println(classNum ); 
            System.out.println(avg);

            StringClassNum= JOptionPane.showInputDialog("Enter the classroom number or "+ QUIT + " to quit ");
        classNum= Integer.parseInt(StringClassNum);

        }
        else 
            System.out.println("Invalid classroom number");
        }

    }
}

Can you post the program's output that shows the problem?

When printing out data, it helps to add an id String that describes what the value is. For example:

System.out.println("avg="+avg)

Where does the program add up the total you want to display?
You should display it outside the loop after the code is finished adding to it.

Thanks!
I added the string
when I enter this data

ID Classroom Score
101 1 75
102 2 86
103 1 92

the output is this

classroom number: 1 average score: 75.0
classroom number: 2 average score: 86.0
classroom number: 1 average score: 83.0

It should be only this

classroom number: 2 average score: 86.0
classroom number: 1 average score: 83.0

If you don't want a line printed every time around the loop, you should move the println statements from inside the loop so nothing is printed until the user says to QUIT. When you exit the loop, will the data you want be in the arrays? You could have another loop that looks through those arrays for the data you want to print.

I added a print statement outside the loop

but now print "999"

classroom number: 1 average score: 23.0
classroom number: 999 average score: 23.0

import javax.swing.JOptionPane; 
public class Watson 
{
    public static void main(String args[])
    { 
    String StringID, StringClassNum, StringScore = null;
    int stuID; 
    int classNum = 0; 
    int score=0;
    double avg = 0;
    int SIZE = 11;
    int QUIT = 999;
    int numStud=35;
    int totalScore []= new int [numStud] ;
    int countClass[] = new int [SIZE];

        StringClassNum= JOptionPane.showInputDialog("Enter the classroom number or "+
                    QUIT + " to quit ");
        classNum= Integer.parseInt(StringClassNum);
        while(classNum != QUIT)
        {

        if (classNum >= 1 && classNum <= SIZE )
        {
            countClass[classNum-1] = countClass[classNum-1] + 1;
            StringID = JOptionPane.showInputDialog("Enter a student ID");
            stuID = Integer.parseInt(StringID); 
            StringScore= JOptionPane.showInputDialog("Enter the score for student");
            score= Integer.parseInt(StringScore); 
            totalScore[classNum-1] = totalScore[classNum-1] + score;
            avg =totalScore[classNum-1]/countClass[classNum-1] ;
            System.out.println("classroom number: " + classNum + " "+ "average score: " + avg);
            StringClassNum= JOptionPane.showInputDialog("Enter the classroom number or "+ QUIT + " to quit ");
            classNum= Integer.parseInt(StringClassNum);

        }
        else

            System.out.println("Invalid classroom number");
        }
        while(classNum <= SIZE)
            classNum =1;
        {
        System.out.println("classroom number: " + classNum + " "+ "average score: " + avg); 
        }
        classNum= classNum + 1;
    }
}

Where is the data that you want to print? Has it been saved in the arrays: totalScore and countClass? You need a loop that goes through those arrays, finds and prints the data that has been stored.

Thanks I got it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.