java.util.scanner - Why am I getting the error, "java.util.NoSuchElementException"? -


this question has answer here:

my error message looks this:

exception in thread "main" java.util.nosuchelementexception: no line found     @ java.util.scanner.nextline(scanner.java:1540)     @ gpa3.answer(gpa3.java:364)     @ gpa3.main(gpa3.java:355) 

i have main method , rest of code working once add piece of code throws error. not allow me enter scanner.

import java.util.scanner; public class gpa3 {      public static void main(string[] args)      {         system.out.println("hello! welcome gpa calculator.");          int math;         double mathgpa = 0;          system.out.println("math = ");         scanner input = new scanner(system.in);         math= input.nextint();          if (math >100){             system.out.println("you have mistyped something");         }         if (math >= 94){             system.out.println("you have a");             mathgpa = 4.0;             system.out.println(mathgpa);         }         if (math < 94 && math >=90){             system.out.println("you have a-");             mathgpa = 3.7;             system.out.println(mathgpa);         }         if (math < 90 && math >=87){             system.out.println("you have b+");             mathgpa = 3.3;             system.out.println(mathgpa);         }         if (math < 87 && math >=80){             system.out.println("you have b");             mathgpa = 3.0;             system.out.println(mathgpa);         }         if (math < 80 && math >=77){             system.out.println("you have b-");             mathgpa = 2.7;             system.out.println(mathgpa);         }         if (math < 77 && math >=73){             system.out.println("you have c+");             mathgpa = 2.3;             system.out.println(mathgpa);         }         if (math < 73 && math >=70){             system.out.println("you have c");             mathgpa = 2.0;             system.out.println(mathgpa);         }         if (math < 70 && math >=67){             system.out.println("you have c-");             mathgpa = 1.7;             system.out.println(mathgpa);         }         if (math < 67 && math >=67){             system.out.println("you have d+");             mathgpa = 1.3;             system.out.println(mathgpa);         }         if (math < 67 && math >=63){             system.out.println("you have d");             mathgpa = 1.0;             system.out.println(mathgpa);         }         if (math < 63 && math >=60){             system.out.println("you have d-");             mathgpa = 0.7;             system.out.println(mathgpa);         }         if (math < 60){             system.out.println("you have f");             mathgpa = 0.0;             system.out.println(mathgpa);         }//end of math          int english;         double englishgpa = 0;         system.out.println("english = ");         english= input.nextint();          if (english >100){             system.out.println("you have mistyped something");         }         if (english >= 94 && english <101){             system.out.println("you have a");             englishgpa = 4.0;             system.out.println(englishgpa);         }         if (english < 94 && english >=90){             system.out.println("you have a-");             englishgpa = 3.7;             system.out.println(englishgpa);         }         if (english < 90 && english >=87){             system.out.println("you have b+");             englishgpa = 3.3;             system.out.println(englishgpa);         }         if (english < 87 && english >=80){             system.out.println("you have b");             englishgpa = 3.0;             system.out.println(englishgpa);         }         if (english < 80 && english >=77){             system.out.println("you have b-");             englishgpa = 2.7;             system.out.println(englishgpa);         }         if (english < 77 && english >=73){             system.out.println("you have c+");             englishgpa = 2.3;             system.out.println(englishgpa);         }         if (english < 73 && english >=70){             system.out.println("you have c");             englishgpa = 2.0;             system.out.println(englishgpa);         }         if (english < 70 && english >=67){             system.out.println("you have c-");             englishgpa = 1.7;             system.out.println(englishgpa);         }         if (english < 67 && english >=67){             system.out.println("you have d+");             englishgpa = 1.3;             system.out.println(englishgpa);         }         if (english < 67 && english >=63){             system.out.println("you have d");             englishgpa = 1.0;             system.out.println(englishgpa);         }         if (english < 63 && english >=60){             system.out.println("you have d-");             englishgpa = 0.7;             system.out.println(englishgpa);         }         if (english < 60){             system.out.println("you have f");             englishgpa = 0.0;             system.out.println(englishgpa);         }//end of english          int science;         double scigpa = 0;         system.out.println("science = ");         science= input.nextint();          if (science >100){             system.out.println("you have mistyped something");         }         if (science >= 94 && science <101){             system.out.println("you have a");             scigpa = 4.0;             system.out.println(scigpa);         }         if (science < 94 && science >=90){             system.out.println("you have a-");             scigpa = 3.7;             system.out.println(scigpa);         }         if (science < 90 && science >=87){             system.out.println("you have b+");             scigpa = 3.3;             system.out.println(scigpa);         }         if (science < 87 && science >=80){             system.out.println("you have b");             scigpa = 3.0;             system.out.println(scigpa);         }         if (science < 80 && science >=77){             system.out.println("you have b-");             scigpa = 2.7;             system.out.println(scigpa);         }         if (science < 77 && science >=73){             system.out.println("you have c+");             scigpa = 2.3;             system.out.println(scigpa);         }         if (science < 73 && science >=70){             system.out.println("you have c");             scigpa = 2.0;             system.out.println(scigpa);         }         if (science < 70 && science >=67){             system.out.println("you have c-");             scigpa = 1.7;             system.out.println(scigpa);         }         if (science < 67 && science >=67){             system.out.println("you have d+");             scigpa = 1.3;             system.out.println(scigpa);         }         if (science < 67 && science >=63){             system.out.println("you have d");             scigpa = 1.0;             system.out.println(scigpa);         }         if (science < 63 && science >=60){             system.out.println("you have d-");             scigpa = 0.7;             system.out.println(scigpa);         }         if (science < 60){             system.out.println("you have f");             scigpa = 0.0;             system.out.println(scigpa);         }//end of science          int spanish;         double spangpa = 0;         system.out.println("spanish = ");         spanish= input.nextint();          if (spanish >100){             system.out.println("you have mistyped something");         }         if (spanish >= 94 && spanish <101){             system.out.println("you have a");             spangpa = 4.0;             system.out.println(spangpa);         }         if (spanish < 94 && spanish >=90){             system.out.println("you have a-");             spangpa = 3.7;             system.out.println(spangpa);         }         if (spanish < 90 && spanish >=87){             system.out.println("you have b+");             spangpa = 3.3;             system.out.println(spangpa);         }         if (spanish < 87 && spanish >=80){             system.out.println("you have b");             spangpa = 3.0;             system.out.println(spangpa);         }         if (spanish < 80 && spanish >=77){             system.out.println("you have b-");             spangpa = 2.7;             system.out.println(spangpa);         }         if (spanish < 77 && spanish >=73){             system.out.println("you have c+");             spangpa = 2.3;             system.out.println(spangpa);         }         if (spanish < 73 && spanish >=70){             system.out.println("you have c");             spangpa = 2.0;             system.out.println(spangpa);         }         if (spanish < 70 && spanish >=67){             system.out.println("you have c-");             spangpa = 1.7;             system.out.println(spangpa);         }         if (spanish < 67 && spanish >=67){             system.out.println("you have d+");             spangpa = 1.3;             system.out.println(spangpa);         }         if (spanish < 67 && spanish >=63){             system.out.println("you have d");             spangpa = 1.0;             system.out.println(spangpa);         }         if (spanish < 63 && spanish >=60){             system.out.println("you have d-");             spangpa = 0.7;             system.out.println(spangpa);         }         if (spanish < 60){             system.out.println("you have f");             spangpa = 0.0;             system.out.println(spangpa);         }//end of spanish          int religion;         double rgpa = 0;         system.out.println("religion = ");         religion= input.nextint();         input.close();          if (religion >100){             system.out.println("you have mistyped something");         }         if (religion >= 94 && religion < 101){             system.out.println("you have a");             rgpa = 4.0;             system.out.println(rgpa);         }         if (religion < 94 && religion >=90){             system.out.println("you have a-");             rgpa = 3.7;             system.out.println(rgpa);         }         if (religion < 90 && religion >=87){             system.out.println("you have b+");             rgpa = 3.3;             system.out.println(rgpa);         }         if (religion < 87 && religion >=80){             system.out.println("you have b");             rgpa = 3.0;             system.out.println(rgpa);         }         if (religion < 80 && religion >=77){             system.out.println("you have b-");             rgpa = 2.7;             system.out.println(rgpa);         }         if (religion < 77 && religion >=73){             system.out.println("you have c+");             rgpa = 2.3;             system.out.println(rgpa);         }         if (religion < 73 && religion >=70){             system.out.println("you have c");             rgpa = 2.0;             system.out.println(rgpa);         }         if (religion < 70 && religion >=67){             system.out.println("you have c-");             rgpa = 1.7;             system.out.println(rgpa);         }         if (religion < 67 && religion >=67){             system.out.println("you have d+");             rgpa = 1.3;             system.out.println(rgpa);         }         if (religion < 67 && religion >=63){             system.out.println("you have d");             rgpa = 1.0;             system.out.println(rgpa);         }         if (religion < 63 && religion >=60){             system.out.println("you have d-");             rgpa = 0.7;             system.out.println(rgpa);         }         if (religion < 60){             system.out.println("you have f");             rgpa = 0.0;             system.out.println(rgpa);         }//end of religion          answer();     }//end of main      public static double answer()         {//begin answer         double sclass=0;         system.out.println("do have gym semester?");         scanner input = new scanner(system.in);         string yes = "yes";         string answer = input.nextline();          if (answer.equals(yes)){          }         input.close();         return sclass;     }//end answer  } 

the issue trying open 2 different scanner objects on same stream - system.in - @ same time (both open simultaneously, causing issue when second scanner tries read stream.

what can make answer() function accept scanner object parameter , use instead of creating new 1 on same stream.

example -

    .... // lots of logic till here     }//end of religion      answer(input); }//end of main  public static double answer(scanner input)     {//begin answer     double sclass=0;     system.out.println("do have gym semester?");     string yes = "yes";     string answer = input.nextline();      if (answer.equals(yes)){      }     input.close();     return sclass; }//end answer 

you need remove input.close(); well, close underlying stream system.in .


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -