• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Challenging Java Array

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Ronbert

Member
Joined
Jan 30, 2010
Location
La Crosse, WI
Hey guys I'm having a little trouble on some home work. I've got the program working flawlessly but I noticed a portion of my code where it's essentially the same thing repeating three times and I think I'm missing a more efficient way to do this.

Code:
package arrays;

import javax.swing.JOptionPane;

public class BookProgram{
public static void main(String[] args)
{	 
String[][] books = new String[4][5];
String[][] patrons = new String[3][4];
	
	
String[] mainMenu = {"Print Books","Print Patrons","Book Info","EXIT"};  		
	 
int optionChosen = JOptionPane.showOptionDialog(
    		null 				// Center in window
            , "Choose an option (or X to quit)" // Message
            , "Choose Option"            	// Title in titlebar
            , JOptionPane.OK_CANCEL_OPTION  	// Option type
            , JOptionPane.PLAIN_MESSAGE 	// messageType
            , null                       	// Icon (none)
            , mainMenu           		// ARRAY text
            , "Print Books"    				// Default button's label
          );

switch (optionChosen)
{
case 0:
books = loadBookArray(books);
printBook(books);
break;

case 1:
patrons = loadPatronArray(patrons);
printPatrons(patrons);
break;

case 2://STARTING A NESTED SWITCH AND SOME IF STATEMENTS TO DEFINE BOOKS
int chosen = 0;
	
	books = loadBookArray(books);
	String[] bookInfo = {books[0][0],books[1][0],books[2][0],books[3][0]};
	int bookMenu = JOptionPane.showOptionDialog(
    		null 				// Center in window
            , "Choose an option (or X to quit)" // Message
            , "Choose Option"            	// Title in titlebar
            , JOptionPane.OK_CANCEL_OPTION  	// Option type
            , JOptionPane.PLAIN_MESSAGE 	// messageType
            , null                       	// Icon (none)
            , bookInfo           		// ARRAY text
            , "Choose Book"    				// Default button's label
          );
	
	
	switch (bookMenu){//ASK BECKY ABOUT HOW TO STOP THIS REPTETIVE CODE
	
	case 0:
		books = loadBookArray(books);
		chosen = askInfo(chosen);
		if (chosen==0){
			System.out.println("The author of "+books[0][0]+" is "+books[0][1]+".");
		}
		else if (chosen==1){
			System.out.println("The publication date of "+books[0][0]+" is "+books[0][2]+".");
		}
		else if (chosen==2){
			System.out.println("The patron ID of "+books[0][4]+" is "+books[0][3]+".");
		}
		else if (chosen==3){
			System.out.println("The patron name is "+books[0][4]+".");
		}
		
		break;
		
	case 1:
		books = loadBookArray(books);
		chosen = askInfo(chosen);
		if (chosen==0){
			System.out.println("The author of "+books[1][0]+" is "+books[1][1]+".");
		}
		else if (chosen==1){
			System.out.println("The publication date of "+books[1][0]+" is "+books[1][2]+".");
		}
		else if (chosen==2){
			System.out.println("The patron ID of "+books[1][4]+" is "+books[1][3]+".");
		}
		else if (chosen==3){
			System.out.println("The patron name is "+books[1][4]+".");
		}
		
		break;
		
	case 2:
		books = loadBookArray(books);
		chosen = askInfo(chosen);
		if (chosen==0){
			System.out.println("The author of "+books[2][0]+" is "+books[2][1]+".");
		}
		else if (chosen==1){
			System.out.println("The publication date of "+books[2][0]+" is "+books[2][2]+".");
		}
		else if (chosen==2){
			System.out.println("No one currently has this book checked out.");
		}
		else if (chosen==3){
			System.out.println("No one currently has this book checked out.");
		}
		
		break;
		
	case 3:
		books = loadBookArray(books);
		chosen = askInfo(chosen);
		if (chosen==0){
			System.out.println("The author of "+books[3][0]+" is "+books[3][1]+".");
		}
		else if (chosen==1){
			System.out.println("The publication date of "+books[3][0]+" is "+books[3][2]+".");
		}
		else if (chosen==2){
			System.out.println("The patron ID of "+books[0][4]+" is "+books[0][3]+".");
		}
		else if (chosen==3){
			System.out.println("The patron name is "+books[0][4]+".");
		}
		
		break;
	}//END NESTED SWITCH
break;

case 3:
System.out.println("EXIT");
System.exit(0);
break;

default:
System.out.println("EXIT");
System.exit(0);}//END OF CASE STATEMENTS
}//END MAIN


public static String[][] loadBookArray(String books[][]){
	books[0][0] = "Blue River";
	books[0][1] = "John Jones";
	books[0][2] = "10/12/2009";
	books[0][3] = "11";
	books[0][4] = "Ronald De Long";
	
	books[1][0] = "One Life";
	books[1][1] = "Susan Smith";
	books[1][2] = "04/02/2007";
	books[1][3] = "22";
	books[1][4] = "Sally Smith";
	
	books[2][0] = "The Heart";
	books[2][1] = "Howard Black";
	books[2][2] = "07/14/2003";
	books[2][3] = "N/A";
	books[2][4] = "N/A";
	
	books[3][0] = "Two Again";
	books[3][1] = "Wana Dith";
	books[3][2] = "12/31/2009";
	books[3][3] = "11";
	books[3][4] = "Ronald De Long";
	
	
	return books;
}//END OF METHOD




public static String[][] loadPatronArray(String patrons[][]){
	patrons[0][0] = "Ronald De Long";
	patrons[0][1] = "11";
	patrons[0][2] = "123 Cool Ave.";
	patrons[0][3] = "Fake Town, WI 90210";
	
	patrons[1][0] = "Sally Smith";
	patrons[1][1] = "22";
	patrons[1][2] = "620 Onalaska Park";
	patrons[1][3] = "Onalaska, WI 54650";
	
	patrons[2][0] = "Steve Menthil";
	patrons[2][1] = "33";
	patrons[2][2] = "546 State Road 33";
	patrons[2][3] = "Shelby, WI 54601";
	
	
	return patrons;
}//END OF METHOD





public static void printBook(String books[][]){
	System.out.printf("%-20s", "Title");
	System.out.printf("%-20s", "Author");
	System.out.printf("%-20s", "Publication Date");
	System.out.printf("%-20s", "Patron ID");
	System.out.printf("%-20s", "Patron Name");
	System.out.printf("\n");
	
	for (int row = 0; row<books.length;++row){
		//START COLUMN
		for (int column = 0; column<5; ++column){
			System.out.printf("%-20s",books[row][column]);
		}
		System.out.printf("\n");
	}
}//END METHOD



public static int askInfo(int returnNum){
	String[] infoMenu = {"Author","Publication Date","Patron ID","Patron Name"};
	returnNum = JOptionPane.showOptionDialog(
    		null 				// Center in window
            , "Choose an option (or X to quit)" // Message
            , "Choose Option"            	// Title in titlebar
            , JOptionPane.OK_CANCEL_OPTION  	// Option type
            , JOptionPane.PLAIN_MESSAGE 	// messageType
            , null                       	// Icon (none)
            , infoMenu           		// ARRAY text
            , "Author"    				// Default button's label
          );
	return returnNum;
}




public static void printPatrons(String patrons[][]){
	System.out.printf("%-20s", "Patron Name");
	System.out.printf("%-20s", "Patron ID");
	System.out.printf("%-20s", "Address");
	System.out.printf("%-20s", "City, State, Zipcode");
	System.out.printf("\n");
	
	for (int row = 0; row<patrons.length;++row){
		//START COLUMN
		for (int column = 0; column<4; ++column){
			System.out.printf("%-20s",patrons[row][column]);
		}
		System.out.printf("\n");
	}
}//END METHOD





}//END PROGRAM

