0

I am doing an assignment on CodeHS(9.5.9 Assignments) and I am getting an error explained as "AssignmentRunner.java: Line 20: Your scanner expected a different input then was given."

The variable and input method are both double. The class that it is later inputted into accepts doubles. Scanner is properly initialized, I don't understand the issue.

import java.util.*;

public class AssignmentRunner {

    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        ArrayList<Assignment> assignments= new ArrayList<Assignment>();
        
        while(true){
            System.out.print("Enter the assignment's name (exit to quit): ");
                String name = input.nextLine();
                if(name.equals("exit")) break;
            System.out.print("Enter the due date: ");
                String dueDate = input.nextLine();
            System.out.print("How many points is assignment worth? ");
                double totPoints = input.nextDouble();         //No issue.

            System.out.print("How many points were earned? ");
                double earnedPoints = input.nextDouble();      //Issue here.
                
                input.nextLine();
            System.out.print("Is this a (T)est or a (P)roject? ");
                String assignmentType = input.nextLine();
                
            if(assignmentType.equals("T"))
            {
                System.out.print("What type of test is it? ");
                String testType = input.nextLine();
                Assignment a = new Test(name, dueDate, totPoints, earnedPoints, testType);
                assignments.add(a);
            }
            else
            {
                System.out.print("Does this project require(true/false) ...\nGroups?");
                boolean groups = input.nextBoolean();
                input.nextLine();
                System.out.print("A presentation? ");
                boolean presentation = input.nextBoolean();
                Assignment b = new Project(name, dueDate, totPoints, earnedPoints, groups, presentation);
                assignments.add(b);
            }
            input.nextLine();
        }
        
        printSummary(assignments);
    }

    // Print due date and score percentage on the assignment
    public static void printSummary(ArrayList<Assignment> assignments) {
       for(Assignment assignment : assignments){
            System.out.println(assignment.getName() + " - " + (assignment.getEarnedPoints()/assignment.getAvailablePoints()*100));
        }

       
    }
}
public class Assignment
{
    private String name;
    private String dueDate;
    private double availablePoints;
    private double earnedPoints;
    
    public Assignment(String name, String dueDate, double availablePoints, double earnedPoints){
        this.name = name;
        this.dueDate = dueDate;
        this.availablePoints = availablePoints;
        this.earnedPoints = earnedPoints;
    }
    
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name = name;
    }
    
    public String getDueDate(){
        return dueDate;
    }
    public void setDueDate(String dueDate){
        this.dueDate = dueDate;
    }
    
    public double getAvailablePoints(){
        return availablePoints;
    }
    public void setAvailablePoints(double avPts){
        availablePoints = avPts;
    }
    
    public double getEarnedPoints(){
        return earnedPoints;
    }
    public void setEarnedPoints(double ePnts){
        earnedPoints = ePnts;
    }
    
    
}
public class Project extends Assignment 
{
    private boolean groups;
    private boolean presentation;
    
    public Project(String name, String dueDate, double availablePoints, double earnedPoints, boolean hasGroups, boolean hasPresentation){
        super(name, dueDate, availablePoints, earnedPoints);
        groups = hasGroups;
        presentation = hasPresentation;
    }
    
    public boolean hasGroups(){
        return groups;
    }
    public void setGroups(boolean x){
        groups = x;
    }
    
    public boolean hasPresentation(){
        return presentation;
    }
    public void setPresentation(boolean x){
        presentation = x;
    }
}
public class Test extends Assignment 
{
    private String testType;
    
    public Test(String name, String dueDate, double availablePoints, double earnedPoints, String testType){
        super(name, dueDate, availablePoints, earnedPoints);
        this.testType = testType;
    }
    
    public String getTestType(){
        return testType;
    }
    public void setTestType(String x){
        testType = x;
    }
}

I tried to see if input.nextLine() after line 17 would help but the compiler is still focused on that single double earnedPoints = input.nextDouble(); I asked my teacher about it and he said it was some glitch/bug on CodeHS's part but I wanted to know if you guys could help.

Tried adding '.useLocale(Locale.US);' to the initial Scanner method but it did not help.

Sinosco
  • 11
  • 2
  • 1
    what is the value you entered in the terminal? – Farid Fakhry Nov 23 '22 at 14:25
  • My guess is that when you called `nextDouble();` first time (when you didn't have any problems) you provided some integer value like 20, but when you called `nextDouble();` again you provided some double value like `12.34`. Some Locales expect `,` and some `.` as *comma*. So if you wrote `.` try with `,` instead (or vice versa). – Pshemo Nov 23 '22 at 14:29
  • Possibly related? [Scanner double value - InputMismatchException](https://stackoverflow.com/q/17150627) – Pshemo Nov 23 '22 at 14:40
  • I'm coding this on CodeHS for my comp sci class so whatever code I make has to pass through the website's grader. When I run the code on their terminal it works fine, whether I enter integers or doubles. It's just when I try to have it graded that it tells me there is an error. I tried adding '.useLocale(Locale.US);' as per the post you attached but it's still repeating the same error message. – Sinosco Nov 24 '22 at 19:25

1 Answers1

0

The problem you are encountering is due to the type mismatch that happening from your nextLine, nextDouble, and nextBoolean lines. Keystrokes are stored in the keyboard buffer, which means hat sometimes the newline character gets left behind. To be able to access the next data, we have to consume this newline character with the Scanner's nextLine() statement before we can move forward.

I would suggest adding the last line of code in the main Assignment Runner after these two lines:

System.out.print("how many points is assignment worth? ");
double totPoints = input.nextDouble();
input.nextLine();  // Add this to consume the keyboard buffer

and again here:

System.out.print("A presentation? ");
boolean presentation = input.nextBoolean();
input.nextLine();  // Add this to consume the keyboard buffer.

Making these two changes to your code should align your inputs with the correct next Line/Double/Boolean methods.

What happened was that when a user types keystrokes, they are stored in the keyboard buffer, as mentioned above. Pressing the "Enter" key causes a newline character to be stored in this keyboard buffer. So when the prompt "How many points is the assignment worth? " takes place, if someone enters 10, then the number 10 gets saved to total Points with the next Double, HOWEVER, the newline character still remains. You need to consume this with next Line method so that the newline character gets used up. The same happens below with nextBoolean method.

Here is another Stack Overflow post that talks about this: Java NextBoolean() read from text file

Dana D
  • 1
  • 1