Hi I am new to programming and java.
I don’t know why:
- The checking of the empty string (the displaying of the ‘Error. Please enter a name.’ message) only works when the outer do-while loop is not present, but the checking of the time works.
- I also put the name = “”;
time = -1.0;
at the start of the do-while loop again to restart the variables since the input for the name would get skipped over since it has a string already from the first time the code is run, but is there another way to restart the variables properly or to improve my empty string checking so i don’t have to do this roundabout method?
Would appreciate any help.
import java.util.*;
public class programTest
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String name = "";
double time = -1.0;
char moreDataChoice = 'a';
boolean moreData = true;
do
{
name = "";
time = -1.0;
System.out.println("Name:");
while (name.isEmpty() == true)
{
name = sc.next();
if (teamName.isEmpty() == false)
{
break;
}
else
{
System.out.println("Error. Please enter a name.");
}
}
System.out.println("Time:");
do
{
time = sc.nextDouble();
sc.nextLine();
if (time >0)
{
break;
}
else
{
System.out.println("Error. Please enter a appropriate time");
}
} while (time <0.0);
System.out.println("Enter more data? (y/n)");
do
{
moreDataChoice = sc.next().charAt(0);
if (moreDataChoice == 'y')
{
moreData = true;
}
else if (moreDataChoice == 'n'
{
moreData = false;
break;
}
else
{
System.out.println("Error. Please enter y or n.");
}
} while ((moreDataChoice != 'y') && (moreDataChoice != 'n') );
} while (moreData == true);
sc.close();
}
}
- The checking of the empty string (the displaying of the ‘Error. Please enter a name.’ message) only works when the outer do-while loop is not present, but the checking of the time works.
- I also put the name = “”;
time = -1.0;
at the start of the do-while loop again to restart the variables since the input for the name would get skipped over since it has a string already from the first time the code is run, but is there another way to restart the variables properly or to improve my empty string checking so i don’t have to do this roundabout method?
New contributor