Basically this is a library program we wrote with some hard coded array values. The issue I'm talking about is if you click the button "Book info" you get another set of buttons asking which book you want to know about. For that part I've made several "if (chosen==x)" statements that way what ever button I press displays the information wanted about the book.

I just feel that instead of having three hard coded if else statements I should somehow be able to shorten the code.

EDIT: This is the specific set of code I'm talking about...

Code:
	switch (bookMenu){//ASK BECKY ABOUT HOW TO STOP THIS REPTETIVE CODE
	
	case 0:
		books = loadBookArray(books);
		chosen = askInfo(chosen);
		if (chosen==0){
			System.out.println("The author of "+books[0][0]+" is "+books[0][1]+".");
		}
		else if (chosen==1){
			System.out.println("The publication date of "+books[0][0]+" is "+books[0][2]+".");
		}
		else if (chosen==2){
			System.out.println("The patron ID of "+books[0][4]+" is "+books[0][3]+".");
		}
		else if (chosen==3){
			System.out.println("The patron name is "+books[0][4]+".");
		}
		
		break;
		
	case 1:
		books = loadBookArray(books);
		chosen = askInfo(chosen);
		if (chosen==0){
			System.out.println("The author of "+books[1][0]+" is "+books[1][1]+".");
		}
		else if (chosen==1){
			System.out.println("The publication date of "+books[1][0]+" is "+books[1][2]+".");
		}
		else if (chosen==2){
			System.out.println("The patron ID of "+books[1][4]+" is "+books[1][3]+".");
		}
		else if (chosen==3){
			System.out.println("The patron name is "+books[1][4]+".");
		}
		
		break;
		
	case 2:
		books = loadBookArray(books);
		chosen = askInfo(chosen);
		if (chosen==0){
			System.out.println("The author of "+books[2][0]+" is "+books[2][1]+".");
		}
		else if (chosen==1){
			System.out.println("The publication date of "+books[2][0]+" is "+books[2][2]+".");
		}
		else if (chosen==2){
			System.out.println("No one currently has this book checked out.");
		}
		else if (chosen==3){
			System.out.println("No one currently has this book checked out.");
		}
		
		break;
		
	case 3:
		books = loadBookArray(books);
		chosen = askInfo(chosen);
		if (chosen==0){
			System.out.println("The author of "+books[3][0]+" is "+books[3][1]+".");
		}
		else if (chosen==1){
			System.out.println("The publication date of "+books[3][0]+" is "+books[3][2]+".");
		}
		else if (chosen==2){
			System.out.println("The patron ID of "+books[0][4]+" is "+books[0][3]+".");
		}
		else if (chosen==3){
			System.out.println("The patron name is "+books[0][4]+".");
		}
		
		break;
	}//END NESTED SWITCH

