簡體   English   中英

我對從文件中讀取的數組進行了排序,但是無法打印

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM