Hi there, I'm relatively new and I'm making a program that reads reads a txt. file stores it in an array, validates and finally displays back. However I'm not getting the desired display when I tell my program to display txt. file contents, I get some of the correct content but mostly nulls. Here is what my text file looks like:
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
S
4
83.55
E
1
99.40
E
2
25.39
etc..
It has 4 Divisions N, E, S and W a number for the quarter and the sales amount.
If possible could you please help me to display my array properly?
Here is what I have thus far for my 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 = 22, cols = 22; //<--------
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;
}
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]); // Validate Sales Report
System.out.print("!!!!!!!!!!" + i + "!!!!!!!!!!!!!!!!!!");
}
}
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] = 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[i][0].equals("N"))
strFormatted[0][Integer.parseInt(strSalesArray[i][1])] = strSalesArray[i][2];
else if(strSalesArray[i][0].equals("E"))
strFormatted[1][Integer.parseInt(strSalesArray[i][1])] = strSalesArray[i][2];
else if(strSalesArray[i][0].equals("S"))
strFormatted[2][Integer.parseInt(strSalesArray[i][1])] = strSalesArray[i][2];
else 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"); //Display Sales Report
}
System.out.println("\n");
}
}
}
I'm trying to get my output to display something like:
1 2 3 4
M # # # #
W # # # #
S # # # #
E # # # #
Any tips or pointers you can offer will be more than appreciated!