简体   繁体   中英

scanner won't scan

I am having trouble coding a hw program that is made to generate test with multiple choice and essay questions. Everything works except my program skips lines when it goes to read a part of the essay class. I know it has to do with the scanner and scan.nextline, scan.nextInt and scan.next, etc but I am confused on how exactly to fix it.

Thank you for your help.

import java.util.*;

public class TestWriter
{
    public static void main (String [] args)
    {
        Scanner scan = new Scanner (System.in);
        String type=null;
        System.out.println ("How many questions are on your test?");
        int num = scan.nextInt ();
        Question [] test = new Question [num];
        for (int i=0; i <num; i++)
        {
            System.out.println ("Question " + (i+1) + ": Essay or multiple choice question? (e/m)");
            type = scan.next ();
            scan.nextLine ();
            if (type.equals ("e"))
            {
                test [i] = new Essay ();
                test [i].readQuestion ();
            }
            if (type.equals ("m"))
            {
                test [i] = new MultChoice ();
                test [i].readQuestion ();
            }
        }

        for (int i=0; i <num; i++)
        {
            System.out.println ("Question " + (i+1)+": "+ type);
            test [i].print ();
        }
    }
}

here is the essay class

public class Essay extends Question
{
    String question;
    int line;
    public void readQuestion ()
    {
        System.out.println ("How many lines?");
        line = scan.nextInt ();
        scan.next ();
        System.out.println ("Enter the question");
        question = scan.nextLine ();
    }
    public void print ()
    {
        System.out.println (question);
        for (int i=0; i <line; i++)
        System.out.println ("");
    }
}

Using scan.nextInt() will generate the following problems If your input is "5 5", nextInt() will get the next integer leaving the remaining " 5" of the buffer line. Of which the remaining " 5" will be caught by

type = scan.next();

In the class test writer:

  System.out.println("How many questions are on your test?");
  int num = scan.nextInt();
  Question[] test = new Question[num];  for(int i=0; i<num; i++)
  {
   System.out.println("Question " + (i+1) + ": Essay or multiple choice question? (e/m)");

    type = scan.next();

This will generate the issue as i have mentioned above.

To fix this you can either

a) Ensure that input is solely a number

b) Get the entire line like so String temp = scan.nextLine(); then convert it to a integer. This will you can play with the string and check if its the input you require ie if the 1st letter / set of numerical digits is an e/m or an integer.

The problem with scan.nextInt() is that it only gets the next integer of the input line. If there are spaces after the input it was taken from ie "5 5" it will grab only the next int 5 and leave " 5" behind.

Thus i would recommend using scan.nextLine() and manipulating the string to ensure that the input can be handled and verified and at the same time ensuring that you do not get confused of where the scanner is at.

You should use .next() / .nextInt() if you are handling an input with various parameters you want to specifically catch such as "25 Male Student 1234" in this case the code would be as such

int age = scan.nextInt(); 
String sex = scan.next(); 
String job = scan.next(); 
int score = scan.nextInt();

Your readQuestion function should be ...

public void readQuestion()
 {
  System.out.println("How many lines?");
  line = scan.nextInt();
  scan.nextLine();
  System.out.println("Enter the question");

  question = scan.nextLine();

 }

It should be scan.nextLine(); to add an empty new line at the end

In your TestWriter.main() method what are you expecting at 3 line in following code:

System.out.println("Question " + (i+1) + ": Essay or multiple choice question? (e/m)");

type = scan.next();
scan.nextLine();     //LINE 3: What are you expecting user to enter over here.

the control flow will stuck at this point unless you enter something on the console.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM