0

Whenever I try to compile it, it keeps giving me an exception: enter image description here

This is my source code:

Scanner input = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("#.##");

System.out.print("Gaji Pokok (x 10.000) : ");
double gaji = input.nextDouble();

System.out.print("Lama Tahun Kerja : ");
int th = input.nextInt();

System.out.print("Lama Bulan Kerja : ");
float bl = input.nextFloat();

if ( bl > 12)
{
    System.out.println("Inputan bulan anda salah");
    System.out.print("Masukkan kembali bulan yang benar : ");
    float blnnew = input.nextFloat();
    float tukar = blnnew;
    bl = tukar;
}
float fak_peng;
fak_peng = Float.valueOf(df.format((th+(bl/12))*2.5));

System.out.print("Jumlah Faktor Penghargaan :  " );
System.out.println(fak_peng + " %");

System.out.println("Nilai Sekarang : 1.0000000 " );


float per_mppeg;
per_mppeg = Float.valueOf(df.format(gaji*(fak_peng/100)*1));
System.out.print("Perhitungan MP Pegawai :  " );

System.out.println(gaji + " x " + fak_peng + "% x " + " 1.0000000 = Rp." + (per_mppeg) + "(x 10.000)");

System.out.print("MP Perbulan :  " );
System.out.println(per_mppeg + " + 100% = Rp." + (per_mppeg) + "(x 10.000)");

System.out.println("MP sekaligus 100% : ");


float peserta;
peserta = Float.valueOf(df.format(100.6650*per_mppeg));
float jd;
jd = Float.valueOf(df.format(14.4820*per_mppeg*0.8));
float anak;
anak = Float.valueOf(df.format(0.6090*per_mppeg*0.8));
float jml;
jml = Float.valueOf(df.format(peserta+jd+anak));
System.out.println("   Peserta = 100.6650 x "+ per_mppeg + "       = " + peserta + "(x 10.000)");
System.out.println("   Jd/Dd   =  14.4820 x "+ per_mppeg + " x 80% = " + jd + "(x 10.000)" );
System.out.println("   Anak    =   0.6090 x "+ per_mppeg + " x 80% = " + anak + "(x 10.000)");
System.out.println("Jumlah Total = "+ jml);

float mpdua;
mpdua = Float.valueOf(df.format (jml*0.2)) ;
float mpdel;
mpdel = Float.valueOf(df.format(per_mppeg*0.8)) ;
System.out.println("MP Sekaligus 20% = "+ mpdua + "(x 10.000)");
System.out.println("MP sekaligus 80% = "+ mpdel + "(x 10.000)");
Tom
  • 16,842
  • 17
  • 45
  • 54
  • You'll have to pay attention: I saw something that may rise another exception in your sample code. Please read my answer below – J.Baoby Nov 26 '16 at 15:45

1 Answers1

2

Your exception is not a compile-time error/exception; it is a runtime exception. It is thrown because the thing the scanner is reading cannot be converted to the type you are asking for (e.g., the next thing the scanner should read is "hello" but you are using scanner.nextInt(), as "hello" cannot be converted to an integer it will raise a InputMismatchException).

In your case the exception is raised when asking for a double. Probably you are using the wrong syntax. You should check which syntax your system uses to represent doubles. On some systems, for example, the fractional and the integer part of a double should be separated with a , and on other systems with a .. So one-half on the first type of system should be written as 0,5 but on the second as 0.5. In Java the syntax the scanner uses is defined with a Locale instance. You can check which-one your scanner uses with the locale() method and change it with useLocale() method.

So you should recheck what you give as input.

Besides your problem with the format of double you are creating your DecimalFormat on a discommanded way (see last quote below) and there is another line that may rise an exception ( NumberFormatException ), if you do not pay attention to the Locale instance you are using:

fak_peng = Float.valueOf(df.format((th+(bl/12))*2.5));

As you are using your own format to parse the decimal (new DecimalFormat("#.##");) the string that will be passed to the Float.valueOf method will depend on the Locale instance used to create the DecimalFormat object df (in the code sample you didn't use a specific Locale instance so your systems default Locale instance is used). But Float.valueOf expects its argument to use a specific syntax defined by The Java™ Language Specification regardless to your system as written in the Java API for Float.valueOf:

[...] where Sign, FloatingPointLiteral, HexNumeral, HexDigits, SignedInteger and FloatTypeSuffix are as defined in the lexical structure sections of The Java™ Language Specification, except that underscores are not accepted between digits. If s does not have the form of a FloatValue, then a NumberFormatException is thrown.

(The complete text was too big too include here. Follow this link or the one above to have more info about what Sign, FloatingPointLiteral, HexNumeral, HexDigits, SignedInteger, FloatTypeSuffix and FloatValue exactly represent)

If you want to change the Locale instance used in your DecimalFormat object, read the API for the DecimalFormat class.

To obtain a NumberFormat for a specific locale, including the default locale, call one of NumberFormat's factory methods, such as getInstance(). In general, do not call the DecimalFormat constructors directly, since the NumberFormat factory methods may return subclasses other than DecimalFormat.

In the API (follow link just before quote) they give an example of how you should correctly create an instance of a NumberFormat.

Good luck!

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
J.Baoby
  • 2,167
  • 2
  • 11
  • 17