Results 1 to 2 of 2
  1. #1
    leashbom69 is offline Member
    Join Date
    May 2017
    Posts
    1
    Rep Power
    0

    Default new to java, got an error

    ERROR: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at program4.main(program4.java:11)


    CODE:
    import java.util.Scanner;


    public class LabAssignment4 {

    static Scanner input = new Scanner(System.in);


    public static void main(String [] args) {
    int n = Integer.parseInt(args[0]);
    double [] test_array = readTestScores(n);
    printTestResults(test_array);

    }

    static double [] readTestScores(int size) {

    /* Test scores */
    double [] test_scores = new double [size];
    System.out.print("enter test scores:\t");
    for (int count = 0; count < size; count++) {
    test_scores[count] = input.nextDouble();
    }

    return test_scores;
    }

    static char getLetterGrade(double score) {
    if (score >=90)
    return 'A';
    else if (score >=80)
    return 'B';
    else if (score>=70)
    return 'C';
    else if (score >=60)
    return 'D';
    else
    return 'F';
    }

    static void printComment(char grade) {
    System.out.print("COMMENT: ");
    switch (grade) {
    case 'A': System.out.println("Very good!");
    break;
    case 'B': System.out.println("Good!");
    break;
    case 'C': System.out.println("Satisfactory.");
    break;
    case 'D': System.out.println("Needs improvement.");
    break;
    case 'F': System.out.println("Poor.");
    break;
    }
    }

    static void printTestResults(double [] testList) {
    char letterGrade;
    for (int i = 0; i < testList.length; i++) {
    letterGrade = getLetterGrade(testList[i]);
    System.out.print(testList[i] + "\t" + letterGrade + "\t");
    printComment(letterGrade);
    }
    }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: new to java, got an error

    Please wrap you code in code tags to preserve formatting. See BB Codes below for help.

    Are you actually passing an argument on the command line to the program? That is what args[0] expects
    to see.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 6
    Last Post: 02-11-2013, 05:22 AM
  2. Replies: 3
    Last Post: 05-08-2012, 05:26 PM
  3. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  4. Replies: 1
    Last Post: 10-26-2009, 04:38 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
  •