多线程生产者和消费者

时间:2016-07-09 19:25:37

标签: java multithreading input substring

我正在尝试创建一个接受用户信息的程序,并创建一个文件,其中包含有关游戏玩家的幻想团队的信息。"这是通过创建一个71个字符的字符串来完成的, 当用户输入" new"命令它应该要求填写71个字符的值: (5 + 26 + 26 + 5 + 9) 5-请求1-20之间的ID(将ID转换为字符串) 26-询问球员名字 26-询问团队名称 5-要求玩家技能等级在0到99之间(将技能等级转换为字符串) 9-草案日期(例如,2013年5月18日)

如果球队名称或日期字段太短,则应使用空格填充,如果太长则应将其切断。 然后,记录应写在ID指示的记录位置。完成此操作后,再次询问用户命令。

对于" old"命令它应该询问用户的ID,索引到文件并检索记录,DIsplay将记录回叫给用户。完成此操作后,再次询问用户命令。

"结束"命令应该简单地关闭程序。

到目前为止,这是我的代码,我似乎被困在" new"特别命令。我被困在代码中,我需要制作71个字符长度的字符串(5 + 26 + 26 + 5 + 9)我相信我需要使用.substring来使每个输入正确的长度。我不知道如何编码这个以及如何处理一个长的输入,最好我宁愿采取第一个正确的数量作为字符串部分。如果输入的格式不正确,我还会如何要求用户输入。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

import javax.swing.JOptionPane;
    public class Main{
    final private static int MAX_RECORD_NUMBER = 20;
    final private static int RECORD_LENGTH = 71;
    public static void main(String [] args) throws FileNotFoundException, IOException 
                        //throws NumberFormatException, NFException;
                        {
        File loc = new File("notepad.txt");
        RandomAccessFile store = new  RandomAccessFile(loc, "rw");
        String Dummy = "01234567890123456789012345678901234567890123456789012345678901234567890"; //71 char for RECORD_LENGTH
        for (int i = 0; i< MAX_RECORD_NUMBER; ++i){
            {
                store.writeUTF(Dummy); //fill file with dummy records
            }
            String idNumber = "";
            String playerName = "";
            String playerTeam = "";
            String playerSkill = "";
            String draftDate = "";
            String record = "";
            int recLocation = 0;
            String where = "0";
            String cmd = "start";

            while (cmd.compareToIgnoreCase("end") != 0 )
            {
                cmd = JOptionPane.showInputDialog(null, "Please input a command:'");
                if (cmd.compareToIgnoreCase("new") == 0 )
                {
                // Command is "new" - user wants to make a new record
                idNumber = JOptionPane.showInputDialog(null, "Please input an ID Number between 1 and 20: ");    //5 char total

                playerName = JOptionPane.showInputDialog(null, "Please input player's full name: ");             // 26 char total

                playerTeam = JOptionPane.showInputDialog(null, "Please input player's team name: ");             //26 total char

                playerSkill = JOptionPane.showInputDialog(null, "Please input player skill level between 0 and 99: "); //5 char total

                draftDate = JOptionPane.showInputDialog(null, "Please input the player draft date(Format 01Jan2014): "); //9 char total
                record = "" + idNumber + playerName + playerTeam + playerSkill + draftDate;
                assert record.length() == RECORD_LENGTH;
                if (recLocation == 0)
                {
                    recLocation = 1;
                }
                store.seek((RECORD_LENGTH+2) * (recLocation-1));
                store.writeUTF(record);
            }
            if  (cmd.compareToIgnoreCase("old") == 0 )
            {
                //Command is "old" - user wants to see an existing record
                where = JOptionPane.showInputDialog(null, "Input rec number");
                recLocation = Integer.parseInt(where);  
                store.seek((RECORD_LENGTH+2) * (recLocation-1));
                record = store.readUTF();
                JOptionPane.showMessageDialog(null,  record);
                }
            if (cmd.compareToIgnoreCase("end") == 0 )
            {
                //end command terminate program
            }
            }
    }
    }
    }

0 个答案:

没有答案