Hi guys this program is giving a runtime error in Java…The code is full correct and no compilation error?

import java.io.*;

public class JavaApplication

{

public static void main() throws IOException,NumberFormatException

{

BufferedReader bis=new BufferedReader(new InputStreamReader(System.in));

System.out.println(“Please enter ur sex or gender”);

char g=(char)bis.read();

System.out.println(“Enter ur age”);

int age=Integer.parseInt(bis.readLine());

int total=;

if(g==’M’ || g==’m’)

{

if(age>)

{

total=+;

}

else if(age>)

{

total=;

}

System.out.println(“Gender= ” + g +” and age = ” + age + ” and ur pension = ” + total);

}

else if(g==’F’ || g==’f’)

{

if(age>)

{

total=;

}

else if (age>)

{

total=+;

}

System.out.println(“Gender= ” + g + ” and age = ” + age + ” and ur pension = ” + total);

}

}

}

✅ Answers

🥇 Favorite Answer

  • Try

    char g = bis.readLine().charAt();

    Well actually the code should be

    System.out.println(“Please enter your sex or gender”);

    String input = bis.readLine();

    while (input.length() == || input.length() > || (!input.equalsIgnoreCase( “m”) && !input.equalsIgnoreCase( “f”))) {

    System.out.println( “An error was detected in your input”);

    System.out.println(“Please enter your sex or gender”);

    input = bis.readLine();

    } //End while

    char g = input.charAt();

    Have fun.

  • Method invokation is wrong

    import java.io.*;

    public class JavaApplication {

    public static void main(String[] args) {

    try {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.print(“Enter your sex or gender: “);

    String gender = br.readLine();

    System.out.print(“nEnter your age: “);

    int age = Integer.parseInt(br.readLine());

    int total = ;

    if (gender.equalsIgnoreCase(“M”)) {

    if (age > ) {

    total = + ;

    }

    else if (age > ) {

    total = ;

    }

    }

    if (gender.equalsIgnoreCase(“F”)) {

    if (age > ) {

    total = ;

    }

    else if (age > ) {

    total = + ;

    }

    }

    System.out.println(“Gender = ” + gender + ” and age = ” + age + ” and your pension = ” + total);

    }

    catch (Exception ex) {

    System.err.println(ex.toString());

    }

    }

    }

  • Your main declaration is wrong:

    public static void main() throws IOException,NumberFormatException

    should be:

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

    There may be other errors, but that is the most obvious. Good luck!

    See also  make a terminal program with perl that makes the computer talk?

    Source(s): Experience

  • Leave a Comment