我编写了一个分配程序来读取2个文件并输出2个文件。读入的记录文件将被组织并重新写入结果文件。将执行读入的指令文件,并将结果生成到报告文件中。
问题是,当我尝试在这样的终端中运行时:
cd desktop/lab/bin/java POS18sz.POS /Users/Jan/Desktop/recordFile.txt /Users/Jan/Desktop/instructionFile.txt resultFile.txt reportFile.txt
什么都没发生,即使我等了很长时间。如果我尝试关闭终端,它会说:“是否要关闭正在运行的程序?”。代码中也没有错误的指示,我真的很困惑。
我在Eclipse中运行run,并且该软件包已自动编译。由于该代码有5个类并且很长,我应该提供代码的哪一部分来使问题更清楚?
这是主要的课程: 封装POS18sz;
import java.util.Scanner; 导入java.io。*;
公共类POS { 公共静态void main(String args []){
if(args.length == 4) {
Processor pp = new Processor(args);
pp.recordFile();
pp.instructionFile();
pp.resultFile();
pp.reportFile();
}
}
}
这是文件处理器类: 封装POS18sz;
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Processor {
private File recordFile;
private File instructionFile;
private File resultFile;
private File reportFile;
private PetShop petshop;
public Processor(String[]s) {
recordFile = new File(s[0]);
instructionFile = new File(s[1]);
resultFile = new File(s[2]);
reportFile = new File(s[3]);
petshop = new PetShop();
}
public void recordFile() {
ArrayList<Residents> petshop = new ArrayList<Residents>();
Residents r1 = new Residents();
try
{ Scanner scan = new Scanner(recordFile);
String temp = scan.nextLine();
String address = null;
while(scan.hasNextLine())
{
if(temp.startsWith("name"))
{
r1.setName(temp);
}
else if(temp.startsWith("birthday"))
{
r1.setBirthday(temp);
}
else if(temp.startsWith("address"))
{
address = temp;
}
else if(!temp.startsWith("name")&&!temp.startsWith("birthday")&&!temp.startsWith("postcode")&&!temp.startsWith("phone")&&!temp.startsWith("pet"))
{
r1.setAddress(address+","+temp.trim());
}
else if(temp.startsWith("postcode"))
{
r1.setPostcode(temp);;
}
else if(temp.startsWith("phone"))
{
r1.setPhone(temp);
}
else if(temp.startsWith("pet"))
{
r1.setPetown(temp);;
}
else if(temp.equals(""))
{
petshop.add(r1); //????
r1.setName(null);
r1.setBirthday(null);
r1.setAddress(null);
r1.setPostcode(null);
r1.setPhone(null);
r1.setPetown(null);
}
} //End of while loop
}catch(FileNotFoundException e)
{
e.printStackTrace();
}
}
public void resultFile(){
try
{
PrintWriter out = new PrintWriter(new FileOutputStream(resultFile));
out.println(petshop.toString()); //toString need to be ajusted.
out.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
}
public void instructionFile() {
try
{
Scanner scan = new Scanner(instructionFile);
while(scan.hasNextLine())
{
String instruction = scan.nextLine();
Scanner sc = new Scanner(instruction);
String keyword;
if(sc.hasNext())
{
keyword = sc.next();
if(sc.hasNextLine())
{
String[]paramup = sc.nextLine().split(";");
String toString = Arrays.toString(paramup);
if(keyword.equals("update"))
{
if(petshop.checkPhone(paramup)==true)
{
for(String s: paramup) {
if(s.contains("phone"))
{
int index = petshop.getIndex(s);
petshop.update(toString,index);
}
else
{
petshop.add(toString);
}
}
}
/*DELETE*/ } else if(keyword.equals("delete"))
{
String []paramde = sc.next().split(";");
petshop.delete(paramde[1], paramde[2]);
/*SORT*/ } else if(keyword.equals("sort"))
{
petshop.sortName();
/*QUERY*/ } else if(keyword.equals("query"))
{
String paramq = sc.nextLine();
if(paramq.substring(6,10).equals("name"))
{
petshop.addqueryInstruction(paramq);
} else if(paramq.substring(11).equals("age"))
{
petshop.addqueryInstruction(paramq);
} else if(paramq.substring(6).equals("pet"))
{
petshop.addqueryInstruction(paramq);
}
}
}
}
}//End of while loop
}//End of try
catch(FileNotFoundException e)
{
e.printStackTrace();
}
}
public void reportFile() {
try
{
PrintWriter out = new PrintWriter(new FileOutputStream(reportFile));
String paramq;
for(int i=0; i<petshop.queryInstructionSize();i++)
{
paramq = petshop.getqueryInstruction(i);
if(paramq.contains("name"))
{
out.println(petshop.printTitle("name", paramq.substring(10)));
out.println(petshop.queryName(paramq.substring(6)));
out.println(petshop.printEnding());
}else if(paramq.contains("age"))
{
out.println(petshop.printTitle(paramq.substring(6,10), " age"));
petshop.addage(paramq.substring(6,10));
out.println("Available pet owner size: "+petshop.getAgeSize());
out.println("Age profile");
out.println("Below 18: "+petshop.blow18());
out.println("Over 18 and Below 65: "+petshop.blow65());
out.println("Over 65: "+petshop.over65());
out.println("Unknown: "+petshop.unknown());
petshop.printEnding();
}else if(paramq.contains("pet"))
{
petshop.printTitle("pet",null);
petshop.addsuburb();
petshop.addpets();
petshop.twodarrayPetSuburb();
for(int j=0; i<petshop.petkinds();j++)
{
System.out.println(petshop.kind(j)+": "+petshop.amount(j)+"; postcode: "+petshop.suburb(j));
}
petshop.printEnding();
}
}
out.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
在您的代码中,您忘记了read
的下一行
String temp = scan.nextLine();
while(scan.hasNextLine())
{
if(temp.startsWith("name")) {
.....
}
....
// read the next line
temp = scan.nextLine();
}