2011-12-09 24 views
0

ファイルから読み込み、価格でソートして出力するコードを書きましたが、それは正しくコンパイルされましたが、それが出力されたとき、それは私にヌルの束を与えました。ファイルから読み込まれた配列がソートされていますが、印刷されません。

は、ここに私のコード -

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 

問題は何ですか?

+0

簡単にコードを入力できますか?それは試してすぐに見て少し長いです。 –

+0

あなたの値のほとんどはnullだと思います。 –

答えて

0

読み込んだときに値を印刷する必要があります。ファイルが空になっていて、配列にデータが入らない場合がほとんどです。

また、ファイルからロードするときに最大値indexを保存する必要があります。そのため、読んだエントリの数が分かります。あなたが今やっていることは、あなたが読んでいるかどうかにかかわらず、100項目全体を印刷することです。

0

あなたはあなたのデータの固定サイズの配列を割り当てている。そして、

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になりますあなたのプログラムで を1つ入力するか数十を読むか100を読み込むかを指定します(宿題プログラムの場合は )。これは100を超えてクラッシュします。

長さは常に100なので、このようにバインドされた 繰り返しには使用できません。 の入力番号 ルーチンのindexを実際にファイルに保存して、実際に何個の項目があるかを把握する必要があります。

このループは、1行ごとにoutputFile を作成して閉じる必要はありません。一度ループの外側でそれを開き、それを使用してから、 をループの外側で閉じます。

私はあなたのルーチンの名前を変更することをお勧めします。あなたの入力は loadFile()という名前ですが、出力ルーチンはsortFile, となり、になります。私はあなたのmain()を読んでいたとき、私は これを見た:

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

私が見込ま何ました:

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

あなたの方法は、自分の名前が言う、 論理グループにあなたの方法をペアに間に合わせます"ロード/セーブ"、 "オープン/クローズ"、 "リード/ライト"、 "setPLU/getPLU"などのように

1

あなたがそれらを読んでいると、あなたのデータを印刷します:

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++; 
} 

は、おそらくあなたは、ファイルからデータを読み込むされていません。

関連する問題