Reputation: 301
Here's the part of code that isn't working properly, didn't want to paste all of it, it gives me "bad data" if i try to input 3.2 but works fine if I try for example 3. And all getters/setters ask for double, not int, debugger tells the same story, the moment i'd input 3.2 in would skin to printing bad data
double fastest = 1000;
double choice1timechoice;
boolean goodTime = false;
while(!(goodTime)) {
System.out.println("Enter time of the sprint:");
try {
choice1timechoice = scan.nextDouble();
if (choice1timechoice < fastest) {
fastest = choice1timechoice;
fastIndex = numSprints;
}
sprint[numSprints].setTime(fastest);
goodTime = true;
System.out.println("Do you wish to continue adding sprints? 1 for yes, 2 for no");
cont = scan.nextInt();
if (cont == 1) numSprints++;
}
catch (InputMismatchException ex)
{
System.out.println("Bad data");
System.out.println("Please try again \n");
scan.nextLine();
}
}
Upvotes: 3
Views: 341
Reputation: 137315
This appears to be a locale issue. Based on the comments, the default local decimal separator on OP's system is ','
rather than '.'
. Thus Scanner
by default refuses to recognize 3.2
and only recognizes 3,2
.
To make the Scanner
accept 3.2
, you can manually set its locale:
scan.useLocale(Locale.US);
Upvotes: 4
Reputation: 1743
As Hovercraft Says in the comments, the problem maybe with the .nextDouble&.nextInt()
as it stays in the same line.
you should go to next line to read another number via .nextLine()
after each .nextDouble()
and .nextInt()
event after .next()
if you use it in the future.
Here a testable example I created for you.
Scanner sc = new Scanner(System.in);
System.out.println("Enter double");
while(true){
System.out.println();
double s = sc.nextDouble();
sc.nextLine();
System.out.println("Enter int");
double i = sc.nextInt();
sc.nextLine();
System.out.println("Double="+s+", int="+i);
System.out.println("Wish to cont? 1-Yes, 2-No");
int j = sc.nextInt();
sc.nextLine();
if(j==1) continue;
else break;
}
Upvotes: 0