I'm trying to read in a specific text file that will contain either codes 1, 2, 3, or 4. My program is supposed to keep track of flights and reservations. If the code is 1, it will add a new reservation, so the 1 is followed up by all the information needed for a reservation. If the code is 2, it will give me a reservation number, and I am supposed to print the itinerary for that reservation. If it is 3, I print all itineraries, and if it is 4, I write all of the reservations to a new file. I'm not sure how to go about this, and I'm really stuck! Please help!
我正在尝试读取包含代码1,2,3或4的特定文本文件。我的程序应该跟踪航班和预订。如果代码为1,它将添加新的预订,因此1后面会跟随预订所需的所有信息。如果代码是2,它将给我一个预订号码,我应该打印该预订的行程。如果是3,我打印所有行程,如果是4,我将所有预约写入新文件。我不知道怎么回事,我真的被卡住了!请帮忙!
Here is what the text file contains:
以下是文本文件包含的内容:
1
John Miller
1234
UA1235
06/12/2014
ORD
LGA
4:00 PM
7:15 PM
30F
UA673
06/19/2014
LGA
ORD
10:00 AM
11:25 AM
10E
3
1
Bob Barker
8497
UA317
08/04/2014
ORD
SFO
8:10 AM
10:45 AM
12A
UA728
08/14/2014
SFO
ORD
12:52 PM
7:03 PM
18A
2
1235
2
8976
3
4
I do not necessarily need specifics which is why I'm not providing any code. I was just wondering how I can go about doing this. Let me know if you need more to answer the question.
我不一定需要细节,这就是为什么我不提供任何代码。我只是想知道如何才能做到这一点。如果您需要更多回答这个问题,请与我们联系。
3 个解决方案
#1
So when you read in each line, just split it by a space, and then convert the string in the first index to an integer.
因此,当您读取每一行时,只需将其拆分为空格,然后将第一个索引中的字符串转换为整数。
Then just use a switch for that to determine what you should do.
然后只需使用一个开关来确定你应该做什么。
Since you aren't using a database, just put the reservation into a hash map, so it is faster to look up later.
由于您没有使用数据库,只需将预留放入哈希映射中,以便以后查找更快。
This should help you start.
这应该可以帮助你开始。
#2
OK so if i'm understanding this correctly here is the psedocode you will use for your application.
好的,所以如果我正确地理解这一点,那么你将用于你的应用程序的psedocode。
1. Read file line by line.
2. Check if line.equals() 1,2,3 or 4.
3. If the line equals 1,2,3 or 4 then jump to function 1(),2(),3() or 4() //Name these better.
Function 1
1. This function should contain one parameter which is an object or array which stores the 16 lines of text which follows a '1' in the file.
2. You should now use this information as needed.
3. You should maybe create a custom class for this information.
Function 2
1. The function parameter should be the reservation number.
2. Fetch the data from this reservation.
3. Print the data.
Function 3
1. No parameters.
2. Print all classes which have been created using function 1?
Function 4
1. Get all objects created by function 1.
2. Loop through these objects and print each one to file.
Some notes here,
这里有一些笔记,
The most important function is function 1. You want to create a list / array which stores all of the objects created in function 1.... this will help with future stages, for example if your file contains function 3 then you can loop through the list and print out each itinerary. I'm making the assumption that you know what objects are and how to create custom ones.
功能1.最重要的功能是你想要创建一个列表/数组来存储在函数1中创建的所有对象....这将有助于未来的阶段,例如,如果你的文件包含函数3,那么你可以循环列表并打印出每个行程。我假设您知道对象是什么以及如何创建自定义对象。
If you need further explanations I can attempt to give them to you. I suggest practicing with file input / output before tackling a problem like this.
如果您需要进一步解释,我可以尝试将它们交给您。我建议在解决这样的问题之前练习文件输入/输出。
#3
Probably this should help u start.... As James said you are better using Hashmap.
可能这应该有助于你开始....正如詹姆斯所说你更善于使用Hashmap。
import java.io.*;
import java.util.*;
public class Test5{
public static void main(String[] args) {
Test5 test=new Test5();
test.calculate("abc.txt");
}
public void calculate(String filename){
String thisLine=null;
int reservationDetails=16; //no of loops to input reservation details
HashMap<String,String> hm=new HashMap<String,String>();
ArrayList<HashMap<String,String>> reservationData=new ArrayList<HashMap<String,String>>();
try{
FileReader infile=new FileReader(filename);
BufferedReader br=new BufferedReader(infile);
FileWriter outputfile=new FileWriter("output.txt");
thisLine=br.readLine();
while(thisLine!=null){
int code=Integer.parseInt(thisLine);
// System.out.println(code);
switch(code){
case 1:
for(int i=1;i<=reservationDetails;i++){
thisLine=br.readLine();
//String[] words =thisLine.split(" "); //only if you require individual word
hm.put("Name",thisLine);
hm.put("id",thisLine);
hm.put("reservationNumber",thisLine);
// do this for all of your data
}
reservationData.add(new HashMap<String,String>(hm));
break;
case 2:
System.out.println(hm.get("reservationNumber"));
break;
case 3:
System.out.println("Printing all reservation ");
Set<Map.Entry<String,String>> set;
for (HashMap<String,String> hms :reservationData ) {
set=hms.entrySet();
for (Map.Entry entity : set ) {
System.out.println(entity.getKey()+": "+entity.getValue());
}
}
break;
case 4:
Set<Map.Entry<String,String>> set2;
for (HashMap<String,String> hms :reservationData ) {
set2=hms.entrySet();
for (Map.Entry entity : set2 ) {
outputfile.write(entity.getKey()+": "+entity.getValue()+"\n");
}
}
break;
}
thisLine=br.readLine();
}
outputfile.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
#1
So when you read in each line, just split it by a space, and then convert the string in the first index to an integer.
因此,当您读取每一行时,只需将其拆分为空格,然后将第一个索引中的字符串转换为整数。
Then just use a switch for that to determine what you should do.
然后只需使用一个开关来确定你应该做什么。
Since you aren't using a database, just put the reservation into a hash map, so it is faster to look up later.
由于您没有使用数据库,只需将预留放入哈希映射中,以便以后查找更快。
This should help you start.
这应该可以帮助你开始。
#2
OK so if i'm understanding this correctly here is the psedocode you will use for your application.
好的,所以如果我正确地理解这一点,那么你将用于你的应用程序的psedocode。
1. Read file line by line.
2. Check if line.equals() 1,2,3 or 4.
3. If the line equals 1,2,3 or 4 then jump to function 1(),2(),3() or 4() //Name these better.
Function 1
1. This function should contain one parameter which is an object or array which stores the 16 lines of text which follows a '1' in the file.
2. You should now use this information as needed.
3. You should maybe create a custom class for this information.
Function 2
1. The function parameter should be the reservation number.
2. Fetch the data from this reservation.
3. Print the data.
Function 3
1. No parameters.
2. Print all classes which have been created using function 1?
Function 4
1. Get all objects created by function 1.
2. Loop through these objects and print each one to file.
Some notes here,
这里有一些笔记,
The most important function is function 1. You want to create a list / array which stores all of the objects created in function 1.... this will help with future stages, for example if your file contains function 3 then you can loop through the list and print out each itinerary. I'm making the assumption that you know what objects are and how to create custom ones.
功能1.最重要的功能是你想要创建一个列表/数组来存储在函数1中创建的所有对象....这将有助于未来的阶段,例如,如果你的文件包含函数3,那么你可以循环列表并打印出每个行程。我假设您知道对象是什么以及如何创建自定义对象。
If you need further explanations I can attempt to give them to you. I suggest practicing with file input / output before tackling a problem like this.
如果您需要进一步解释,我可以尝试将它们交给您。我建议在解决这样的问题之前练习文件输入/输出。
#3
Probably this should help u start.... As James said you are better using Hashmap.
可能这应该有助于你开始....正如詹姆斯所说你更善于使用Hashmap。
import java.io.*;
import java.util.*;
public class Test5{
public static void main(String[] args) {
Test5 test=new Test5();
test.calculate("abc.txt");
}
public void calculate(String filename){
String thisLine=null;
int reservationDetails=16; //no of loops to input reservation details
HashMap<String,String> hm=new HashMap<String,String>();
ArrayList<HashMap<String,String>> reservationData=new ArrayList<HashMap<String,String>>();
try{
FileReader infile=new FileReader(filename);
BufferedReader br=new BufferedReader(infile);
FileWriter outputfile=new FileWriter("output.txt");
thisLine=br.readLine();
while(thisLine!=null){
int code=Integer.parseInt(thisLine);
// System.out.println(code);
switch(code){
case 1:
for(int i=1;i<=reservationDetails;i++){
thisLine=br.readLine();
//String[] words =thisLine.split(" "); //only if you require individual word
hm.put("Name",thisLine);
hm.put("id",thisLine);
hm.put("reservationNumber",thisLine);
// do this for all of your data
}
reservationData.add(new HashMap<String,String>(hm));
break;
case 2:
System.out.println(hm.get("reservationNumber"));
break;
case 3:
System.out.println("Printing all reservation ");
Set<Map.Entry<String,String>> set;
for (HashMap<String,String> hms :reservationData ) {
set=hms.entrySet();
for (Map.Entry entity : set ) {
System.out.println(entity.getKey()+": "+entity.getValue());
}
}
break;
case 4:
Set<Map.Entry<String,String>> set2;
for (HashMap<String,String> hms :reservationData ) {
set2=hms.entrySet();
for (Map.Entry entity : set2 ) {
outputfile.write(entity.getKey()+": "+entity.getValue()+"\n");
}
}
break;
}
thisLine=br.readLine();
}
outputfile.close();
}
catch(Exception e){
System.out.println(e);
}
}
}