Java program Fillarray method error :[ help is greatly needed pelase
Hello, im having problems when i compile this program. After setParam is invoked. Fillarray is giving me Invalid entry when user enter 'f' or 'F'
could someone please let me know what is wrong with my code? :/ thanks in advance
Code:
import java.io.IOException;
import java.util.Scanner;
import java.util.Random;
public class Final {
public static void main(String[] args) throws IOException {
int c; // user choice
int nStudents; // number of students
int nQuizes; // number of quizes
int SPok; // set parameters
int FAok; // fill array
long[] studentIDs = new long[DefineConstants.MAXSTUDENTS];
int[][] studentQScores = new int[DefineConstants.MAXSTUDENTS][DefineConstants.MAXQUIZES];
SPok = 0;
FAok = 0;
nStudents = 0;
nQuizes = 0;
System.out
.println("***************************************************");
System.out
.println(" WELCOME TO THE GRADEBOOK, PREPARE TO BE AMAZED!!!");
System.out
.println("***************************************************");
while (true) {
System.out
.print("\nQuit Help SetParams FillArray DisplayResults ");
System.out.print("\nSelect Q H S F or D and press Enter: ");
while ((c = System.in.read()) == '\n')
;
if (c == 'q')
c = 'Q';
if (c == 'h')
c = 'H';
if (c == 's')
c = 'S';
if (c == 'f')
c = 'F';
if (c == 'd')
c = 'D';
switch (c) {
case 'Q':
Quit();
break;
case 'H':
getHelp();
break;
case 'S':
setParams(nStudents, nQuizes);
SPok = 1;
FAok = 0;
break;
case 'F':
if (SPok != 0) {
fillArray(studentIDs, studentQScores, nStudents, nQuizes);
FAok = 1;
} else
System.out.print("You must SetParams first\n");
break;
case 'D':
if (FAok != 0)
displayResults(studentIDs, studentQScores, nStudents,
nQuizes);
else {
if (SPok != 0)
System.out.print(" You must FillArray first\n");
else
System.out.print(" You must SetParams first\n");
}
break;
default:
System.out.printf("Invalid entry <%c>", c);
break;
}
}
}
private static void displayResults(long[] sIDs, int[][] sQScores, int nS,
int nQ) {
int i;
int j;
int n;
int MAXQUIZES = 5;
double[] quizAvgs = new double[MAXQUIZES];
int[] quizMaxs = new int[MAXQUIZES];
int[] quizMins = new int[MAXQUIZES];
int[] quizMeds = new int[MAXQUIZES];
byte[][] quizGrid = new byte[MAXQUIZES][101]; // Grade rang 0 - 100 -->
// 101 Elements
for (i = 0; i < nQ; i++) {
quizAvgs[i] = 0.0;
quizMaxs[i] = 0;
quizMins[i] = 100;
for (j = 0; j < 101; j++)
quizGrid[i][j] = 0;
}
System.out.print("\nStudent ID:");
for (j = 0; j < nQ; j++)
System.out.printf("Quiz %1d", j + 1);
for (i = 0; i < nS; i++) {
System.out.printf("\n%d", sIDs[i]);
for (j = 0; j < nQ; j++) {
System.out.printf(" %3d", sQScores[i][j]);
quizAvgs[j] = (i * quizAvgs[j] + sQScores[i][j] / (i + 1));
if (sQScores[i][j] > quizMaxs[j])
quizMaxs[j] = sQScores[i][j];
if (sQScores[i][j] < quizMins[j])
quizMins[j] = sQScores[i][j];
quizGrid[j][sQScores[i][j]] = 1;
}
}
for (i = 0; i < nQ; i++) {
n = 0;
for (j = 0; j < 101; j++)
if (quizGrid[i][j] != 0)
n++;
n = (n + 1) / 2;
for (j = 0; j < 101; j++)
if (quizGrid[i][j] != 0)
n--;
quizMeds[i] = j - 1;
}
System.out.print("\n\nMaxs:");
for (i = 0; i < nQ; i++)
System.out.printf("%3d", quizMaxs[i]);
System.out.print("\nMins:");
for (i = 0; i < nQ; i++)
System.out.printf("%3d", quizMins[i]);
System.out.print("\nAvgs:");
for (i = 0; i < nQ; i++)
System.out.printf("%6.2f", quizAvgs[i]);
System.out.print("\nMeds:");
for (i = 0; i < nQ; i++)
System.out.printf("%3d", quizMeds[i]);
System.out.print("\n");
}
private static void fillArray(long[] sIDs, int[][] sQScores, int nS, int nQ) {
int i;
int j;
int FIRSTSID = 75678;
for (i = 0; i < nS; i++) {
(sIDs)[i] = FIRSTSID + i;
for (j = 0; j < nQ; j++)
(sQScores)[i][j] = (int) (((float) rand()) / 324.5);
}
}
private static float rand() {
Random rand = new Random();
int number = 0;
{
for (int counter = +1; counter <= 1; counter++) {
number = rand.nextInt(100);
System.out.println(number);
}
}
return number;
}
private static void setParams(int nS, int nQ) {
int n;
new String(new char[80]);
Scanner keyboard = new Scanner(System.in);
n = 0;
while (n < 1 || n > 50) {
System.out.print("Please enter the number of students(1-50):");
n = keyboard.nextInt();
}
nS = n;
n = 0;
while (n < 1 || n > 5) {
System.out
.print("Please enter the number of quizes per student(1-5):");
n = keyboard.nextInt();
}
nQ = n;
}
private static void getHelp() {
System.out
.println("\n______________________HELP MENU__________________________"
+ "\n Welcome to the Help Menu, here you will find tips to using this program."
+ "\n"
+ "\n The first step is to click the 'SET PARAMETERS' button. Here you will "
+ "\n input the number of students (up to 50) and number of quizzes for each "
+ "\n student (up to 5.) "
+ "\n "
+ "\n Next you will select the 'FILL ARRAY' button. This button will open up"
+ "\n the menu to select the ID numbers and quiz grades for each student and "
+ "\n quiz, then store them within the paremeters set in 'SET PARAMETERS'."
+ "\n "
+ "\n Finally you will click the 'DISPLAY' button. This button will compile all"
+ "\n the data you entered in the last two sections and display the results."
+ "\n This program computes and displays the lowest, highest, average, and"
+ "\n medium for each grade of each set of quizzes."
+ "\n "
+ "\n As an added note if you wish to quit this program at any time, simply"
+ "\n press the 'QUIT' button and the program will close");
}
private static void Quit() {
System.out.print("\nADIOS\n");
System.exit(0);
}
final class DefineConstants {
public static final int MAXSTUDENTS = 50;
public static final int MAXQUIZES = 5;
public static final int FIRSTSID = 75678;
}
}
i am using jGrasp and this is what happens when i execute the program.
Quote:
----jGRASP: process aborted by user.
----jGRASP exec: java Finals
************************************************** *
WELCOME TO THE GRADEBOOK, PREPARE TO BE AMAZED!!!
************************************************** *
Quit Help SetParams FillArray DisplayResults
Select Q H S F or D and press Enter: s
Please enter the number of students(1-50):3
Please enter the number of quizes per student(1-5):2
Quit Help SetParams FillArray DisplayResults
Select Q H S F or D and press Enter: f
Quit Help SetParams FillArray DisplayResults
Select Q H S F or D and press Enter: Invalid entry <
>
Quit Help SetParams FillArray DisplayResults
Select Q H S F or D and press Enter: F
Quit Help SetParams FillArray DisplayResults
Select Q H S F or D and press Enter: Invalid entry <
>
Quit Help SetParams FillArray DisplayResults
Select Q H S F or D and press Enter: q
ADIOS
Re: Java program Fillarray method error :[ help is greatly needed pelase
That's a heck of a lot of code -- and all left justified (?!). Why not just write a small program that tries to solve or isolate just this current problem, post it here, and we'll be able to help you better.
Re: Java program Fillarray method error :[ help is greatly needed pelase
Quote:
Originally Posted by
Fubarable
That's a heck of a lot of code -- and all left justified (?!). Why not just write a small program that tries to solve or isolate just this current problem, post it here, and we'll be able to help you better.
this was what i'm required to do by my professor. it's almost complete but i do not understand why when i input 'f' or 'F' to execute FillArray it returns Invalid entry :[
Re: Java program Fillarray method error :[ help is greatly needed pelase
could i change Code:
if (c == 'q')
c = 'Q';
if (c == 'h')
c = 'H';
if (c == 's')
c = 'S';
if (c == 'f')
c = 'F';
if (c == 'd')
c = 'D';
switch (c) {
case 'Q':
Quit();
break;
case 'H':
getHelp();
break;
case 'S':
setParams(nStudents, nQuizes);
SPok = 1;
FAok = 0;
break;
case 'F':
if (SPok != 0) {
fillArray(studentIDs, studentQScores, nStudents, nQuizes);
FAok = 1;
} else
System.out.print("You must SetParams first\n");
break;
case 'D':
if (FAok != 0)
displayResults(studentIDs, studentQScores, nStudents,
nQuizes);
else {
if (SPok != 0)
System.out.print(" You must FillArray first\n");
else
System.out.print(" You must SetParams first\n");
}
break;
default:
System.out.printf("Invalid entry <%c>", c);
break;
}
}
to something this? Code:
boolean setPRun=false;
boolean fillARun=false;
char select;
String buffer;
while (true)
buffer = in.nextLine()
selet = buffer.charAt(0);
if (select == 'h' || select == 'H')
getHelp();
if (select == 's' || select == 'S')
{
setParams();
setPRun = true;
}
if (select == 'f' || select == 'F')
if (setPRun)
{
fillArray();
fillArun = true;
}
else
System.out.println("SetParams must be run first!");
if (select == 'd' || select == 'D')
if (fillARun)
displayResults();
else
System.out.println("FillArray must be run first!");
if (select == 'q' || select == 'Q')
System.exit(0);
Re: Java program Fillarray method error :[ help is greatly needed pelase
Quote:
Originally Posted by
stan989
this was what i'm required to do by my professor.
I'm confused. Does he require you not to indent your code at all so no one can read and understand it?
Or does he require that you not break your problem down and isolate the cause so that you and others can understand and fix it?
Re: Java program Fillarray method error :[ help is greatly needed pelase
Code:
import java.io.IOException;
import java.util.Scanner;
import java.util.Random;
public class Finals
{
public static void main(String[] args) throws IOException
{
int c; // user choice
int nStudents; // number of students
int nQuizes; // number of quizes
int SPok; // set parameters
int FAok; // fill array
long[] studentIDs = new long[DefineConstants.MAXSTUDENTS];
int[][] studentQScores = new int[DefineConstants.MAXSTUDENTS][DefineConstants.MAXQUIZES];
SPok = 0;
FAok = 0;
nStudents = 0;
nQuizes = 0;
System.out.println("***************************************************");
System.out.println(" WELCOME TO THE GRADEBOOK, PREPARE TO BE AMAZED!!!");
System.out.println("***************************************************");
while (true)
{
System.out.print("\nQuit Help SetParams FillArray DisplayResults ");
System.out.print("\nSelect Q H S F or D and press Enter: ");
while ((c = System.in.read()) == '\n');
if (c == 'q')
c = 'Q';
if (c == 'h')
c = 'H';
if (c == 's')
c = 'S';
if (c == 'f')
c = 'F';
if (c == 'd')
c = 'D';
switch (c)
{
case 'Q':
Quit();
break;
case 'H':
getHelp();
break;
case 'S':
setParams(nStudents, nQuizes);
SPok = 1;
FAok = 0;
break;
case 'F':
if (SPok != 0)
{
fillArray(studentIDs, studentQScores, nStudents, nQuizes);
FAok = 1;
}
else
System.out.print("You must SetParams first\n");
break;
case 'D':
if (FAok != 0)
displayResults(studentIDs, studentQScores, nStudents, nQuizes);
else
{
if (SPok != 0)
System.out.print(" You must FillArray first\n");
else
System.out.print(" You must SetParams first\n");
}
break;
default:
System.out.printf("Invalid entry <%c>", c);
break;
}
}
}
private static void displayResults(long[] sIDs, int[][] sQScores, int nS, int nQ)
{
int i;
int j;
int n;
int MAXQUIZES = 5;
double[] quizAvgs = new double[MAXQUIZES];
int[] quizMaxs = new int[MAXQUIZES];
int[] quizMins = new int[MAXQUIZES];
int[] quizMeds = new int[MAXQUIZES];
byte[][] quizGrid = new byte[MAXQUIZES][101]; // Grade rang 0 - 100 --> // 101 Elements
for (i = 0; i < nQ; i++)
{
quizAvgs[i] = 0.0;
quizMaxs[i] = 0;
quizMins[i] = 100;
for (j = 0; j < 101; j++)
quizGrid[i][j] = 0;
}
System.out.print("\nStudent ID:");
for (j = 0; j < nQ; j++)
System.out.printf("Quiz %1d", j + 1);
for (i = 0; i < nS; i++)
{
System.out.printf("\n%d", sIDs[i]);
for (j = 0; j < nQ; j++)
{
System.out.printf(" %3d", sQScores[i][j]);
quizAvgs[j] = (i * quizAvgs[j] + sQScores[i][j] / (i + 1));
if (sQScores[i][j] > quizMaxs[j])
quizMaxs[j] = sQScores[i][j];
if (sQScores[i][j] < quizMins[j])
quizMins[j] = sQScores[i][j];
quizGrid[j][sQScores[i][j]] = 1;
}
}
for (i = 0; i < nQ; i++)
{
n = 0;
for (j = 0; j < 101; j++)
if (quizGrid[i][j] != 0)
n++;
n = (n + 1) / 2;
for (j = 0; j < 101; j++)
if (quizGrid[i][j] != 0)
n--;
quizMeds[i] = j - 1;
}
System.out.print("\n\nMaxs:");
for (i = 0; i < nQ; i++)
System.out.printf("%3d", quizMaxs[i]);
System.out.print("\nMins:");
for (i = 0; i < nQ; i++)
System.out.printf("%3d", quizMins[i]);
System.out.print("\nAvgs:");
for (i = 0; i < nQ; i++)
System.out.printf("%6.2f", quizAvgs[i]);
System.out.print("\nMeds:");
for (i = 0; i < nQ; i++)
System.out.printf("%3d", quizMeds[i]);
System.out.print("\n");
}
private static void fillArray(long[] sIDs, int[][] sQScores, int nS, int nQ)
{
int i;
int j;
int FIRSTSID = 75678;
for (i = 0; i < nS; i++)
{
(sIDs)[i] = FIRSTSID + i;
for (j = 0; j < nQ; j++)
(sQScores)[i][j] = (int) (((float) rand()) / 324.5);
}
}
private static float rand()
{
Random rand = new Random();
int number = 0;
{
for (int counter = +1; counter <= 1; counter++)
{
number = rand.nextInt(100);
System.out.println(number);
}
}
return number;
}
private static void setParams(int nS, int nQ)
{
int n;
new String(new char[80]);
Scanner keyboard = new Scanner(System.in);
n = 0;
while (n < 1 || n > 50)
{
System.out.print("Please enter the number of students(1-50):");
n = keyboard.nextInt();
}
nS = n;
n = 0;
while (n < 1 || n > 5)
{
System.out.print("Please enter the number of quizes per student(1-5):");
n = keyboard.nextInt();
}
nQ = n;
}
private static void getHelp()
{
System.out.println("\n______________________HELP MENU__________________________"
+ "\n Welcome to the Help Menu, here you will find tips to using this program."
+ "\n"
+ "\n The first step is to click the 'SET PARAMETERS' button. Here you will "
+ "\n input the number of students (up to 50) and number of quizzes for each "
+ "\n student (up to 5.) "
+ "\n "
+ "\n Next you will select the 'FILL ARRAY' button. This button will open up"
+ "\n the menu to select the ID numbers and quiz grades for each student and "
+ "\n quiz, then store them within the paremeters set in 'SET PARAMETERS'."
+ "\n "
+ "\n Finally you will click the 'DISPLAY' button. This button will compile all"
+ "\n the data you entered in the last two sections and display the results."
+ "\n This program computes and displays the lowest, highest, average, and"
+ "\n medium for each grade of each set of quizzes."
+ "\n "
+ "\n As an added note if you wish to quit this program at any time, simply"
+ "\n press the 'QUIT' button and the program will close");
}
private static void Quit()
{
System.out.print("\nADIOS\n");
System.exit(0);
}
final class DefineConstants
{
public static final int MAXSTUDENTS = 50;
public static final int MAXQUIZES = 5;
public static final int FIRSTSID = 75678;
}
}