0

So basically I am trying to figure out how to stop this program using Integer.valueOf(String s) or Integer.parseInt(String s) by typing "end" for the user input

import java.util.Scanner;

public class monkey1{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int sum = 0;
        int lol = 1;
        do { 
            System.out.print("Enter a number: ");
            lol = s.nextInt();
            sum += lol;
            if (lol > 0) {
                System.out.println("Sum is now: " + sum);
            }
        } while (lol>0);
    }
}

4 Answers 4

3

First - you need to change lol=sc.nextInt() to String tmp = s.nextLine()

Second - do

try {
   if (tmp.equals("END")) {
       break;  // This will exit do-while loop
   }
   lol = Integer.parseInt(tmp);
} catch (NumberFormatException e) {
  e.printStackTrace();
  // Here you can also exit loop by adding break. Or just ask user to enter new text.
}

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

1 Comment

can you help and suggest how to handle this stackoverflow.com/questions/62036791/…
1

you should learn how to use try/catch. Here we go:

String text = "";
    int lol = 1;
    do {
        System.out.print("Enter a number: ");
        text = s.nextLine();
        try {
            lol = Integer.valueOf(text);
        }catch (Exception e){
            System.out.println("Exit");
            System.exit(0);
        }

Comments

1

Here is your code

package Phone;

import java.util.Scanner;

public class monkey1{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int sum = 0;
        int hi = 1;
        do { 
            System.out.print("Enter a number: ");
            String lol = s.nextLine();
            if(lol.equals("end")) {
                break;
        }

        hi = Integer.parseInt(lol);

        sum += hi;
        if (hi > 0) {
            System.out.println("Sum is now: " + sum);
        }
    } while (hi > 0);
}

What I did here is I changed lol to hi for adding and took String input so that you can input end...

1 Comment

can you help and suggest how to handle this stackoverflow.com/questions/62036791/…
0

I would suggest checking if s.equals("end") before converting to int so something like this:

do { 
        System.out.print("Enter a number: ");
        String userValue = s.nextLine();
        if(userValue.equals("end")
            break;
        lol = Integer.parseInt(userValue);
        sum += lol;
        if (lol > 0){
            System.out.println("Sum is now: " + sum);
        }
    }while (lol>0);

2 Comments

Please note that this example will fail with NumberFormatException in case if user will enter text other than end and that is not a number.
Yes,important note @VladislavVarslavans , it was taken for granted that user will provide correct input.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.