Skip to content
Advertisement

How to use arrayList sort and manipulate the vlaue

By using Java 11, I am trying to find the average value inside the ArrayList. The algorithm i want to develop is ((value inside list/maximum value)*100)/length of list). I got number format runtime error while I run the program. java.lang.NumberFormatException is the error i got.

JavaScript

enter image description here

Advertisement

Answer

I find two issues with the code above:

  1. The parsing of br.readLine() to int is the line which throws NumberFormatException, since br.readLine() contains both numbers and spaces (at least). Since the line is NOT a valid number, the parsing fails.

    Example:

    • User enters the following when br.readLine() is executed: “1 2 3 4”
    • The next instruction looks like: int N = Integer.parseInt("1 2 3 4")
    • The correct number should not contain spaces: “1234”
  2. This one is not really a bug, however, you’re asking the user to input the numbers again on the second appearance of br.readLine here: new StringTokenizer(br.readLine()," ");

Solution

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement