简体   繁体   中英

I have an sorted an array that was read from a file, but it won't print

I wrote a code to read from a file, and sort it by price, and then output it before continuing on to check out functionality, and it compiled correctly, but when it outputted it gave me a bunch of nulls.

Here's my code-

import java.util.*;
import java.io.*;

public class Program5
{
public static int[] pluCode = new int[100];
public static String[] names = new String[100];
public static double[] storePrice = new double[100];
public static int[] type = new int[100];
public static double total = 0;

    public static void main(String[]args) throws IOException
    {
    loadFile("products.txt");
      sortFile();
    getPLU();
    System.exit(0);
    }

 public static void getPLU()
 {
Scanner keyboard = new Scanner(System.in);
System.out.println("\n What is the PLU code of your next product? Type 0");
System.out.println(" to check              out.");
int pluInput = keyboard.nextInt();
if (pluInput != 0)
{ 
int index = pluSearch(pluInput);
    if (index != -1)
    {
     retrieveProduct(index);
    }
            else 
            { 
            System.out.println("Invalid PLU code.");
            getPLU();
            }
    }
    else
    {
    out();
    }
}
public static void sortFile()throws IOException
{  

for ( int i =0 ; i < storePrice.length ; i++)
{
  PrintWriter outputFile =  new PrintWriter ("products2.txt");

 outputFile.print(pluCode[i] + " " + names[i] + " " + type[i] + " " + storePrice[i]+ "     ");
 //outFile.print(calcPay(names[i], pluCode[i], type[i]) + " " + calcTax(storePrice[i]));

  System.out.println(pluCode[i] + " " + names[i] + " " + type[i] + " " +      storePrice[i] + " ");

    outputFile.close();

}              
}                 
public static void retrieveProduct(int index)
{
if(type[index] == 1)
{
System.out.println("How much do the " + names[index] + " weigh?");
Scanner keyboard = new Scanner(System.in);
total += storePrice[index] * keyboard.nextDouble();
System.out.println("So far it costs " +(total));
getPLU();
}
    else
    {
    System.out.println("Number of units is?" + names[index]);
    Scanner keyboard = new Scanner(System.in);
    total += storePrice [index] * keyboard.nextInt();
    System.out.println("So far it costs " +(total));
    getPLU();
    }
}

public static void out()
{
System.out.println("Your total is $" +(total));
double discount = 0;
if (total > 50)
    {
    discount = total * .05;
    total -= discount;
    }
System.out.println(" Your discount is $" + discount);
System.out.println(" Your total charge is $" + total);        
    if (user())
    {
    getPLU();
    }
    else
    {
    System.exit(0);
    }
}

public static boolean user()
{
    System.out.println("Enter Y for next customer. Enter N to exit.");
    Scanner keyboard = new Scanner(System.in);
    String reply = keyboard.nextLine();
    int yes = reply.indexOf("Y");
    int no = reply.indexOf("N");
    if (yes != -1)
    {
    System.out.println("Next customer.");
    return true;
    }
    else if(no != -1)
    {
    System.out.println("Thank you for your business.");
    return false;
    }
            else
            {
            System.out.println("Invalid answer. Type Y or N.");
            return user();
            }
}

public static int pluSearch(int pluInput)
{
int index = 0;
for (int value : pluCode)
    {
    if (value == pluInput)
    return index;

            else
            index++;
    }
    return -1;
}

public static void loadFile(String str)throws IOException
{

Scanner input;
File file = new File(str);
if(!file.exists())
    {
    System.out.println("Could not open file.");
    System.exit(0);
    }

    input = new Scanner(file);
    int index = 0;

    while (input.hasNext())
    {
    pluCode[index] = Integer.parseInt(input.next());
    names[index] = input.next();
    type[index] = Integer.parseInt(input.next());
    storePrice[index] = Double.parseDouble(input.next());
    index++;
    }
    input.close();
   }
}

And here is the output:

 ----jGRASP exec: java Program5
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 
0 null 0 0.0 

What is the problem?

Print your data as you are reading them:

while ( input.hasNext() ) {
  pluCode[index] = Integer.parseInt(input.next());
  names[index] = input.next();
  type[index] = Integer.parseInt(input.next());
  storePrice[index] = Double.parseDouble(input.next());
  System.out.println(pluCode[index] + " " + names[index] + " " + type[index] + " " + storePrice[index] + " ");
  index++;
 }

Most probably you are not reading any data from the file.

You need to print out the values as you read them in. Most likely the file is coming up empty and you're never getting data into your arrays to begin with.

You also need to somewhere store the max value of index when loading from the file, so you know how many entries you read. What you're doing now is printing the entire 100 entries, whether you read any or not.

You're allocating fixed-size arrays for your data:

public static double[] storePrice = new double[100];
public static int[] type = new int[100];

And then try to use the length of those arrays in your output routine:

public static void sortFile() throws IOException {
    for (int i = 0; i < storePrice.length; i++) {
        PrintWriter outputFile = new PrintWriter("products2.txt");

        outputFile.print(pluCode[i] + " " + names[i] + " " +
            type[i] + " " + storePrice[i] + "     ");
        System.out.println(pluCode[i] + " " + names[i] + " " +
            type[i] + " " + storePrice[i] + " ");

        outputFile.close();
    }
}

storePrice.length will always be 100 in your program, whether you read one entry or dozens or 100. (It'll crash above 100, which might be fine for a homework program.)

Because the length will always be 100 , you cannot use it for an iteration bound like this. You should store the index from your input routines later in the file to keep track of how many items you actually have.

This loop absolutely should not be creating and closing the outputFile for every single line. Open it once, outside the loop, use it, and then close it outside the loop.

I'd suggest changing the names of your routines. Your input is named loadFile() , which is fine, but your output routine is named sortFile , and it does no sorting at all. When I was reading your main() , I saw this:

loadFile("products.txt");
  sortFile();
getPLU();

What I expected to see was:

loadFile("products.txt");
sortFile();
saveFile("sorted_products.txt");
getPLU();
/* and so on */

Make your methods do what their name says, pair up your methods into logical groups like "load / save", "open / close", "read / write", "setPLU / getPLU", etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM