如何编写此代码的main方法?

时间:2014-05-27 03:55:25

标签: java

以下是我的Simmulation.java文件,我应该编写main方法来使用它。但我不知道该怎么做。

我尝试过如下,但它没有用!

public static void main(String args[])
{
    new Simmulation(args[0]);       
} 

非常感谢任何帮助。提前谢谢

这是我的Simmulation.java文件

import java.io.File;
import java.util.LinkedList;
import java.util.Queue;
import java.util.*;
import java.util.Scanner;
import java.io.*;

public class Simmulation implements Operation
{
    Queue < CashewPallet > inputQueue = new LinkedList < CashewPallet > ();
    Stack < CashewPallet > stBay1 = new Stack < CashewPallet > ();
    Stack < CashewPallet > stBay2 = new Stack < CashewPallet > ();
    FileOutputStream fout4;
    PrintWriter pw;
    static int tick = 0;
    CashewPallet c1;
    String temp;
    Scanner sc;

    public Simmulation(String fn)
    {
        int index = 0;
        String nutType = "";
        int id = 0;

        Scanner s2;

        try
        {
            sc = new Scanner(new File(fn));
            fout4 = new FileOutputStream("nuts.txt");
            pw = new PrintWriter(fout4, true);
            String eol = System.getProperty("line.separator"); // Reading string line by line
            while (sc.hasNextLine())
            {
                tick++;
                s2 = new Scanner(sc.nextLine());

                if (s2.hasNext())
                {
                    while (s2.hasNext())
                    {
                        String s = s2.next();

                        if (index == 0)
                        {
                            nutType = s;
                        }
                        else
                        {
                            id = Integer.parseInt(s);
                        }
                        index++;
                    }

                    System.out.println("Nuttype " + nutType + " Id is " + id + "tick " + tick);

                    if ((nutType.equalsIgnoreCase("A") || nutType.equalsIgnoreCase("P") || nutType.equalsIgnoreCase("C") || nutType.equalsIgnoreCase("W")) && id != 0)
                        inputQueue.add(new CashewPallet(nutType.toUpperCase(), id));

                    System.out.println("Size of Queue " + inputQueue.size());

                    int k = 0;
                    if (!inputQueue.isEmpty())
                    {
                        while (inputQueue.size() > k)
                        {
                            // stBay1.push(inputQueue.poll());
                            process(inputQueue.poll());
                            k++;
                        }
                        // System.out.println("Size of input "+inputQueue.size() +" Size of stay "+stBay1.size());
                    }
                }
                else
                {
                    fout4.write(" ".getBytes());
                }

                index = 0;

                if (!stBay2.isEmpty())
                {
                    while (!stBay2.isEmpty())
                    {
                        c1 = stBay2.pop();
                        temp = tick + " " + c1.getNutType() + " " + c1.getId() + eol;

                        fout4.write(temp.getBytes());

                    }
                    // System.out.println("Nut final "+ stBay2.peek().getNutType());
                }
                else
                {
                    temp = tick + eol;
                    fout4.write(temp.getBytes());
                }
            }
        }
        catch (Exception e)
        {
            System.out.println("Exception " + e);
        }
        closeStream();
    }

    public CashewPallet process(CashewPallet c)
    {
        //   CashewPallet  c=new CashewPallet();
        int k = 0;
        //    while(stBay.size()>k)
        //   {
        //   c=stBay.pop();
        String operation = c.getNutType();

        if (c.getPriority() == 1)
        {
            shelling(c);
            washing(c);
            packing(c);
            //stBay2.push(c);
        }
        else
        {
            switch (operation)
            {
            case "A":
                shelling(c);
                washing(c);
                packing(c);
                break;

            case "C":
                washing(c);
                packing(c);
                break;

            case "W":
                washing(c);
                shelling(c);
                packing(c);
                break;
            }
        }

        return c;
    }

    public void closeStream()
    {
        try
        {
            fout4.close();
        }
        catch (Exception e)
        {
        }
    }

    public boolean shelling(CashewPallet c)
    {
        // for(int i=0;i<20; i++)
        {
            System.out.println("Performing Shelling for  " + c.getNutType());
        }
        return true;
    }
    public boolean washing(CashewPallet c)
    {
        // for(int i=0;i<20; i++)
        {
            System.out.println("Performing Washing for  " + c.getNutType());
        }
        return true;
    }

    public boolean packing(CashewPallet c)
    {
        //  for(int i=0;i<20; i++)
        {
            System.out.println("Performing Packing for  " + c.getNutType());
        }
        stBay2.push(c);
        return true;
    }

1 个答案:

答案 0 :(得分:2)

问题是您没有将任何参数传递给程序。因此args的长度为0.您可以尝试检查在使用它之前传递的args的长度。

if (args.length > 0)
    new Simulation(args[0]);
else
    new Simulation("Default value");

这应该可以解决你的问题。