1

I am working on a program for a homework program and everything is finally compiled however I am getting some runtime errors that I am unsure of how to interpret.

import java.util.Scanner;
import java.lang.Math;

public class A1
{
   public static void main(String [] args)
   {
      float ex, mid, bonus, cheat, a1, a2, a3, a4, a5, a6, a7, grade,A; 
      float Jb=0;
      String name; 
      String letGr="NA";
      Scanner scanner = new Scanner(System.in);
      System.out.println("Please input grades");
      String line = scanner.nextLine();
      String data[] = line.split("|");
      name = data[0];
      ex = Float.parseFloat(data[1]);
      mid = Float.parseFloat(data[2]);
      bonus = Float.parseFloat(data[3]);
      cheat = Float.parseFloat(data[4]);
      a1 = Float.parseFloat(data[5]);
      a2 = Float.parseFloat(data[6]);
      a3 = Float.parseFloat(data[7]);
      a4 = Float.parseFloat(data[8]);
      a5 = Float.parseFloat(data[9]);
      a6 = Float.parseFloat(data[10]);
      a7 = Float.parseFloat(data[11]);
      A = (a1+a2+a3+a4+a5+a6+a7)/320;

      grade = 0.5f * ex + 0.1f *mid + 0.1f * A + bonus + Jb;
      if (ex < 40) 
         grade += 0.3 * ex; 
      else
         if (ex > 50)
            grade += 0.2 * Math.max(ex, mid) + 0.1 * Math.max(ex, A);
      else
         grade += 0.02 * ((ex - 40) * Math.max(ex, mid) + (50 - ex) * ex)
         + 0.01 * ((ex - 40) * Math.max(ex, A) + (50 - ex) * ex);
      if (mid < 40)
         grade = Math.min(grade, mid);
      if (A < 50)
         grade = Math.min(grade, A);

      if (cheat >= 1)
         grade = 0;

      if (grade >= 94)
         letGr = ("A+");
      if (grade >= 87 && grade < 94)
         letGr = ("A");
      if (grade >= 80 && grade < 87)
         letGr = ("A-");
      if (grade >= 77 && grade < 80)
         letGr = ("B+");
      if (grade >= 73 && grade < 77)
         letGr = ("B");
      if (grade >= 70 && grade < 73)
         letGr = ("B-");
      if (grade >= 67 && grade < 70)
         letGr = ("C+");
      if (grade >= 63 && grade < 67)
         letGr = ("C");
      if (grade >= 60 && grade < 63)
         letGr = ("C-");
      if (grade >= 57 && grade < 60)
         letGr = ("D+");
      if (grade >= 53 && grade < 57)
         letGr = ("D");
      if (grade >= 50 && grade < 53)
         letGr = ("D-");
      if (grade < 50)
         letGr = ("F");



      System.out.println(line);




   }
}

When data is fed into the program in the form
John Q. Woodcutter|75|82|2|0|15|57|55|56|10|25|48 I get the errors

java.lang.NumberFormatException: For input string: "J"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1222)
    at java.lang.Float.parseFloat(Float.java:422)
    at A1.main(A1.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

I am unsure of what to do, as you can see I need the values that are read in in order to do the later grade calculations but have hit a brick wall as to where to go. I have also tried using delimiters to break the string up as it is read in but was unable to get that method to work. I realize this is a lot of code and I don't expect anyone to do my homework, I was just looking for some hints as to where to go from here and if I am using the Float.parseFloat method correctly.

Thanks for any and all help

tacoofdoomk
  • 53
  • 1
  • 7
  • "*I am unsure of what to do*" in case of exceptions start from debugging. You could use debugger tool, or simply series of `System.our.println` statements which will print content of variables you think may be related to your problem (or all variables). Then if you localize cause of problem, but you can't find solution don't post your entire code but create minimal example which lets us reproduce it. – Pshemo Sep 08 '15 at 17:36

1 Answers1

3
String data[] = line.split("|");

split() takes regular expressions and | is a special character in regexes. To split on literal pipes, escape the pipe.

String data[] = line.split("\\|");

Using "|" causes every character to be split out, which is why parseFloat complains that you passed it the string "J". split("|") is equivalent to split("") which splits the string every time it "finds" an empty string. If you think about it, there is an empty string in between every character.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578