0

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

4
  • what! I could not get you in the last few lines. Please, What is it again ? Commented Dec 26, 2013 at 15:56
  • You're asking how to find part of a string, then parse it into an Integer. What have you tried? What are you having trouble with? Commented Dec 26, 2013 at 15:58
  • You are not talking about casting a String into integer? Are you? Commented Dec 26, 2013 at 15:58
  • I'm pretty much after a point system. But, the point equals whatever you want. So, let's say I have 0 points, and I want 5000. I'd do give 5000, which would grant me 5000 points. Commented Dec 26, 2013 at 16:06

1 Answer 1

5

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.

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

2 Comments

Yes, this is what I needed, although is there a way to do it on the same line, instead of give, then points? Thanks
I see now what you wanted, see my Edit. This would take "give 500" as an expression and then add the 500 value to your counter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.