正如您所看到的,我正在创建一个文件并从中读取信息,然后将该信息分配给变量,以便我可以使用它来执行操作。有人可以帮我理解为什么我的编译器说像deskQuantity,lampPrice,theName等可能没有初始化?
谢谢!
/**
Add in javadoc comments
*/
//import statements
import java.io.*;
import java.util.*;
public class Try2 {
public static void main(String[] args){
//variables
Scanner kb = new Scanner(System.in);
boolean valid = false;
int mainSelect = 0;
int itemOption = 0;
boolean valid2 = false;
boolean returnToMain = false;
String theName;
double thePrice;
int theQuantity;
double currBal;
String lampName;
double lampPrice;
int lampQuantity;
String chairName;
double chairPrice;
int chairQuantity;
String deskName;
double deskPrice;
int deskQuantity;
//create file
try{
PrintWriter outputFile = new PrintWriter("inventory.txt");
outputFile.println("3000.0");
outputFile.println("Lamps 15.3 400");
outputFile.println("Chairs 19.95 250");
outputFile.println("Desks 95.0 300");
outputFile.close();
}
catch(IOException e){
System.out.println("File cannot be created.");
}
//read data in from file
try{
File file = new File("inventory.txt");
Scanner inFile = new Scanner(file);
currBal = inFile.nextDouble();
lampName = inFile.next();
lampPrice = inFile.nextDouble();
lampQuantity = inFile.nextInt();
chairName = inFile.next();
chairPrice = inFile.nextDouble();
chairQuantity = inFile.nextInt();
deskName = inFile.next();
deskPrice = inFile.nextDouble();
deskQuantity = inFile.nextInt();
inFile.close();
}
catch(FileNotFoundException e){
System.out.println("Cannot find file.");
}
//present user with main menu
do{
System.out.println("Current Balance: $" + currBal);
System.out.println("\t1. " + lampName + "\t\t(" + lampQuantity + " at $" + lampPrice + ")");
System.out.println("\t2. " + chairName + "\t\t(" + chairQuantity + " at $" + chairPrice + ")");
System.out.println("\t3. " + deskName + "\t\t(" + deskQuantity + " at $" + deskPrice + ")");
System.out.println("\t0. Exit");
while(valid == false){
System.out.print("\nPlease enter choice: ");
try{
mainSelect = kb.nextInt();
if(0 <= mainSelect || mainSelect >= 3){
valid = true;
}
else{
System.out.println("That is not a valid selection. Try again.");
}
}
catch(InputMismatchException ime){
System.out.println("That is not a valid selection. Try again.");
kb.next();
}
}
//present user with second menu
switch(mainSelect){
case 1:
theQuantity = lampQuantity;
thePrice = lampPrice;
theName = lampName;
break;
case 2:
theQuantity = chairQuantity;
thePrice = chairPrice;
theName = chairName;
break;
case 3:
theQuantity = deskQuantity;
thePrice = deskPrice;
theName = deskName;
break;
case 0:
System.exit(0);
break;
}
System.out.println("Current balance: $" + currBal);
System.out.println("Current Quantity: " + theQuantity);
System.out.println("Current price: $" + thePrice);
System.out.println("1. Sell " + theName);
System.out.println("2. Buy " + theName);
System.out.println("3. Change price");
System.out.println("0. Return to main menu");
while(valid2 == false){
System.out.print("\nPlease enter choice: ");
try{
itemOption = kb.nextInt();
if(0 <= itemOption || itemOption >= 3){
valid2 = true;
}
else{
System.out.println("That is not a valid selection. Try again.");
}
}
catch(InputMismatchException ime){
System.out.println("That is not a valid selection. Try again.");
kb.next();
}
}
}while(returnToMain == true);
}
}
答案 0 :(得分:1)
编译器警告您正在尝试使用可能没有任何值的变量。
您可以像这样创建办公桌数量:
int deskQuantity;
然后你尝试为它赋值(在try {}块中)
try{
File file = new File("inventory.txt");
Scanner inFile = new Scanner(file);
currBal = inFile.nextDouble();
lampName = inFile.next();
lampPrice = inFile.nextDouble();
lampQuantity = inFile.nextInt();
chairName = inFile.next();
chairPrice = inFile.nextDouble();
chairQuantity = inFile.nextInt();
deskName = inFile.next();
deskPrice = inFile.nextDouble();
deskQuantity = inFile.nextInt();
inFile.close();
}
然后你会像这样打印出来:
System.out.println("\t3. " + deskName + "\t\t(" + deskQuantity + " at $" + deskPrice + ")");
但是如果try块失败,则永远不会设置该值。您可以通过设置默认值(初始化)来避免此警告:
String deskName = "";
int deskQuantity = 0;
但是你也应该注意处理未设置的值或其他故障。您已经检查过是否找不到该文件。您还应该在此处捕获其他异常,如果您发现任何异常,可能会停止处理:
catch(FileNotFoundException e){
System.out.println("Cannot find file.");
} catch(Exception e) {
System.out.println("Caught Exception: " + e.getLocalizedMessage() );
}
答案 1 :(得分:0)
如果Scanner inFile = new Scanner(file);
或第二个try
块中的任何其他行引发异常,则程序会捕获它,并继续尝试显示菜单。
但是菜单代码指的是在这种情况下不会给出值的变量。编译器阻止您执行此操作,并强制您确保这些变量具有值。
最好将菜单代码移到第二个try
块中,这样如果从Scanner
读取值的代码失败,它就不会运行。