0

I am trying to take the user input using a scanner, and double any number within the userInput. For example: I have no clue what to do 22 should outprint as: I have no clue what to do 44

When I try to use the parseInt I am unsure how to go about it to outprint my result .

public class CopyOfTester
{
    public static void main(String args[])
    {
        boolean Bool = true;
        do{
            Scanner kbReader = new Scanner(System.in);
            System.out.println("Please type in a sentence, or type EXIT to end the program.");
            String UserIn = kbReader.nextLine();

            if (UserIn.equals("EXIT")){
                Bool=false;
            }
            else{
                String sp[] = UserIn.split("\\d+");
                sp = UserIn.split(" ");  
                String lemon;
                int nu;
                for (int i = 0; i >= sp.length; i++){
                    nu= Integer.parseInt(sp[i])*2;
                    nu = nu * 2;

                }

                System.out.println(nu );
            }
        }while (Bool == true);
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110

2 Answers2

0

There is a number of problems in the else branch:

  • You need to know that you found a (stringified) integer before you parse is. Use a matcher.
  • If your string array is larger than 0, the parse loop is never entered. Use < instead of >=.
  • To print the whole input, manipulate the string array; do not try to use an int for printing strings.
  • Define your string array only once.
  • Lemon tastes good, but does not help here :)

This works:

String sp[] = userIn.split(" ");
Pattern p = Pattern.compile("-?\\d+");
for (int i = 0; i < sp.length; i++) {
    Matcher m = p.matcher(sp[i]);
    while (m.find()) {
        sp[i] = String.valueOf(Integer.parseInt(m.group()) * 2);
    }
}
Arrays.stream(sp).forEach(string -> System.out.print(string + " "));
System.out.println();
kalabalik
  • 3,792
  • 2
  • 21
  • 50
0
  1. You need a loop to continue the execution until the user inputs EXIT.
  2. You can match and replace an integer using a regex.

Demo:

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        Scanner kbReader = new Scanner(System.in);
        String userIn;

        Pattern pattern = Pattern.compile("\\b\\d+\\b");// String of one or more digits bounded by word-boundary

        while (true) {
            System.out.print("Please type in a sentence, or type EXIT to end the program: ");
            userIn = kbReader.nextLine();

            if ("EXIT".equalsIgnoreCase(userIn)) {
                break;
            }

            StringBuilder sb = new StringBuilder();
            Matcher matcher = pattern.matcher(userIn);
            int lastProcessedIndex = 0;

            while (matcher.find()) {
                String number = matcher.group();
                int startIndex = matcher.start();

                // Append the string starting from the last processed index and then append the
                // doubled integer
                sb.append(userIn.substring(lastProcessedIndex, startIndex))
                        .append(String.valueOf(2 * Integer.parseInt(number)));

                // Update the last processed index
                lastProcessedIndex = startIndex + number.length();
            }
            // Append the remaining substring
            sb.append(userIn.substring(lastProcessedIndex));

            System.out.println("The updated string: " + sb);
        }
    }
}

A sample run:

Please type in a sentence, or type EXIT to end the program: hello 10 world 5 how are 15 doing?
The updated string: hello 20 world 10 how are 30 doing?
Please type in a sentence, or type EXIT to end the program: exit
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110