Results 1 to 6 of 6
  1. #1
    stan989 is offline Member
    Join Date
    Dec 2011
    Posts
    20
    Rep Power
    0

    Default 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


    Java 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.

    ----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

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

    Default 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.

  3. #3
    stan989 is offline Member
    Join Date
    Dec 2011
    Posts
    20
    Rep Power
    0

    Default Re: Java program Fillarray method error :[ help is greatly needed pelase

    Quote Originally Posted by Fubarable View Post
    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 :[

  4. #4
    stan989 is offline Member
    Join Date
    Dec 2011
    Posts
    20
    Rep Power
    0

    Default Re: Java program Fillarray method error :[ help is greatly needed pelase

    could i change
    Java 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?
    Java 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);

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java program Fillarray method error :[ help is greatly needed pelase

    Quote Originally Posted by stan989 View Post
    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?

  6. #6
    stan989 is offline Member
    Join Date
    Dec 2011
    Posts
    20
    Rep Power
    0

    Default Re: Java program Fillarray method error :[ help is greatly needed pelase

    Java 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;
    	}
    }

Similar Threads

  1. Help Needed. New to Java Compound Interest Program....
    By SubjectJ80 in forum New To Java
    Replies: 7
    Last Post: 11-05-2011, 10:15 AM
  2. Java Calculator Help Would Be Greatly Appreciated
    By TommyR in forum New To Java
    Replies: 1
    Last Post: 03-14-2011, 10:30 PM
  3. Morse Code Java Program - Help Needed Please!!
    By dungeondragon in forum New To Java
    Replies: 8
    Last Post: 02-25-2011, 05:36 AM
  4. the simlex method program by java ?
    By mufix in forum New To Java
    Replies: 1
    Last Post: 04-09-2010, 09:14 AM
  5. Help needed very simple java program
    By pavez in forum New To Java
    Replies: 3
    Last Post: 08-22-2009, 04:05 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
  •