java.util.scanner - No Such Element - No Line Found (Java) -


i'm creating program prints summary of situation after interactive input has ended (ctrl - d). prints summary of average age , percentage of children have received vaccines after interactive input.

however, i'm receiving no line found error whenever press ctrl-d @ name:. compiler tells me error @ name = sc.nextline(); within while loop don't know causing error exactly.

public static void main(string[] args) {          string name = new string();         int age, num = 0, i, totalage = 0;         boolean vaccinated;         int numvaccinated = 0;         double average = 0, percent = 0, count = 0;          scanner sc = new scanner(system.in);              system.out.print("name: ");             name = sc.nextline();              system.out.println("name \"" + name + "\"");             system.out.print("age: ");             age = sc.nextint();             system.out.println("age " + age);             system.out.print("vaccinated chickenpox? ");             vaccinated = sc.nextboolean();              totalage += age;             num++;              if(vaccinated == true)             {                 count++;                 system.out.println("vaccinated chickenpox");             }             else             {                    system.out.println("not vaccinated chickenpox");             }          while(sc.hasnextline())         {                sc.nextline();             system.out.print("name: ");             name = sc.nextline();              system.out.println("name \"" + name + "\"");             system.out.print("age: ");             age = sc.nextint();             system.out.println("age " + age);             system.out.print("vaccinated chickenpox? ");             vaccinated = sc.nextboolean();              totalage += age;             num++;              if(vaccinated == true)             {                 count++;                 system.out.println("vaccinated chickenpox");             }             else             {                    system.out.println("not vaccinated chickenpox");             }         }          average = (double) totalage/num;          percent = (double) count/num * 100;          system.out.printf("average age %.2f\n", average);         system.out.printf("percentage of children vaccinated %.2f%%\n", percent);      } } 

you not correctly implement exit condition loop if ask me.

try this:

string input = ""; {     system.out.print("name: ");     name = sc.nextline();     [... input parameters ...]     sc.nextline();     system.out.print("do want enter child (y/n)? ");     input = sc.nextline(); } while (!input.equals("n")); 

this way can quit entering new persons without having enter strange command might lead error. furthermore, do-while loop helps reduce code, because don't have use same code twice, i.e., between scanner sc = new scanner(system.in); , while(sc.hasnextline()) in example.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -