我正在创建一个FileManager类,该类正在创建arraylists,以便在数据之间使用@@保留文本文件中输入的信息,并在文本中输出整行数据。我的问题是,我只能将其打印出我需要全部三个数据的一部分。
示例:玉米片@@一块玉米片@@ 200 炸奶酪@@奶酪面包屑和炸@@ 300 Peirogies @@装满土豆@@ 250 椒盐脆饼@@大型椒盐脆饼@@ 150
我的代码仅输出“一堆墨西哥玉米片”或“装满土豆”
我的FileManager:
package edu.pitt.is17.ndi4.menumanager;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
public class FileManager {
final static String SPLIT = "@@"; //string of separating chars
public static ArrayList<Entree> readEntrees (String fileName){
//all the file read methods are the same, only the first will be commented
ArrayList<Entree> entrees = new ArrayList<Entree>(); //create array list to hold data read
int arrayIndex=0; //tracks the array index and increments after each line of the file
try {
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String line = null;
while ((line = br.readLine()) != null) {
int first = line.indexOf(SPLIT); //determine the two spots in the line that split the data
int second = line.indexOf(SPLIT, first + 1);
//add to the arraylist a new object of each menu type using the constructor and substrings of the file line
entrees.add(arrayIndex, //index of arraylist starts at 0 and goes up each line
new Entree(line.substring(0,first), //Name of item
line.substring(first+2,second), //Description
Integer.parseInt(line.substring(second+2)),0)); //calories
arrayIndex++; //increment the arraylist index for the next line
}
br.close();
fr.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return entrees;
}
public static ArrayList<Side> readSides (String fileName){
ArrayList<Side> sides = new ArrayList<Side>();
int arrayIndex = 0 ;
try {
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String line = null;
while ((line = br.readLine()) != null) {
int first = line.indexOf(SPLIT);
int second = line.indexOf(SPLIT, first + 1);
sides.add(arrayIndex, new Side(line.substring(0,first), line.substring(first+2,second), Integer.parseInt(line.substring(second+2))));
arrayIndex++;
}
br.close();
fr.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return sides;
}
public static ArrayList<Beer> readBeers (String fileName){
ArrayList<Beer> beers = new ArrayList<Beer>();
int arrayIndex = 0 ;
try {
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String line = null;
while ((line = br.readLine()) != null) {
int first = line.indexOf(SPLIT);
int second = line.indexOf(SPLIT, first + 1);
beers.add(arrayIndex, new Beer(line.substring(0,first), line.substring(first+2,second), Integer.parseInt(line.substring(second+2))));
arrayIndex++;
}
br.close();
fr.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return beers;
}
public static ArrayList<Appetizer> readAppetizers (String fileName){
ArrayList<Appetizer> appetizers = new ArrayList<Appetizer>();
int arrayIndex = 0 ;
try {
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String line = null;
while ((line = br.readLine()) != null) {
int first = line.indexOf(SPLIT);
int second = line.indexOf(SPLIT, first + 1);
appetizers.add(arrayIndex, new Appetizer(line.substring(0,first), line.substring(first+2,second), Integer.parseInt(line.substring(second+2))));
arrayIndex++;
}
br.close();
fr.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return appetizers;
}
}
我的菜单随机化:
package edu.pitt.is17.ndi4.menumanager;
import java.util.ArrayList;
public class MenuRandomize {
private ArrayList<Entree> entrees = new ArrayList<Entree>();
private ArrayList<Side> sides = new ArrayList<Side>();
private ArrayList<Beer> beers = new ArrayList<Beer>();
private ArrayList<Appetizer> appetizers = new ArrayList<Appetizer>();
public MenuRandomize (String entreeFile, String sideFile, String
beerFile, String appetizerFile){
entrees = FileManager.readEntrees(entreeFile);
sides = FileManager.readSides(sideFile);
beers = FileManager.readBeers(beerFile);
appetizers = FileManager.readAppetizers(appetizerFile);
public Menu randomMenu() {
//create a new menu object
Menu randMenu = new Menu ("Random Sample Plate");
//use setter for each item, based on size of array list * random
randMenu.setEntree(entrees.get((int)(entrees.size()*Math.random())));
randMenu.setSide(sides.get((int)(sides.size()*Math.random())));
randMenu.setBeer(beers.get((int)(beers.size()*Math.random())));
randMenu.setAppetizer(appetizers.get((int)(appetizers.size()*Math.random())));
return randMenu;
我的MenuTester:
package edu.pitt.is17.ndi4.menumanager;
public class MenuTester{
public static void main(String[] args){
MenuRandomize randomize = new MenuRandomize("data/entrees.txt",
"data/sides.txt","data/beers.txt","data/appetizers.txt");
Menu myMenu = randomize.randomMenu();
System.out.println(myMenu.description()+"\nTotal calories "+
myMenu.totalCalories());
}
}
运行此命令时,我只会得到以下输出:
开胃菜:装满土豆
主菜:精心挑选的优质牛排烧烤炉
侧面:煮熟的土豆在烤箱中烤一点黄油
啤酒:味道像铁!
总卡路里1460
例如,我需要在此随机菜单中显示以下内容:“ Peirogies装满土豆250
任何帮助将不胜感激,我觉得我在忽略某些东西,如果需要更多代码才能找到此错误,可以提供此错误。谢谢您的帮助。