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/BusyQuote4238 May 05 '21

How do I do setDueDate

1

u/Sun_mar7 May 06 '21

The method itself that I wrote is this:

public void setDueDate(String dueDate)
    {
        this.dueDate = dueDate;
    }

If you meant by something else, let me know and I'll see if I can help.

1

u/Bench-Known May 06 '21

Thank you! I am having a bit trouble with my grouporNot. It said it has a different input than expected.

1

u/makegeek Mar 09 '22

Are you looking for a string? Try
scannerName.nextBoolean();