Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is in your <code>enterGradeNames()</code> method with:</p> <pre><code>input.next() </code></pre> <p>You have to first call <code>input.hasNext()</code>;</p> <p>From <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next%28%29" rel="nofollow noreferrer">documentation</a>:</p> <blockquote> <p>Throws: NoSuchElementException - if no more tokens are available.</p> </blockquote> <p><strong>EDIT:</strong> as per comments</p> <p>I am unable to reproduce the problem, but there are many unnecessary lines in your code, so try running this edited code and see whether it changes anything.</p> <pre><code>public class Grades { private int len; private String [] gradeNames; private int [] gradeArray; private int enterGradeNames(){ Scanner input = new Scanner(System.in); for (int i = 0; i &lt; len; i++){ System.out.println("Enter the type of grades you will be reporting: (" + (i + 1) + " of " + gradeArray.length + ")" ); gradeNames[i] = new String(input.next()); } return 0; } public Grades(int length){ this.len = length; gradeArray = new int[len]; gradeNames = new String[len]; } </code></pre> <p>It's not generally good choice to call non-static methods inside constructor as the object isn't finished yet. You could do this in a (factory) method instead:</p> <pre><code>public static Grades buildGrades(){ Scanner s = new Scanner(System.in); System.out.println("Enter size:"); int size = s.nextInt(); Grades grades = new Grades(size); grades.enterGradeNames(); return grades; } </code></pre> <p><strong>EDIT2:</strong></p> <p>I searched a bit and the problem might be with your closing of the <code>Scanner</code>. Because if you call close on the <code>Scanner</code>, it will look whether its stream implements <code>Closeable</code> and if so it will close it as well. I never thought <code>System.in</code> would be closeable, but <em>it is</em>.</p> <p>So the best option? Possibly use one <code>Scanner</code> for the whole program OR just don't close it if you dont want its stream to be closed. More can be read <a href="https://stackoverflow.com/questions/14142853/close-a-scanner-linked-to-system-in?lq=1">here</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload