[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!!