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!

Trouble With Arrays in an txt. reading/verifying/displaying program

843789Dec 7 2009 — edited Dec 7 2009
Hey, I'm getting back into Java and I'm working on an program assignment that uses arrays. Basically it's a program that allows the user to load all division?s quarterly sales statistics from a file called Quarterly Sales.txt, validate the data and produce a quarterly sales report. The contents of the sales report are something like this:
"N
1
35.50
N
2
26.99
N
3
77.45
N
4
58.30
S
1
132.15
S
2
81.19
S
3
159.06"
Etc. and it's going to display something like:
Q1 Q2 Q3 Q4
N
E
S
W

When you enter the program you're given three options 1. Verify text document 2. Display verified text document in a more readable format and 3. Exit
My problem seems to lie in the arrays, however, I don't know how or why but for whatever reason after verifying the .txt document and I go to display it it displays only 1 of the numbers that were actually found in the .txt and a bunch of nulls, would you guys please help me to pinpoint what is wrong with this program?

Here is the code:
import java.util.Scanner;
import java.io.*;

public class QuarterlySales
{


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

		boolean blnValid = false, blnNoError = true;
		int intChoice;
		int rows = 7, cols = 7;
		String[][] SalesArray = new String[rows][cols];


		System.out.println("Quarterly Sales Report\n");

		do
		{
			intChoice = dispMenu();

			if(intChoice == 1)
			{
				//Get data from QSales.txt
				SalesArray = takeArray(rows,cols);
				blnNoError = editSales(SalesArray);

				blnValid = true;
			}else if(intChoice == 2)
			{
				if(blnValid == false)
				{
					System.out.println("You must validate the quarterly sales file before displaying the report");
					intChoice = dispMenu();
				}else if(blnNoError == false)
				{
					System.out.println("Please correct errors and re-validate before continuing");
					intChoice = dispMenu();
				}
				if((blnValid == true) && (blnNoError == true))
				{
					dispReport(SalesArray,rows,cols);
				}
			}else
			{
				System.out.println("Selection must be 1,2, or 3");
				intChoice = dispMenu();
			}
		}while(intChoice != 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[][] takeArray(int rows, int cols) throws IOException
	{
		FileReader freader = new FileReader("Quarterly Sales.txt");
		BufferedReader inputFile = new BufferedReader(freader);

		int ArraySize = 100;
		int i = 0,j = 0;
		String [][] SalesArray = new String[rows][cols];

		char[] chrLocation = new char[ArraySize];
		String input = "";

		input = inputFile.readLine();


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

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


		}
		return SalesArray;
	}


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

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

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

				}

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

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

					}
				}

			}

		}

		return blnValid;
	}

public static void dispReport(String[][] strSalesArray, final int rows, int cols)
	{
		String strDivision[] = new String[4], strFormatted[][] = new String[rows + 1][cols +1];
		for(int i = 0; i < rows -1; i++)
		{
				if(strSalesArray[0].equals("N"))
strFormatted[0][Integer.parseInt(strSalesArray[i][1])] = strSalesArray[i][2];

if(strSalesArray[i][0].equals("E"))
strFormatted[1][Integer.parseInt(strSalesArray[i][1])] = strSalesArray[i][2];

if(strSalesArray[i][0].equals("S"))
strFormatted[2][Integer.parseInt(strSalesArray[i][1])] = strSalesArray[i][2];

if(strSalesArray[i][0].equals("W"))
strFormatted[3][Integer.parseInt(strSalesArray[i][1])] = strSalesArray[i][2];
}


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");
}
}
}
Any input that you can offer me will be more than appreciated! I'm sorry for the inconvenience, but I'm fairly new to this Java and inexperienced.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 4 2010
Added on Dec 7 2009
5 comments
186 views