sujiy
sujiy

Reputation: 1

Exception in thread "main" java.lang.NumberFormatException: For input string: "a"

 import java.io.*;

public class Joinsung {

    public static void main(String args[])throws IOException{

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        Float javag,comg,junjag; //this is grade
        String str;
        int name;

        System.out.println("name");
        str = in.readLine();
        System.out.println("java grade");
        String str1 = in.readLine();
        System.out.println("computer grade");
        String str2 = in.readLine();
        System.out.println("eletronic grade");
        String str3 = in.readLine();

        name = Integer.parseInt(str);
        javag = Float.parseFloat(str1);
        comg = Float.parseFloat(str2);
        junjag = Float.parseFloat(str3);

        System.out.println("this is your grade ");
        System.out.print("total : ");
        System.out.println(javag + comg + junjag );
        System.out.print("avgerage : ");
        System.out.println(javag + comg + junjag );
      }
  }

hi im beginner to studying java. i want to perpect code! but i cant so i need help. help me? this code name is "grade calculator" user can insert grade and program do calculator(ex avg, total..) explain so short help me pls

Upvotes: 0

Views: 841

Answers (4)

Guna
Guna

Reputation: 316

Try this one,

import java.io.*;

public class Joinsung {

public static void main(String args[])throws IOException{

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    Float javag,comg,junjag; //this is grade
    String name;            // this is name

    System.out.println("name");
    name = in.readLine();       //getting value for name
    System.out.println("java grade");
    String str1 = in.readLine();
    System.out.println("computer grade");
    String str2 = in.readLine();
    System.out.println("eletronic grade");
    String str3 = in.readLine();

    javag = Float.parseFloat(str1);
    comg = Float.parseFloat(str2);
    junjag = Float.parseFloat(str3);

    System.out.println("this is your grade ");
    System.out.print("total : ");
    System.out.println(javag + comg + junjag );
    System.out.print("avgerage : ");
    System.out.println((javag + comg + junjag )/3);
  }

}

Upvotes: -1

Saurav
Saurav

Reputation: 118

Are you trying to get "name" as integer input or should this be a String value. Try to input all the values as integer or float and the program would run successfully.

If any String value or even blank is provided the program will give a NumberFormatException treating the value as a String.

Upvotes: 1

Maciej Cygan
Maciej Cygan

Reputation: 5471

I see what you mean ;)

You are trying to parse 'A' as a grade but 'A' is actually a string and not numerical grade which is what you want in this example.

So name = Integer.parseInt(str); will be invalid in this case. It would be valid if you parsed a string which represents a number such as Integer.parseInt("12345") and this will output Integer with value of 12345.

There are couple of ways you can do it but my suggestion would be to stay easy :).

Create if block

if(int >= 80) {
    string = "A"
} else if (int >= 60 && int <80) {
    string = "B";   
} etc...

int in this case will represent the number 'teacher' enters into the system. Each number then can be associated with given Literal grade

Upvotes: 0

Naman Gala
Naman Gala

Reputation: 4692

I think exception is at this line

name = Integer.parseInt(str);

You are trying to parse String (name) which is not an Integer.

Upvotes: 1

Related Questions