java - 我有一个从文件中读取的排序数组,但它不会打印

标签 java arrays

我编写了一段代码来从文件中读取,并按价格对其进行排序,然后在继续检查功能之前输出它,它编译正确,但是当它输出时,它给了我一堆空值。

这是我的代码-

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

很可能您没有从文件中读取任何数据。

关于java - 我有一个从文件中读取的排序数组,但它不会打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8440854/

相关文章:

c++ - 在 C++ 中生成和存储随机数组

php - 响应::json() - Laravel 5.1

javascript - 在数组中得到错误的输出,javascript

jquery - 如何从 jQuery 对象中获取值?

java - SQL - 如果 boolean 值为 True,则返回行

java - 获取 org.hibernate.MappingException : No Dialect mapping for JDBC type:

java - 如何在java中打印HashMap中的单个键值对?

java - 将事物声明为 final 会加速 Eclipse 代码分析和编译吗?

java - String.charAt() 在 java 中返回奇怪的结果

java - 使用 Rest Assured 的 HTTP POST 请求(示例)