Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: No idea why this program doesn't work

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.



    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; 
    		}
     
    	}
    }
    Last edited by Admin; November 7th, 2012 at 12:48 PM. Reason: Syntax highlighting


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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)

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Program works fine but with friend it doesn't work
    By Rizza in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 1st, 2011, 01:20 PM
  2. Replies: 6
    Last Post: November 25th, 2011, 03:58 PM
  3. Replies: 3
    Last Post: October 4th, 2011, 09:03 PM
  4. The program working fine but the "NO" button doesn't work
    By ahcenpg in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 1st, 2011, 07:32 PM
  5. [SOLVED] New at Java... my if, else if, else program doesn't seem to work, skips to else.Help!
    By KevinE in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 1st, 2010, 03:51 PM