Fixing Scnanner scan.hasNext();, infinite loop Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ibo3
    New Member
    • Sep 2007
    • 8

    #1

    Fixing Scnanner scan.hasNext();, infinite loop Java

    /** I have to use the scanner class (no alternatives), to record a dynamic amount of grades. I use the hasNext() method to get the next value on the same line. For some reason when I am done entering numbers and I press enter the curser waites on the next line and does not exit the program. I want to be able to exit the program once I have entered multable grades and then pressed enter. */


    public void enterGrades()
    {
    Scanner scan = new Scanner(System. in);

    ArrayList<Strin g> al = new ArrayList<Strin g>();

    System.out.prin t("Enter grades: ");

    while(scan.hasN ext())
    {
    al.add(scan.nex t() );

    System.out.prin tln(al);
    }

    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by ibo3
    /** I have to use the scanner class (no alternatives), to record a dynamic amount of grades. I use the hasNext() method to get the next value on the same line. For some reason when I am done entering numbers and I press enter the curser waites on the next line and does not exit the program. I want to be able to exit the program once I have entered multable grades and then pressed enter. */


    public void enterGrades()
    {
    Scanner scan = new Scanner(System. in);

    ArrayList<Strin g> al = new ArrayList<Strin g>();

    System.out.prin t("Enter grades: ");

    while(scan.hasN ext())
    {
    al.add(scan.nex t() );

    System.out.prin tln(al);
    }

    }
    So you want to enter the grades on one line separated by a space?
    Don't use the while loop then. Just get the grades as one String using scanner.nextLin e(). You should visit the API docs for the Scanner class to understand the differences between the methods.
    Note that if you read them all as one string, you would need to split that string first to be able to enter each individual grade into the arraylist.

    Comment

    • ibo3
      New Member
      • Sep 2007
      • 8

      #3
      Originally posted by r035198x
      So you want to enter the grades on one line separated by a space?
      Don't use the while loop then. Just get the grades as one String using scanner.nextLin e(). You should visit the API docs for the Scanner class to understand the differences between the methods.
      Note that if you read them all as one string, you would need to split that string first to be able to enter each individual grade into the arraylist.

      Then how would I pull out each value from the arraylist?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by ibo3
        Then how would I pull out each value from the arraylist?
        See the API docs for the ArrayList class. It has all sorts of methods for putting and getting stuff from an ArrayList.
        Have you managed to add the grades individually into the ArrayList(not as one long string)?

        Comment

        • ibo3
          New Member
          • Sep 2007
          • 8

          #5
          No, I get one long string

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by ibo3
            No, I get one long string
            You can split that using the String.split method.
            Check it's specs in API docs again. It can give you an array containing the individual grades.

            Comment

            • ibo3
              New Member
              • Sep 2007
              • 8

              #7
              Originally posted by r035198x
              You can split that using the String.split method.
              Check it's specs in API docs again. It can give you an array containing the individual grades.

              thank you for your help!!!

              I will check into the String.split method

              Comment

              Working...