The purpose of this code here is to take the index of the book chosen, after that you will be asked what you'd like to know about the book, author, pub date, etc. You click on one of the buttons and it gives another index number which I feed to the if else statements. Then it prints out the information at book array [][].
 
Last edited:
Well, did you ask Becky? :D

I haven't coded in Java for a while, so excuse me if I forget terms or make obvious syntax mistakes.

I would create a separate subroutine to perform the outputting of the book info...

Code:
private sub output_book (my_books, selection) {
                books = loadBookArray(my_books);
		chosen = askInfo(selection);
		if (chosen==0){
			System.out.println("The author of "+books[0][0]+" is "+books[0][1]+".");
		}
		else if (chosen==1){
			System.out.println("The publication date of "+books[0][0]+" is "+books[0][2]+".");
		}
		else if (chosen==2){
			System.out.println("The patron ID of "+books[0][4]+" is "+books[0][3]+".");
		}
		else if (chosen==3){
			System.out.println("The patron name is "+books[0][4]+".");
		}
}

Then pop the subroutine call in there in place of the "repetitive switch".

Code:
output_books(books, chosen);
 
It's been a long time since I posted back in this, school heated up so much I was never able to revisit it. I did ask Becky and she made it pretty clear on how to eliminate the repetitive portion.

I actually replaced all those else if statements by simply reference the method I created and pulling all the information that way.
 
I was going to respond to this already earlier, but felt unsure on what your actual assignment requirements where... The structure you used was not very "javaish", but of course that could be due to an actual request?

Generally in Java you would not have arrays of arrays of Strings like you used, rather you would make a Book class and then store them in a list / array / map. However I do understand it's also common to to teach in this was in the beginning to get a better understanding of basic structures and how they work.

Nice to hear you got this to work, for next time I suggest the following:

- It's good that you mentioned it's home work, however consider actually posting the assignment text since homework will often expect you to be doing things differently from the real world since it's trying to teach some specific concept.
- Happy to see you solved it, and also that you posted back with some details on how you did it. Maybe consider posting the actual solution as that might help the next person struggling with a similar case.
 
Back