No idea why this program doesn't work
I am writing this code for a simple program that would allow a place such as a DMV. It would have two lines and go through as shown in the code below. I just have no idea why its not even compiling let alone not working. Maybe I'm just over looking something simple because I've been looking at this code so much. Please if you can help at all, it would be much appreciated.
Code Java:
import java.util.Scanner;
import java.util.Queue;
import java.util.Random;
import java.util.LinkedList;
class Person {
Random generator = new Random();
int age = age();
int gender = gender();
int costumerID;
int testTime = testTime();
private int age() {
int age = generator.nextInt(50) + 16;
if (age <= 33)
return 0;
else
return 1;
}
private int testTime() {
int temp;
temp = age / 10;
return temp;
}
private int gender() {
int gender = generator.nextInt(2) + 1;
return gender;
// 1 is for female
}
}
public class DMV2 {
static Random generator = new Random();
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Queue renewal = new LinkedList();
Queue test = new LinkedList();
Person[][] desks = new Person[4][4];
Person[] tellers = new Person[2];
Person person = new Person();
double grade, gTotal = 0;
int totalFailed = 0, totalPassed = 0;
int costumer = 0, totalTests =0;
int renewed = 0;
System.out.println("Welcome to the DMV");
System.out.println("Please enter amount of people that entered DMV today: ");
int costumerTotal = input.nextInt();
person.costumerID = costumer + 1;
for (int i=0; i<= costumerTotal; i++){
lineChoice(renewal, test, person);
if (renewal.peek() != null){
for (int j=0; j <= tellers.length; j++){
if (tellers[j] == null){
tellers[j] = (Person) renewal.peek();
}
else{
System.out.println("Costumer: " + tellers[j] + "is done renewing.");
tellers[j] = null;
renewed++;
}
}
}
for (int x=0;x<= 4;x = x+2){
for (int y=0;y<=4;y++){
if (desks[x][y] == null){
desks[x][y] = (Person) test.peek();
test.remove();
}
else if (person.testTime == 0){
System.out.println("Costumer: " + person.costumerID + "is done with their test");
desks[x][y] = null;
totalTests = totalTests + 1;
grade = grade();
if (person.gender == 1)
grade = grade + 15.0;
if (grade <=50.0){
totalFailed++;
System.out.println("Sorry costumer: "+ person.costumerID + " you failed please come again another day.");
}
else{
totalPassed++;
System.out.println("Congradualtions! Costumer: " + person.costumerID + " you passed.");
}
gTotal = gTotal + grade;
}
else
person.testTime = person.testTime - 1;
}
}
}
results(totalFailed, totalPassed, costumerTotal, gTotal, totalTests, renewed);
}
private static void results(int totalFailed, int totalPassed,int costumerTotal,double gTotal,int totalTests, int renewed ) {
double gAverage;
gAverage = gTotal / totalTests;
System.out.println("Total number of costumers: " + costumerTotal);
System.out.println("Total number of people who renewed: " + renewed);
System.out.println(totalTests + " people took the test");
System.out.println(totalFailed + " people failed while " + totalPassed + " people passed.");
System.out.println("The average grade was: " + gAverage);
}
private static double grade() {
double temp = generator.nextDouble() * 100.0;
return temp;
}
private static Queue lineChoice(Queue renewal, Queue test, Person person){
int line = generator.nextInt(2) + 1;
if(line == 1){
renewal.add(person);
return renewal;
}
else{
test.add(person);
return test;
}
}
}
Re: No idea why this program doesn't work
When posting code, please use the highlight tags. Unformatted code is pretty much impossible to read.
What error are you getting? What line is it on?
Re: No idea why this program doesn't work
Sorry I'm new. The problem I am having right now it that it wont compile. I keep getting these errors when I try to run the program.
Exception in thread "main" java.util.NoSuchElementException
at java.util.LinkedList.remove(LinkedList.java:788)
at java.util.LinkedList.removeFirst(LinkedList.java:1 34)
at java.util.LinkedList.remove(LinkedList.java:481)
at DMV2.main(DMV2.java:74)
Re: No idea why this program doesn't work
What code is on that line? What are you trying to remove? What does the List contain at that point? You can step through this with a debugger or add some print statements to help answer those questions.