user3128007
user3128007

Reputation: 37

Java Integer with String?

I was wondering if it's possible to make up a command, let's say we have a Scanner.

Here's example:

Scanner INPUT = new Scanner(System.in);
int IsPoints = 0;
String isValue = INPUT.nextLine();

if (isValue.equalsIgnoreCase("give"){
   isPoints += The int value you want here;
}

So my real question is, is it possible to increase the int value of points, via a String command?

The output, if it worked, would be give X (Amount of points you want = x)

So if I did give 5000 I'd get 5000 points, is that possible to do?

Thanks

Upvotes: 0

Views: 136

Answers (1)

everag
everag

Reputation: 7672

Are you looking for this?

Scanner input = new Scanner(System.in);
int points = 0;
String value = input.nextLine(); // give 500

String[] tokens = value.split(" ");

if (tokens[0].equalsIgnoreCase("give")) {
   points += Integer.parseInt(tokens[1]);
}

System.out.println(points); // 500

This would take give XXX as an expression and then add X to your counter.

Upvotes: 5

Related Questions