[英]run java program in terminal through command line
我編寫了一個作業程序來讀取 2 個文件並輸出 2 個文件。 讀入的記錄文件將被整理並重新寫入結果文件。 讀入的指令文件將被執行,結果應生成到報告文件中。
問題是當我嘗試像這樣在終端中運行時:
cd desktop/lab/bin/java POS18sz.POS /Users/Jan/Desktop/recordFile.txt /Users/Jan/Desktop/instructionFile.txt resultFile.txt reportFile.txt
什么都沒有發生,即使我等了很長時間。 如果我嘗試關閉終端,它會說:“是否要關閉正在運行的程序?”。 代碼中也沒有錯誤的指示,我真的很困惑。
我在 Eclipse 中點擊運行,並且該包已自動編譯。 由於代碼有 5 個類並且很長,我應該提供哪部分代碼來使問題更清楚?
這是主類:包 POS18sz;
導入 java.util.Scanner; 導入 java.io.*;
公共類 POS { public static 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();
}
}
}
在您的代碼中,您忘記read
下一行
String temp = scan.nextLine();
while(scan.hasNextLine())
{
if(temp.startsWith("name")) {
.....
}
....
// read the next line
temp = scan.nextLine();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.