Duz Posted April 26, 2010 Share Posted April 26, 2010 I cant seem to get this to compile any one see why? or any way to tidy up the code. ///////////////////////////////////////////////////////////////////////////////// // / // Conor Hughes / // / // Group 5 / // / // / // Student Number: B00032375 / // / // / // Java Project; Option 2. Email Sort / // / ///////////////////////////////////////////////////////////////////////////////// class projectmail2{ public static void main (String[]args) { //declare variables int repeat; int a; a=0; int array [][] = new int [5][3]; // Declare and alocate array for(int r = 0; r < array.length; r++) //for loop to fill array { for(int c = 0; c < array[r].length; c++) { array[r][c] = (int)(Math.random()*10000%10 + 1); } } do{ System.out.println("Emails (Unsorted):"); //print to screen System.out.println("--------------------------------------"); //print to screen for aesthetics System.out.print("Date: From: Attachments:\n"); //print to screen displayArray(array); //display array (array) to screen /*__________________________________________________________________________*/ System.out.println("Select sort type:\n"); System.out.println("1.Sort by date.\n"); System.out.println("2.Sort by number of attachments.\n"); System.out.println("3.Sort by name.\n"); a =Keyboard.readInt(); if(a==1){ //if user selects 1 sortdate(array); // Function to sort array displayArray(array); //displays (array) to screen } else if (a==2){ sortattachment(array); //if user selects 2 displayArray(array);} else if (a==3){ sortname(array); //if user selects displayArray(array);} /*__________________________________________________________________________*/ System.out.println("REPEAT? 1= Yes. 2= No."); repeat=Keyboard.readInt(); }while (repeat==1); //while repeat = 1, re-run program } static void displayArray(int array[][]) // Display array function { System.out.println("Emails (Sorted):"); System.out.println("--------------------------------------"); //aesthetics System.out.print("Date: From: Attachments:\n"); for(int r = 0; r < array.length; r++) { System.out.println(); for(int c = 0; c < array[r].length; c++) { if(c == 0) { System.out.print(array[r][c] + " "); } if(c == 1) { System.out.print(getName(array[r][c])); } if(c == 2) { System.out.print(array[r][c] + " "); } } } System.out.print("\n\n\n\n"); } static String getName(int num) // Function to return name { String name = null; switch(num) //switch statement to return name. { case 1: name = "Arnie\t\t";break; case 2: name = "Paul\t\t";break; case 3: name = "Simon\t\t";break; case 4: name = "Robert\t\t";break; case 5: name = "Luke\t\t";break; case 6: name = "John\t\t";break; case 7: name = "Kerry\t\t";break; case 8: name = "Sue\t\t";break; case 9: name = "Pamela\t\t";break; case 10: name = "Sinead\t\t";break; } return (name); } static void sortname(int array [][]) // Function to sort array by name { int n=array.length; int temp[] = new int[3]; for(int pass=1; pass<n; pass ++) { //open for loop for( int i=0; i< n-pass; i++) { //open nested for loop if (array[1] > array[i+1][1] ){ temp[0] = array[0]; temp[1] = array[1]; temp[2] = array[2]; array[0] = array[0]; array[1] = array[1]; array[2] = array[2]; array[0] = temp[0]; array[1] = temp[1]; array[2] = temp[2]; } //close if statement } //close for loop } //close for loop } static void sortdate(int array [][]) // Function to sort array by date { int n=array.length; int temp[] = new int[3]; for(int pass=1; pass<n; pass ++) { for( int i=0; i< n-pass; i++) { if (array[0] > array[i+1][0] ){ temp[0] = array[0]; temp[1] = array[1]; temp[2] = array[2]; array[0] = array[0]; array[1] = array[1]; array[2] = array[2]; array[0] = temp[0]; array[1] = temp[1]; array[2] = temp[2]; } } } } static void sortattachment(int array [][]) // Function to sort array by attachments { int n=array.length; int temp[] = new int[3]; for(int pass=1; pass<n; pass ++) { for( int i=0; i< n-pass; i++) { if (array[2] > array[i+1][2] ){ temp[0] = array[0]; temp[1] = array[1]; temp[2] = array[2]; array[0] = array[0]; array[1] = array[1]; array[2] = array[2]; array[0] = temp[0]; array[1] = temp[1]; array[2] = temp[2]; } } } } } //end of program. Compilation= Success Thanks everyone Quote Link to comment Share on other sites More sharing options...
Sparda Posted April 26, 2010 Share Posted April 26, 2010 What is the error you receive? Also, this code references a class called Keyboard which is not a class in the JDK. Perhaps you have been supplied this class and the class path is not set correctly? Quote Link to comment Share on other sites More sharing options...
Duz Posted April 27, 2010 Author Share Posted April 27, 2010 I have the class thats not my problem. Is there a keyboard class thing in the JDK if so how do I use it. I will post the error when I get home. Quote Link to comment Share on other sites More sharing options...
Alias Posted April 30, 2010 Share Posted April 30, 2010 You know why it can't compile? Cause it's Java, ooooooh Java burn :) Quote Link to comment Share on other sites More sharing options...
Brains Posted May 1, 2010 Share Posted May 1, 2010 I took your code and made it run, I cleaned it up with the eclipse cleanup tools, so everything should be nice and formatted. The try/catch blocks need to be redone to compensate for the lack of an input if there isn't one, but this was just thrown together with the code you provided. You also don't need all those \n's inside the System.out.printlns, they already create new lines. If you want to use \n use System.out.print. You might want it that way, but in eclipse it looks very... spread out, I like all my information in as close as a group as I can get it. You can create a Keyboard class if you want that will handle the getting of ints and stuff, but I'm lazy and didn't want to create another class, so I just threw in some code I already had laying around. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class projectmail2 { // /////////////////////////////////////////////////////////////////////////////// // / // Conor Hughes / // / // Group 5 / // / // / // Student Number: B00032375 / // / // / // Java Project; Option 2. Email Sort / // / // /////////////////////////////////////////////////////////////////////////////// public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // declare variables int repeat = 0; int a; a = 0; int array[][] = new int[5][3]; // Declare and alocate array for (int r = 0; r < array.length; r++) // for loop to fill array { for (int c = 0; c < array[r].length; c++) { array[r][c] = (int) (Math.random() * 10000 % 10 + 1); } } do { System.out.println("Emails (Unsorted):"); // print to screen System.out.println("--------------------------------------"); // print // to // screen // for // aesthetics System.out.print("Date: From: Attachments:\n"); // print to screen displayArray(array); // display array (array) to screen /* __________________________________________________________________________ */ System.out.println("Select sort type:\n"); System.out.println("1.Sort by date.\n"); System.out.println("2.Sort by number of attachments.\n"); System.out.println("3.Sort by name.\n"); String input = null; try { input = br.readLine().toLowerCase(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } a = Integer.parseInt(input); if (a == 1) { // if user selects 1 sortdate(array); // Function to sort array displayArray(array); // displays (array) to screen } else if (a == 2) { sortattachment(array); // if user selects 2 displayArray(array); } else if (a == 3) { sortname(array); // if user selects displayArray(array); } /* __________________________________________________________________________ */ System.out.println("REPEAT? 1= Yes. 2= No."); try { input = br.readLine().toLowerCase(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } a = Integer.parseInt(input); } while (repeat == 1); // while repeat = 1, re-run program } static void displayArray(int array[][]) // Display array function { System.out.println("Emails (Sorted):"); System.out.println("--------------------------------------"); // aesthetics System.out.print("Date: From: Attachments:\n"); for (int r = 0; r < array.length; r++) { System.out.println(); for (int c = 0; c < array[r].length; c++) { if (c == 0) { System.out.print(array[r][c] + " "); } if (c == 1) { System.out.print(getName(array[r][c])); } if (c == 2) { System.out.print(array[r][c] + " "); } } } System.out.print("\n\n\n\n"); } static String getName(int num) // Function to return name { String name = null; switch (num) // switch statement to return name. { case 1: name = "Arnie\t\t"; break; case 2: name = "Paul\t\t"; break; case 3: name = "Simon\t\t"; break; case 4: name = "Robert\t\t"; break; case 5: name = "Luke\t\t"; break; case 6: name = "John\t\t"; break; case 7: name = "Kerry\t\t"; break; case 8: name = "Sue\t\t"; break; case 9: name = "Pamela\t\t"; break; case 10: name = "Sinead\t\t"; break; } return (name); } static void sortname(int array[][]) // Function to sort array by name { int n = array.length; int temp[] = new int[3]; for (int pass = 1; pass < n; pass++) { // open for loop for (int i = 0; i < n - pass; i++) { // open nested for loop if (array[i][1] > array[i + 1][1]) { temp[0] = array[i][0]; temp[1] = array[i][1]; temp[2] = array[i][2]; array[i][0] = array[i + 1][0]; array[i][1] = array[i + 1][1]; array[i][2] = array[i + 1][2]; array[i + 1][0] = temp[0]; array[i + 1][1] = temp[1]; array[i + 1][2] = temp[2]; } // close if statement } // close for loop } // close for loop } static void sortdate(int array[][]) // Function to sort array by date { int n = array.length; int temp[] = new int[3]; for (int pass = 1; pass < n; pass++) { for (int i = 0; i < n - pass; i++) { if (array[i][0] > array[i + 1][0]) { temp[0] = array[i][0]; temp[1] = array[i][1]; temp[2] = array[i][2]; array[i][0] = array[i + 1][0]; array[i][1] = array[i + 1][1]; array[i][2] = array[i + 1][2]; array[i + 1][0] = temp[0]; array[i + 1][1] = temp[1]; array[i + 1][2] = temp[2]; } } } } static void sortattachment(int array[][]) // Function to sort array by // attachments { int n = array.length; int temp[] = new int[3]; for (int pass = 1; pass < n; pass++) { for (int i = 0; i < n - pass; i++) { if (array[i][2] > array[i + 1][2]) { temp[0] = array[i][0]; temp[1] = array[i][1]; temp[2] = array[i][2]; array[i][0] = array[i + 1][0]; array[i][1] = array[i + 1][1]; array[i][2] = array[i + 1][2]; array[i + 1][0] = temp[0]; array[i + 1][1] = temp[1]; array[i + 1][2] = temp[2]; } } } } } // end of program. Compilation= Success Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.