首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >通过命令行在终端运行java程序

通过命令行在终端运行java程序
EN

Stack Overflow用户
提问于 2018-10-24 08:02:16
回答 1查看 91关注 0票数 -3

我已经写了一个任务程序来读入2个文件和输出2个文件。读取的记录文件将被组织并重写到结果文件中。将执行写入指令文件,并将结果生成报告文件。

问题是,当我尝试在终端中运行时,如下所示:

代码语言:javascript
复制
cd desktop/lab/bin/java POS18sz.POS /Users/Jan/Desktop/recordFile.txt /Users/Jan/Desktop/instructionFile.txt resultFile.txt reportFile.txt

什么也没发生,甚至我等了很长时间。如果我试图关闭终端,它会说:“如果你想关闭正在运行的程序?”在代码中也没有错误的指示,我真的很困惑。

我在Eclipse中点击run,包已经自动编译了。由于代码有5个类,并且相当长,我应该提供代码的哪一部分来使问题更清晰?

下面是主类: package POS18sz;

导入java.util.Scanner;导入java.io.*;

公共类位置{ public static void main(String args[]) {

代码语言:javascript
复制
if(args.length == 4) {  
    Processor pp = new Processor(args);
    pp.recordFile();        
    pp.instructionFile();
    pp.resultFile();
    pp.reportFile();

    }
}
}

下面是文件处理器类: package POS18sz;

代码语言:javascript
复制
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();
    }

}

}

EN

回答 1

Stack Overflow用户

发布于 2018-10-24 08:30:14

在您的代码中,您忘记了read下一行

代码语言:javascript
复制
String temp = scan.nextLine();
while(scan.hasNextLine())
{  

     if(temp.startsWith("name")) {
     .....
     }
     ....

     // read the next line
     temp = scan.nextLine();
}              
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52959345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档