[英]I have an sorted an array that was read from a file, but it won't print
我编写了一个代码来读取文件,并按价格对其进行排序,然后在继续检查功能之前将其输出,并且可以正确编译,但是在输出时却给了我很多空白。
这是我的代码-
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();
}
}
这是输出:
----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
问题是什么?
在读取数据时打印数据:
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++;
}
很可能您没有从文件中读取任何数据。
读入时,您需要打印出这些值。文件很可能是空的,并且永远不会将数据放入数组中。
从文件加载时,还需要将index
的最大值存储在某个位置,以便知道读取了多少个条目。 现在您正在执行的是打印全部100条记录,无论您是否阅读。
您正在为数据分配固定大小的数组:
public static double[] storePrice = new double[100];
public static int[] type = new int[100];
然后尝试在输出例程中使用这些数组的长度:
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
将永远是100
在你的程序,不管你读一个条目或几十个或100(它会崩溃在100以上,这可能是罚款一门功课程序)。
由于长度始终为100
,因此无法将其用于这样的迭代范围。 您应该稍后将输入例程中的index
存储在文件中,以跟踪实际拥有的项数。
绝对不要为每个行创建和关闭outputFile
这个循环。 在循环外部将其打开一次,使用它,然后在循环外部将其关闭。
我建议更改您的例程的名称。 您的输入名为loadFile()
,这很好,但您的输出例程名为sortFile
,它根本不进行排序。 当我阅读您的main()
,我看到了:
loadFile("products.txt");
sortFile();
getPLU();
我期望看到的是:
loadFile("products.txt");
sortFile();
saveFile("sorted_products.txt");
getPLU();
/* and so on */
使您的方法按其名称所言,将您的方法配对为逻辑组,例如“加载/保存”,“打开/关闭”,“读/写”,“ setPLU / getPLU”等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.