Home > database >  Java, how do you have a user enter their values in one line with no spaces?
Java, how do you have a user enter their values in one line with no spaces?

Time:10-01

I'm new to learning java. My current assignment is to have the user enter the values they want with an operator and come out with a result on one line with no spaces. I figured out most of the code, but I'm having trouble running it without adding the spaces. For example: 1 1 = 2 (This will work) 1 1 (this causes an error)

Here is my code:

import java.util.Scanner;

public class Mod2 {
    
    Scanner sc = new Scanner(System.in);
    static int value1;
    static int value2;
    static int result;
    String input = "";

    public static void main(String[] args){
        Mod2 m = new Mod2();
        
        System.out.println("Beginning Application");
        System.out.println("Enter expression like [value][operator][value2] no spaces");
        
        value1 = m.sc.nextInt();
        String input = m.sc.next();
        value2 = m.sc.nextInt();
        
        switch (input) {
        case " ":   
            result = (value1   value2);
            System.out.println(result);
            break;
        case "-":   
            result = (value1 - value2);
            System.out.println(result);
            break;
        case "*":   
            result = (value1 * value2);
            System.out.println(result);
            break;
        case "/":   
            result = (value1 / value2);
            System.out.println(result);
            break;
        case "%":   
            result = (value1 % value2);
            System.out.println(result);
            break;
        }
            
        System.out.println("Ending application");               
        System.exit(0);
    }
}

CodePudding user response:

You can split the input string by the operator then use the first half of that string as value 1 then second half as value 2.

input.split("-"); // you can use a regex expression here to check for the existence of the operator

 ...
    String input = m.sc.next();
    String[] operation = input.split("(?<=\\d)(?=\\D)|(?<=\\D)(?=\\d)");
    Integer value1 = Integer.parseInt(operation[0]);
    Integer value2 = Integer.parseInt(operation[2]);

    switch (operation[1]) {
      case " ":
        result = (value1   value2);
        break;
      case "-":
        result = (value1 - value2);
        break;
      case "*":
        result = (value1 * value2);
        break;
      case "/":
        result = (value1 / value2);
        break;
      case "%":
        result = (value1 % value2);
        break;
    }
    
    System.out.println(result);

That should let you input "2 2" and produce 4 as result

CodePudding user response:

For the nextXXX()-methods (except nextLine()) Scanner uses strings up to some delimiter as its tokens. By default, whitespace is used as delimiter. Since e. g. string "100 50" doesn't contain any whitespace, it's a single token. This token is not an int, therefore a call on nextInt() will fail.

What can you do? Either you read the whole line using nextLine(), then you'll have to parse it on your own.

Or, you use different delimiters. E.g.

m.sc.useDelimiter("[^0-9]"); // delimiter is any non-digit.
int value1 = m.sc.nextInt(); 
m.sc.useDelimiter("[0-9]");  // delimiter is any digit
String operator = m.sc.next();
m.sc.useDelimiter("[^0-9]"); // delimiter is any non-digit.
int value2 = m.sc.nextInt();

The delimiter is a regular expression. You can find more information on those in the documentation for Pattern.

EDIT: note, the used expressions won't work with negative numbers.

CodePudding user response:

I don't know if I understand but the problem occurs when there is space in the right operator?

You can simply remove the operator spaces before entering the switch, like this:

input = input.replaceAll(" ","");

switch (input) {...
  •  Tags:  
  • java
  • Related