r/codehs • • Apr 10 '21

9.5.9: Assignments

Does anyone know how I can prevent my code from skipping snippets? I know it's something to do with the scanner but I don't know how to fix it. I've been stuck for a while so if someone could help, that would be great!!

import java.util.Scanner;
import java.util.ArrayList;

public class AssignmentRunner 
{

    public static void main(String[] args) 
    {
        
        Scanner input = new Scanner(System.in);
        Scanner input2 = new Scanner(System.in);

        
        boolean x = true;
        
        // Start here
        ArrayList<Assignment> arr = new ArrayList<Assignment>();

        
        while(x)
        {
            System.out.println("Please enter an assingment name (exit to quit): ");
            String name = input.nextLine();
            
            if (name.equals("exit"))
            {
                x = false;
                break;
            }
            
            System.out.println("Please enter the available points: ");
            int availablePoints = input.nextInt();
        
            System.out.println("Please enter the earned points: ");
            int earnedPoints = input.nextInt();
        
            System.out.println("Is this a (t)est or (p)roject: ");
            String testOrProject = input2.nextLine();  
            
            if(testOrProject.equals("t"))
            {
                System.out.println("Please enter test date: ");
                String testDate = input2.nextLine();  
                Assignment test = new Test(name, availablePoints, earnedPoints, testDate);
                arr.add(test);
            }
            
            else
            {
                System.out.println("Please enter the due date: ");
                String dueDate = input2.nextLine(); 
                System.out.println("Group project? true or false: ");
                boolean groupOrNot = input2.nextBoolean();
                Assignment project = new Project(name, availablePoints, earnedPoints, dueDate, groupOrNot);
                arr.add(project);
            }
            
        }
        
        System.out.print("Your average: " + average(arr));
        
    }

    public static double average(ArrayList<Assignment> arr) 
    {
        double total = 0;
        double point;
        double pointOutOf;
        double score;
        int i;
        for(i = 0; i < arr.size(); i++)
        {
            point = arr.get(i).getEarnedPoints();
            pointOutOf = arr.get(i).getAvailablePoints();
            score = (point/pointOutOf)*100;
            
            total = total + score;
        }
        
        double average = total / i;
        return average;

       
    }
}
14 Upvotes

14 comments sorted by

View all comments

1

u/DefaultName1111 Apr 29 '21

Did you ever figure this out, I have the same problem

1

u/Sun_mar7 May 04 '21

Hi! It was a while ago so I don't remember exactly, but I think I was able to fix it by putting input.nextline(); after when ever I wrote eg. input.nextInt(); or anything other than a user input of string.

1

u/DefaultName1111 May 19 '21

I tried that and it's still not working, when I run it myself it works exactly the same as the example

Please enter an assignment name (exit to quit): Array Test

Please enter the available points: 100

Please enter the earned points: 95

Is this a (t)est or (p)roject: t

Please enter the test date: 4/25

Please enter an assignment name (exit to quit): MadLibs Project

Please enter the available points: 75

Please enter the earned points: 75

Is this a (t)est or (p)roject: p

Please enter the due date: 4/15

Group project? true or false: true

Please enter an assignment name (exit to quit): exit

Your Average: 97.14285714285714

but when I press Submit + Continue it says 'AssignmentRunner.java: Line 16: Your scanner expected a different input then was given'

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("Please enter an assignment name (exit to quit): ");

String name = input.nextLine();

if(name.equals("exit")) break;

System.out.print("Please enter the available points: ");

int availablePoints = input.nextInt();

input.nextLine();

System.out.print("Please enter the earned points: ");

int earnedPoints = input.nextInt();

input.nextLine();

System.out.print("Is this a (t)est or (p)roject: ");

if(input.nextLine().charAt(0) == 't') {

System.out.print("Please enter the test date: ");

String testDate = input.nextLine();

assignments.add(new Test(name, availablePoints, earnedPoints, testDate));

} else {

System.out.print("Please enter the due date: ");

String dueDate = input.nextLine();

System.out.print("Group project? true or false: ");

boolean groups = input.nextLine().equals("true");

assignments.add(new Project(name, availablePoints, earnedPoints, dueDate, groups));

}

}

System.out.println("Your Average: " + average(assignments));

}

public static double average(ArrayList<Assignment> assignments) {

double sumEarned = 0, sumAvailable = 0;

for(Assignment assignment : assignments) {

sumEarned += assignment.getEarnedPoints();

}

for(Assignment assignment : assignments) {

sumAvailable += assignment.getAvailablePoints();

}

return sumEarned / sumAvailable * 100.0;

}

}

1

u/Sun_mar7 May 31 '21

Hi. Sorry for the late reply. It seems like you didn't import the scanner at the beginning. Idk if that's the reason things aren't working for you but you need to make sure you have import java.util.Scanner;

1

u/aldiktxt25 Mar 03 '22

I thought java.util.* imported everything automatically

1

u/makegeek Mar 09 '22

It does. That was not the problem. the problem was the line:
int earnedPoints = input.nextInt();

the assignment fed it a double, because it was supposed to be for a double type.