Reputation: 149
alright so im wondering how can I read an integer and string from the same line? Ill give an example: if I have input=3K how can I make my output look like this: 3K=3000?
Upvotes: 1
Views: 1214
Reputation: 347194
Start by breaking down you requirements.
If you want a variable/flexible solution, where you can supply any type of modifier, you will need to determine the number of digits the user has entered and then the modifier.
Once you have that, you can split the String
, convert the digits to an int
and apply the appropriate calculations based on the modifier...
Scanner kb = new Scanner(System.in);
String input = kb.nextLine();
int index = 0;
while (index < input.length() && Character.isDigit(input.charAt(index))) {
index++;
}
if (index >= input.length()) {
System.out.println("Input is invaid");
} else {
String digits = input.substring(0, index);
String modifier = input.substring(index);
int value = Integer.parseInt(digits);
switch (modifier.toLowerCase()) {
case "k":
value *= 1000;
break;
//...
}
System.out.println("Expanded value = " + value);
}
Upvotes: 1
Reputation: 2664
String input = "3k";
String output = input.replaceAll("[Kk]", "000");
int outputAsInt = Integer.parseInt(output);
Upvotes: 0