ファイルから読み込み、価格でソートして出力するコードを書きましたが、それは正しくコンパイルされましたが、それが出力されたとき、それは私にヌルの束を与えました。ファイルから読み込まれた配列がソートされていますが、印刷されません。
は、ここに私のコード -
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
問題は何ですか?
簡単にコードを入力できますか?それは試してすぐに見て少し長いです。 –
あなたの値のほとんどはnullだと思います。 –