0

Hi I'm a beginner in Java and I am trying to write this program where I can enter numbers, but that when I enter "done", I get the values that I called total, numberInputs. However when i run it and that i input "done" I get Exception in thread "main" java.lang.NumberFormatException: For input string: "done". Do you know how I can fix this please?

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at repeat.numberReader(repeat.java:15)
at repeat.main(repeat.java:23)

This is my code:

import java.util.Scanner;

public class repeat{
    public void numberReader(){
        Scanner myScanner = new Scanner(System.in);
        int total = 1;
        int numberInputs = 1;
        String userInput;

        do {
            System.out.println("Enter a number: ");
            userInput = myScanner.nextLine();
            total = total + Integer.parseInt(userInput);
            numberInputs++;
        } while (!"done".equals(userInput));
        System.out.println(total + "" + numberInputs + "" + (total / numberInputs));
    }

    public static void main(String[] args){
        repeat instance = new repeat();
        instance.numberReader();
    }
}

Thank you for your help

JensS
  • 1,151
  • 2
  • 13
  • 20
hhppaarr
  • 49
  • 2
  • 8
  • You cannot convert words to numbers. "Done" is a word which can't be parsed into an integer. It's bound to give you this error. Check the other question pointed out by nullpointer. – Yashovardhan99 Oct 03 '17 at 16:39

4 Answers4

1

You are checking userInput for the value "done", but you do it AFTER the call to Integer.parseInt(). Calling Integer.parseInt("done") is throwing the NumberFormatException as the string "done" cannot be parsed as an integer.

do {
  System.out.println("Enter a number: ");
  userInput = myScanner.nextLine();
  ***ERROR HERE***
  total = total + Integer.parseInt(userInput);
  numberInputs++;
} while (!"done".equals(userInput));
  System.out.println(total + "" + numberInputs + "" + (total / numberInputs));
}

You can resolve this a couple of ways. The simplest would be to reorder your code so that you check the input immediately after receiving it and only parse it after. This does require having the prompt twice though (once before the loop, and once at the bottom of it. This will also have issues if the first entry is "done" as a do-while loop always executes at least one time. You could change to a standard while loop to avoid this. This also will not handle strings other than your expected "done" string.

System.out.println("Enter a number: ");
userInput = myScanner.nextLine();
do {
  total = total + Integer.parseInt(userInput);
  numberInputs++;
  System.out.println("Enter a number: ");
  userInput = myScanner.nextLine();
} while (!"done".equals(userInput));
  System.out.println(total + "" + numberInputs + "" + (total / numberInputs));
}

Alternatively, you can also implement error handling to catch and handle the exception, but that is a bit more complicated. This sounds like homework, and error handling is probably beyond your scope, so i'd just consider the first option.

As a side note, you should consider changing "done".equals(userInput) to userInput.equals("done"). They are functionally identical, however the second statement is much more clear what your intent is, which is to see if userInput is "done". The first is harder for yourself and others to read and understand.

Tyler Lee
  • 2,736
  • 13
  • 24
1

As indicated by the website: NumberFormatException, the exception is caused by trying to convert a string that does not represent a number, to an integer, in this case: "done"

The exception is triggered in this line:

total = total + Integer.parseInt(userInput);

You can avoid and handle this exception as follows:

public class repeat{
    public void numberReader(){
        Scanner myScanner = new Scanner(System.in);
        int total = 1;
        int numberInputs = 1;
        String userInput;

        boolean done = false; // Stop when true

        do {
            System.out.println("Enter a number: ");
            userInput = myScanner.nextLine();
            try{        // Managing the exception
                total = total + Integer.parseInt(userInput);
            } catch(NumberFormatException e){
                if ("done".equals(userInput)){  // it's done!!!!
                    done = true;
                }
            }
            numberInputs++;
        } while (!done);
            System.out.println(total + "" + numberInputs + "" + (total / numberInputs));
        }

    public static void main(String[] args){
        repeat instance = new repeat();
        instance.numberReader();
    }
}

Read more about the try / catch

ErisoHV
  • 367
  • 1
  • 6
  • 15
1

The problem with your code is in this line

total = total + Integer.parseInt(userInput);

Suppose user enters the String "done".

now your code will try to parse the string to integer i.e.

total=total+Integer.parseInt("done");

Therefore , NumberFormatException will occur.

The correct code will be

import java.util.Scanner;

public class repeat{
    public void numberReader(){
        Scanner myScanner = new Scanner(System.in);
        int total = 1;
        int numberInputs = 1;
        String userInput;



        while (true)
        {
            System.out.println("Enter a number: ");
            userInput = myScanner.nextLine();
            if("done".equals(userInput))
            {
                System.out.println(total + "" + numberInputs + "" + (total / numberInputs));
                break;
            }
            else
            {
                total = total + Integer.parseInt(userInput);
                numberInputs++;
            }
        }
    }

    public static void main(String[] args){
        repeat instance = new repeat();
        instance.numberReader();
    }
}
Tyler Lee
  • 2,736
  • 13
  • 24
Karan
  • 448
  • 4
  • 9
0

you need to check if input is "done" before using Integer.parseInt(userInput)

        do {
            System.out.println("Enter a number: ");
            userInput = myScanner.nextLine();
            if(!"done".equals(userInput)){
            total = total + Integer.parseInt(userInput);
            numberInputs++;
            }

        } while (!"done".equals(userInput));
            System.out.println(total + "" + numberInputs + "" + (total / numberInputs));
        }
Amir Elsaeed
  • 81
  • 1
  • 12