简体   繁体   English

我对从文件中读取的数组进行了排序,但是无法打印

[英]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. 从文件加载时,还需要将index的最大值存储在某个位置,以便知道读取了多少个条目。 What you're doing now is printing the entire 100 entries, whether you read any or not. 现在您正在执行的是打印全部100条记录,无论您是否阅读。

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.) storePrice.length将永远是100在你的程序,不管你读一个条目或几十个或100(它会崩溃在100以上,这可能是罚款一门功课程序)。

Because the length will always be 100 , you cannot use it for an iteration bound like this. 由于长度始终为100 ,因此无法将其用于这样的迭代范围。 You should store the index from your input routines later in the file to keep track of how many items you actually have. 您应该稍后将输入例程中的index存储在文件中,以跟踪实际拥有的项数。

This loop absolutely should not be creating and closing the outputFile for every single line. 绝对不要为每个行创建和关闭outputFile这个循环。 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. 您的输入名为loadFile() ,这很好,但您的输出例程名为sortFile ,它根本不进行排序。 When I was reading your main() , I saw this: 当我阅读您的main() ,我看到了:

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. 使您的方法按其名称所言,将您的方法配对为逻辑组,例如“加载/保存”,“打开/关闭”,“读/写”,“ setPLU / getPLU”等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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