For my school, I have to create an add
function which can add 2 numbers.
Example: /add 6 4
Solution: 10
This is my code at the moment, which lets me do that, but only with at maximum /add 9 9
:
String stString = message_text;
String number = stString.replaceAll("[^0-9]", "");
int numArr[] = new int[2];
for (int i = 0; i<2; i++) {
numArr[i] = Integer.parseInt(String.valueOf(number.charAt(i)));
}
int sum = IntStream.of(numArr).sum();
I need a way to be able to make it that I could use /add 14 23
or so.
Why don't you use split like this:
String[] myValues = stString.split(" ");
And after that get the values by myValues[1] and myValues[2]
Can't write your homework for you, but look at your logic. If you only read a single character, you can only ever have a single digit.
Parse for multidigit inputs instead of using charAt()
. Take a few extra lines of code to collect and build your inputs. Try String.substring()
or String.split()
and work from there.
Since you are already using streams, you could make a 1-liner stream-only solution:
int sum = Stream.of(stString.split(" ")).
skip(1).
mapToInt(Integer::parseInt).
sum();
Your problem is that you are complicating it too much with replace()
, String.valueOf()
and IntStream.of(numArr).sum()
, and then you limit your program to read only one character, so your numbers will be always composed of only one digit.
Solution:
You just need to use .split()
method and pass to it a space as delimiter:
String stString = "/add 64 10";
int a = Integer.parseInt(stString.split(" ")[1]);
int b = Integer.parseInt(stString.split(" ")[2]);
System.out.println(a+b); //will print 74
The problem with your approach is that you confuse the index of the required numbers in the String
with their actual index in the String
. Essentially you want two numbers but you always look for two consecutive digits after your replacement.
One way to work around this would be to use a simple regular expression to fetch 1+ digit sequences and collect them.
For instance:
String[] test = {
"/add 1 2",
"/add 22 20"
};
Pattern digits = Pattern.compile("\\d+");
for (String s: test) {
// parsing after /add
String sequence = s.substring(4);
// matching digits
Matcher m = digits.matcher(sequence);
// finding as many numbers as possible - you can always hard-limit to 2 if needed
long sum = 0;
while (m.find()) {
// no need to check parsing, it will always be integer figures at this point
sum += Integer.parseInt(m.group());
}
System.out.println(sum);
}
Output
3
42
You can use split string instead and then convert the fragments into numbers:
String stString = "/add 14 56";
String[] fragments = stString.split(" ");
int sum = 0;
// Start from fragment 1 assuming that fragment 0 is "/add"
for (int i = 1; i < fragments.length; i++)
try {
sum += Integer.valueOf(fragments[i]);
} catch (Exception e) {
System.err.println("Something went wrong converting the strings into integers.");
System.err.println("Please check the input string.");
System.err.println("\n");
e.printStackTrace();
break;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.