I have written an assignment program to read in 2 files and output 2 files. The read-in record file will be organized and re-wrote into result file. The read-in instruction file will be executed and the outcomes should be generated into the report file.
The problem is when I try to run in terminal like this:
cd desktop/lab/bin/java POS18sz.POS /Users/Jan/Desktop/recordFile.txt /Users/Jan/Desktop/instructionFile.txt resultFile.txt reportFile.txt
nothing happens, even I wait for a long time. If I try to close the terminal, it said: "if you want to close the running program? ". There are no wrong indication in the code as well, I am really confused.
I hit run in Eclipse, and the package has automatically complied. Since the code has 5 classes and is pretty long, which part of code should I provided to make the problem clearer?
Here is the main class: package POS18sz;
import java.util.Scanner; import java.io.*;
public class POS { public static void main(String args[]) {
if(args.length == 4) {
Processor pp = new Processor(args);
pp.recordFile();
pp.instructionFile();
pp.resultFile();
pp.reportFile();
}
}
}
Here is the file processor class: package 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();
}
}
}
In your code, you are forgetting to read
the next line
String temp = scan.nextLine();
while(scan.hasNextLine())
{
if(temp.startsWith("name")) {
.....
}
....
// read the next line
temp = scan.nextLine();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.