Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
控制台显示扫描仪已关闭,我需要它重新打开。Java错误_Java - Fatal编程技术网

控制台显示扫描仪已关闭,我需要它重新打开。Java错误

控制台显示扫描仪已关闭,我需要它重新打开。Java错误,java,Java,这是我到目前为止的所有代码,我不确定问题出在哪里。这是一个杂货店收据程序,用于处理异常,用户可以更新文件中的产品列表。该程序将搜索该文件,并在我的电脑中弹出一个名为“收据”的文件,记录所有“购买”的产品 代码如下: package groceries; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; impor

这是我到目前为止的所有代码,我不确定问题出在哪里。这是一个杂货店收据程序,用于处理异常,用户可以更新文件中的产品列表。该程序将搜索该文件,并在我的电脑中弹出一个名为“收据”的文件,记录所有“购买”的产品

代码如下:

package groceries;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Project2_ReceiptPrinterV2 {

public static void main(String[] args) throws FileNotFoundException {

    // Declaring all of the variables and containers that the program needs

    File inFile = new File("C:\\Users\\camer\\Desktop\\CECS 274\\Pricelist.txt");
    Scanner fileReader = new Scanner(inFile);
    Scanner userInput = new Scanner(System.in);
    String input;

    String itemRegex = "([\\w |\\W]+[s|\\w])\\s+(\\w[\\w |\\W]+\\S)\\s+([\\w |\\W]+\\s[\\w]+\\w)"; 
    String regexFull = "([\\w |\\W]+[s|\\w])\\s+(\\w[\\w |\\W]+\\S)\\s+([\\w |\\W]+\\s[\\w]+\\w)\\s+([\\w |\\W]+)";

    Pattern fullItemPattern = Pattern.compile(regexFull, Pattern.CASE_INSENSITIVE); 
    Pattern noPricePattern = Pattern.compile(itemRegex, Pattern.CASE_INSENSITIVE);

    Matcher fileMatcher; 
    Matcher userMatcher;

    System.out.println("Enter the output filename: "); // Create the file name 
    String outFileName = userInput.nextLine();
    PrintWriter outFile = new PrintWriter("C:\\Users\\camer\\Desktop\\Receipts\\" + outFileName +".txt");
    StringBuilder returnedString = new StringBuilder();
    final int maxLen = 60;

    ArrayList<Item> products = new ArrayList<>();
    ArrayList<String> buffer = new ArrayList<>();
    NumberFormat formatter = NumberFormat.getCurrencyInstance();

    String product;
    int holder;
    boolean inList = false;
    double total = 0.0;
    int numberOfItems;
    String quantifier;
    String itemPrice;
    boolean runProgram = true;
    String ProductEntry = "";
    String ProductEntryName = "";
    String ProductQuality = "";
    double ProductEntryPrice = 0;

    // Starting the user input loop


    while (runProgram) {
        System.out.println("Enter a product to add to your cart:\nEnter 'done' when finished.");
        input = userInput.nextLine().trim();
        if (input.equalsIgnoreCase("done")) {
            break;
        }
        else {
            userMatcher = noPricePattern.matcher(input);
        }
        try {
            if (userMatcher.find()) {
                while (fileReader.hasNextLine()) {

                    fileReader = new Scanner(inFile);

                    fileMatcher = fullItemPattern.matcher(fileReader.nextLine());

                    if (fileMatcher.find()) {
                        if (fileMatcher.group(1).trim().equals(userMatcher.group(1)) && fileMatcher.group(2).trim().equals(userMatcher.group(2)) && fileMatcher.group(3).trim().equals(userMatcher.group(3))){
                            product = fileMatcher.group(1) + " " + fileMatcher.group(2).trim();
                            Item itemHolder = new Item(product, Double.parseDouble(fileMatcher.group(4)), fileMatcher.group(3).trim());


                            if (products.contains(itemHolder)) {
                                // Adding the first product
                                products.get(products.indexOf(itemHolder)).incQuantity();
                            }
                            else {
                                // Adding multiple products after that
                                products.add(itemHolder);
                            }
                            // User feedback that the item was added
                            inList = true;
                            System.out.println("Product added!");
                        }
                    }
                }
            }
            if (!inList) {
                // If there is no such item in the file
                throw new IOException();
            }
            else {
                inList = false;
            }
            // Resetting the fileReader for each new item
            fileReader.close();
            fileReader = new Scanner(inFile);

        }
        catch(IOException exception) {
            System.out.println("Error! Selected product is not listed in file!");
            System.out.println("Would you like to correct your entry?");
            String UserResponse = userInput.nextLine().trim();
            if(UserResponse.equals("yes")) {
            }
            if (UserResponse.equals("no")) {
                System.out.println("Would you like to add your selected entry in to the file");
                String UserResponse2 = userInput.nextLine().trim();
                    if(UserResponse2.equals("yes")) {}
                        try {
                            File PriceList = new File("C:\\Users\\camer\\Desktop\\CECS 274\\Pricelist.txt");
                            FileWriter NewEntry = new FileWriter(PriceList, true);
                            BufferedWriter added = new BufferedWriter(NewEntry);
                            added.newLine();
                            System.out.println("Adding selected product into file now ...");
                            System.out.println("Enter Product name: ");
                            ProductEntry = userInput.nextLine().trim();
                            ProductEntryName = ProductEntry.split(" ")[0];
                            ProductQuality = ProductEntry.split(" ")[1];
                            ProductEntryName = ProductEntryName + " " + ProductQuality;
                            System.out.println("Enter the Size/Weight of the product");
                            String ProductEntrySize = userInput.nextLine().trim();
                            System.out.println("Enter the price of the product");
                            ProductEntryPrice = userInput.nextDouble();
                            String NewProduct = ProductEntryName + " " +  ProductEntrySize + " " + ProductEntryPrice;
                            added.write(NewProduct);
                            System.out.println("Product added!\n");
                            added.close();



                     }
                     catch (IOException error) {
                         error.printStackTrace();
                     }
                     if(UserResponse2.equals("no")) {
                         break;
                     }
                 }
                 }

        finally {

                // Outputting to the file
                for (int i = 0; i <= 60; i ++) {
                    outFile.print("_");
                }
                outFile.println();
                outFile.println("Java Market");
                outFile.println("242 W Santa Cruz St");
                outFile.println("San Pedro, CA");
                outFile.println("90731");
                outFile.println();
                outFile.println("Product                                              Subtotal");

                // Looping through products and processing multiples, formatting strings, and outputting to the file
                for (Item a : products) {
                    buffer.add(a.toString());
                    total += a.getTotalPrice();
                }

                Collections.sort(buffer);
                for (String b : buffer) {
                    outFile.println(b);
                }
                //outFile.println();

                for (int i = 0; i <= 60; i ++) {
                    outFile.print("_");
                }
                // Skipping lines and printing the formatted total
                outFile.println();
                outFile.println();
                outFile.println();
                outFile.printf("Your total is: $%.2f", total);
                outFile.println();
                for (int i = 0; i <= 60; i ++) {
                    outFile.print("_");
                }
                // Closing the out file and file reader after everything is done
                outFile.close();
                fileReader.close();
            }
        }
    }
}
包装食品;
导入java.io.BufferedWriter;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.io.PrintWriter;
导入java.text.NumberFormat;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.Scanner;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
公共类项目2_ReceiptPrinterV2{
公共静态void main(字符串[]args)引发FileNotFoundException{
//声明程序需要的所有变量和容器
File infle=新文件(“C:\\Users\\camer\\Desktop\\CECS 274\\Pricelist.txt”);
Scanner fileReader=新扫描仪(infle);
扫描仪用户输入=新扫描仪(System.in);
字符串输入;
String itemRegex=“([\\w |\\w]+[s |\\w])\\s+(\\w[\\w |\\w]+\\s)\\s+([\\w |\\w]+\\s[\\w]+\\w)”;
字符串regexFull=“([\\w|\\w]+[s|\\w])\\s+(\\w[\\w|\\w]+\\s)\\s+([\\w|\\w]+\\s[\\w]+\\s[\\w]+\\w)\\s+([\\w|\\w]+);
Pattern fullItemPattern=Pattern.compile(regexFull,Pattern.CASE\u不区分大小写);
Pattern noPricePattern=Pattern.compile(itemRegex,Pattern.CASE不区分大小写);
匹配器文件匹配器;
匹配器用户匹配器;
System.out.println(“输入输出文件名:”);//创建文件名
字符串outFileName=userInput.nextLine();
PrintWriter outFile=新的PrintWriter(“C:\\Users\\camer\\Desktop\\Receipts\\”+outFileName+“.txt”);
StringBuilder returnedString=新建StringBuilder();
最终整数maxLen=60;
ArrayList产品=新的ArrayList();
ArrayList缓冲区=新的ArrayList();
NumberFormat formatter=NumberFormat.getCurrencyInstance();
串产品;
int持有人;
布尔inList=false;
双倍合计=0.0;
国际项目数;
字符串量词;
字符串项价格;
布尔runProgram=true;
字符串ProductEntry=“”;
字符串ProductEntryName=“”;
字符串ProductQuality=“”;
double ProductEntryPrice=0;
//启动用户输入循环
while(运行程序){
System.out.println(“输入要添加到购物车中的产品:\n完成后输入'done'”);
input=userInput.nextLine().trim();
if(input.equalsIgnoreCase(“完成”)){
打破
}
否则{
userMatcher=noPricePattern.matcher(输入);
}
试一试{
if(userMatcher.find()){
while(fileReader.hasNextLine()){
fileReader=新扫描仪(内嵌);
fileMatcher=fullItemPattern.matcher(fileReader.nextLine());
if(fileMatcher.find()){
if(fileMatcher.group(1).trim()等于(userMatcher.group(1))&&fileMatcher.group(2).trim()等于(userMatcher.group(2))&&fileMatcher.group(3).trim()等于(userMatcher.group(3))){
product=fileMatcher.group(1)+“”+fileMatcher.group(2.trim();
itemHolder=新项(product,Double.parseDouble(fileMatcher.group(4)),fileMatcher.group(3.trim());
if(产品包含(项目持有人)){
//添加第一个产品
products.get(products.indexOf(itemHolder)).incQuantity();
}
否则{
//之后添加多个产品
产品。添加(项目持有人);
}
//用户反馈已添加该项
inList=true;
System.out.println(“添加产品!”);
}
}
}
}
如果(!inList){
//如果文件中没有此类项目
抛出新IOException();
}
否则{
inList=false;
}
//为每个新项目重置fileReader
fileReader.close();
fileReader=新扫描仪(内嵌);
}
捕获(IOException异常){
System.out.println(“错误!文件中未列出所选产品!”);
System.out.println(“您想更正您的输入吗?”);
字符串UserResponse=userInput.nextLine().trim();
if(UserResponse.equals(“yes”)){
}
if(UserResponse.equals(“no”)){
System.out.println(“是否将所选条目添加到文件中”);
字符串UserResponse2=userInput.nextLine().trim();
如果(UserResponse2.equals(“yes”){}
试一试{
File PriceList=新文件(“C:\\Users\\camer\\Desktop\\CECS 274\\PriceList.txt”);
FileWriter NewEntry=新FileWriter(PriceList,true);
BufferedWriter added=新的BufferedWriter(NewEntry);
添加了.newLine();
System.out.println(“立即将所选产品添加到文件…”);
System.out.println(“输入产品名称:”);
ProductEntry=userInput.nextLine().trim();
ProductEntryName=ProductEntry.split(“”[0];
ProductQuality=ProductEntry.split(“”[1];
ProductEntryName=ProductEntryName+“”+ProductQuality;
System.out.println(“输入p的大小/重量