Reputation: 11
if (diceValues == null || diceValues.length() == 0) return 0;
int temp;
int val = 0;
Scanner scanner = new Scanner(diceValues);
while (scanner.hasNext()) {
temp = scanner.useDelimiter(" ").nextInt();
if (temp == 1) val += 100;
if (temp == 5) val += 50;
}
diceValue is a string like this: "1 2 3 4 5", and the scanner always skips the last number. So the int val(value) always smaller than it should be.
Upvotes: 1
Views: 427
Reputation: 24
I think your code has no problem.I ran it this way and it returned me value 150.
import java.util.Scanner;
public class Prog1
{
public static void main(String[] args){
Prog1 p =new Prog1();
int value = p.mymethod();
System.out.println(value);
}
public int mymethod()
{
String diceValues = "1 2 3 4 5";
if (diceValues == null || diceValues.length() == 0) return 0;
int temp;
int val = 0;
Scanner scanner = new Scanner(diceValues);
while (scanner.hasNext()) {
temp = scanner.useDelimiter(" ").nextInt();
if (temp == 1) val += 100;
if (temp == 5) val += 50;
}
return val;
}
Upvotes: 1