Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Displaying 2D array

807600Oct 31 2007 — edited Nov 1 2007
So, I'm so sorry for being such a major pain in the butt. I am new, please forgive me.

this will hopefully be the last question I ask for a while... but it is the most broad.

I'm trying to display a quarterly sales report that I have in a 2D array, but I have stalemated.
Am I approaching this from the wrong angle? I have all my methods working nicely, except for the dispReport method.

Here is the code for my program
import java.util.Scanner;
import java.io.*;

public class QSales
{


	public static void main (String[] args) throws IOException
	{

		boolean blnValid = false, blnErrorFree = true;
		int intSelection;
		final int ROWS = 20, COLS = 5;
		String[][] strSalesArray = new String[ROWS][COLS];


		System.out.println("Compass Corporation Quarterly Sales Report\n" +
							"Programmed by: Craig J. Ward");

		do
		{
			intSelection = dispMenu();

			if(intSelection == 1)
			{
				//Get data from QSales.txt
				strSalesArray = getArray(ROWS,COLS);
				blnErrorFree = editSales(strSalesArray);

				blnValid = true;
			}else if(intSelection == 2)
			{
				if(blnValid == false)
				{
					System.out.println("You must validate the quarterly sales file before displaying the report");
					intSelection = dispMenu();
				}else if(blnErrorFree == false)
				{
					System.out.println("Please correct errors and re-validate before continuing");
					intSelection = dispMenu();
				}
				if((blnValid == true) && (blnErrorFree == true))
				{
					dispReport(strSalesArray,ROWS,COLS);
				}

			}else
			{
				System.out.println("Selection must be 1,2, or 3");
				intSelection = dispMenu();
			}

		}while(intSelection != 3);
	}

	public static int dispMenu ()
	{

		Scanner keyboard = new Scanner(System.in);

		System.out.print(	"\n\t\t1. Validate Quarterly Sales" +
							"\n\n\t\t2. Display Sales Report" +
							"\n\n\t\t3. Exit Program" +
							"\n\nPlease make a selection:");
		int intSelection = keyboard.nextInt();

		while(intSelection > 3 && intSelection <= 0)
		{
			System.out.print("\nYou have selected an invalid option. Please re-enter a valid selection: ");
			intSelection = keyboard.nextInt();
		}


		return intSelection;

	}

	/** getArray reads the data from the file puts it into an array and returns it to main
	@param ROWS The amount of rows in our array
	@param COLS The amount of columns in our array
	@returns strSalesArray Our array
	*/
	public static String[][] getArray(int ROWS, int COLS) throws IOException
	{
		FileReader freader = new FileReader("QSales.txt");
		BufferedReader inputFile = new BufferedReader(freader);

		int intArraySize = 100;
		int i = 0,j = 0;
		String [][] strSalesArray = new String[ROWS][COLS];

		char[] chrRegion = new char[intArraySize];
		String input = "";



		input = inputFile.readLine();

		//if( i > chrRegion.length)
		//	System.out.println("i too big");
		//if(inputFile.readLine() == null)
		// System.out.println(inputFile.readLine());
		//inputFile.readLine();
		//chrRegion[i] = inputFile.readLine();
		//System.out.println(chrRegion.length + "\n" + i);

		for(i = 0;i <= strSalesArray.length - 1; i++)
		{

			for(j = 0;j <= 2; j++)
			{
					strSalesArray[i][j] = input;
					input = inputFile.readLine();
					System.out.println(strSalesArray[i][j]);
			}


		}
		return strSalesArray;
	}


	public static boolean editSales(String[][] strSalesArray)
	{
		boolean blnValid = true;

		for(int i = 0; i<= strSalesArray.length - 1;i++)
		{
			for(int j = 0;j <= 2;j++)
			{
				if(j == 0)
				{
					if(Character.isLetter(strSalesArray[i][j].charAt(0)))
						strSalesArray[i][j].toUpperCase();

					if(	(strSalesArray[i][j].charAt(0) != 'N') &&
						(strSalesArray[i][j].charAt(0) != 'E') &&
						(strSalesArray[i][j].charAt(0) != 'S') &&
						(strSalesArray[i][j].charAt(0) != 'W'))
					{
						System.out.println("Value " + strSalesArray[i][j].charAt(0) + " must be N,E,S, or W");
					}

				}

				if(j == 1)
				{
					if(Character.isDigit(strSalesArray[i][j].charAt(0)))
					{
						if((Double.parseDouble(strSalesArray[i][j]) > 4) || (Double.parseDouble(strSalesArray[i][j]) < 1))
						{
							System.out.println("Value " + strSalesArray[i][j] + " must be 1,2,3, or 4");
							blnValid = false;
						}

					}
					else
					{
						System.out.println("Value " + strSalesArray[i][j] + " must be 1,2,3, or 4");
						blnValid = false;

					}
				}

			}

		}

		return blnValid;
	}

	public static void dispReport(String[][] strSalesArray, int ROWS, int COLS)
	{
		String strDivision[] = new String[4], strFormatted[][] = new String[ROWS + 1][COLS +1];
		for(int i = 0; i < ROWS -1; i++)
		{
			//for(int j = 0; j < COLS -1; j++)
			//{
				if(/*(j == 0) &&*/ (strSalesArray[0] == "N"))
strFormatted[0][Integer.parseInt(strSalesArray[i][1])] = strSalesArray[i][2];

if(/*(j == 0) &&*/ (strSalesArray[i][0] == "E"))
strFormatted[1][Integer.parseInt(strSalesArray[i][1])] = strSalesArray[i][2];

if(/*(j == 0) &&*/ (strSalesArray[i][0] == "S"))
strFormatted[2][Integer.parseInt(strSalesArray[i][1])] = strSalesArray[i][2];

if(/*(j == 0) &&*/ (strSalesArray[i][0] == "W"))
strFormatted[3][Integer.parseInt(strSalesArray[i][1])] = strSalesArray[i][2];

//System.out.print(strFormatted[i][j] + "\t");
//}
//System.out.println("\n");
}

/*
for(int i = 0; i < ROWS -1; i++)
{
for(int j = 0; j < COLS -1; j++)
{

System.out.print(strFormatted[i][j] + "\t");
}
System.out.println("\n");
}
*/
}
}
If someone could give me an idea of how to logically structure my dispReport method I would be tremendously appreciative.

Thanks!                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 29 2007
Added on Oct 31 2007
22 comments
215 views