0
 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

3
  • you are trying to convert a string a to number, its one of your parse method. Commented Sep 28, 2015 at 11:05
  • Did you ever try to calculate a + 1 in a base 10 system? then you should know, together with the exception name, that the value a doesn´t represent a valid Number, and hence can´t be parsed to one. Commented Sep 28, 2015 at 11:05
  • Show us your console output. Commented Sep 28, 2015 at 11:07

4 Answers 4

1

I think exception is at this line

name = Integer.parseInt(str);

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Please add further clarification requests as comments under the question.
0

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

Comments

-1

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.