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 2 of 2

Thread: Can anybody help me fix this, what I am supposed to do is at the top

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

    Default Can anybody help me fix this, what I am supposed to do is at the top

    [HTML][/HTML]import java.awt.Color;
    import java.awt.Font;
    import java.io.File;
    import java.util.Scanner;
     
    import javax.swing.JOptionPane;
    import javax.swing.UIManager;
    import javax.swing.plaf.FontUIResource;
     
    /********************************************************************
     * Written by: 
     * Period: 1
     * 
     * ASSIGNMENT: Write a program that lets the user randomly fill a 
     * seating chart with students names, which are read in from a data
     * file.  The seating chart should be stored in a 2-D array of Strings.
     * Your program should:
     *  - Ask the user how many rows and columns of desks are in the
     *  		classroom.
     *  - Declare the 2-D array for the number of rows & columns entered.
     *  - Ask the user for the name of the file that contains the students'
     *  		names.
     *  - Randomly fill the array with the students' names.
     *  - Print the array in a JOP window.  Your rows and columns should
     *  		be NEATLY lined up.  Here are some things to help you:
     *  		- One of the fonts that evenly prints all characters is 
     *  				Monospac821 DL
     *  		- I will not enter more than 5 columns
     *  		- The longest student name is 12 characters
     *  - If the number of students is greater than the number of desks
     *  		then you should print an appropriate message.  Your 
     *  		program should NOT go into an endless loop!
     *  
     *  Remember your main shouldn't contain code - just method calls.
     *  Your methods should fairly short.
     *  
     * HAND IN:  Make a jar file, put it in my hand in folder and print.
     * 
     * GRADING: 50 points)
     * 
     * _____  your program is correctly formatted (5 points)
     * _____  2-D arrays are used correctly (15 points) 
     * _____  methods are used correctly (10 points)
     * _____  your main does not contain much code (5 points)
     * _____  your seating chart is neatly printed (5 points)
     * _____  your program runs correctly (10 points) 
     * 
     *******************************************************************/
    public class SeatingChart
    {
    	public static void changeJOP()
    	{
    		// Here are the commands to change the color & fonts in
    		//	the showMessageDialog() window:
     
    		// The font of the message text
    		UIManager.put("Label.font", new FontUIResource
    				(new Font("Lucida Console", Font.BOLD, 28)));
    		// The color of the message text
    		UIManager.put("OptionPane.messageForeground",Color.black);
     
    		// The color of the panel 
    		UIManager.put("Panel.background",Color.yellow);
    		// The color around the outside of the panel
    		UIManager.put("OptionPane.background",Color.white);
     
     
    		// Buttons at bottom
    		UIManager.put("Button.background",new Color(192,0,0));
    		UIManager.put("Button.foreground", new Color(12,51,139));
    		UIManager.put("Button.font", new FontUIResource
    				(new Font("Monospac821 DL", Font.BOLD, 14)));
    	}
    	public static int rows()
    	{
    		int r= Integer.parseInt(JOptionPane.showInputDialog("Enter the number of rows"));
    		return r;
    	}
    	public static int columns()
    	{
    		int c= Integer.parseInt(JOptionPane.showInputDialog("Enter the number of columns"));
    		return c;
    	}
    	public static String enterFileName()
    	{
    		String file=JOptionPane.showInputDialog("Enter a file name");
    		return file;
    	}
    	public static int fillNames(String fileName, String[][] seats)
    	{
    		int count=0;
    		try{
    			Scanner inFile= new Scanner(new File(fileName));
     
     
     
    			while(inFile.hasNext()){
    				inFile.nextLine();
    				count++;
    			}
    			inFile.close();
    		}catch(Exception e){
    			JOptionPane.showMessageDialog(null,"error reading file: "+e);
    		}
    		try{
    			Scanner inFile= new Scanner(new File(fileName));
    			int max=(int)(Math.random()*seats.length);
    			int gro=(int)(Math.random()*seats[0].length);
    			while(inFile.hasNext()){
     
    				seats[max][gro]=inFile.nextLine();
    			}
    			inFile.close();
    		}catch(Exception e){
    			JOptionPane.showMessageDialog(null,"error reading file: "+e);
    		}
    		if(count<seats.length*seats[0].length);
    		return count;
    	}
     
    	public static void output(String seats[][])
    	{
    		String out="";
    		for(int r=0; r<seats.length; r++){
    			for(int c=0; c<seats[0].length; c++)
    				out+=seats[r][c];
    			out+="\n";
    		}
    		JOptionPane.showMessageDialog(null, out);
     
     
    	}
    	public static void emptyDesk(String[][] seats)
    	{
    		for(int r=0; r<seats.length; r++)
    		{
    			for(int c=0; c<seats[0].length; c++)
    				if(seats[r][c]==null)
    					seats[r][c]="Empty";	
    		}
    	}
    	public static void nameSpaces(String[][] seats)
    	{
    		for(int r=0; r<seats.length; r++)
    		{
    			for(int c=0; c<seats[0].length; c++)
    				while(seats[r][c].length()<15)
    					seats[r][c]+=" ";	
    		}
    	}
    	public static void main(String[] args)
    	{
    		changeJOP();
    		int row=rows();
    		int col=columns();
    		String[][] seats= new String[row][col];
    		String fileName = enterFileName();
    		fillNames(fileName, seats);
    		emptyDesk(seats);
    		nameSpaces(seats);
    		output(seats);
     
     
    	}
    }

    I am having trouble getting the random number generator to select all the students (data) from the text file, sometimes it selects 10 or 11 or 8 when there are 16 pieces of data. Thanks!!
    Last edited by peplo1214; February 18th, 2012 at 07:49 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Can anybody help me fix this, what I am supposed to do is at the top

    Do you have any specific questions?

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

  3. The Following User Says Thank You to Norm For This Useful Post:

    peplo1214 (February 18th, 2012)

Similar Threads

  1. Not able to read from a textfiel the way it's supposed to.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 15
    Last Post: January 27th, 2012, 04:11 AM
  2. Replies: 6
    Last Post: November 25th, 2011, 03:58 PM
  3. Not sure what I'm supposed to do...
    By colerelm in forum Java Theory & Questions
    Replies: 1
    Last Post: October 3rd, 2011, 11:13 PM
  4. I don't understand what I'm supposed to do
    By dmcettrick in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 11th, 2011, 09:34 AM
  5. I dont see why this program is not doing what it is supposed to
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 12th, 2011, 08:24 AM