【问题标题】:How do I use change this code to be without any arrays?如何使用将此代码更改为没有任何数组?
【发布时间】:2017-09-28 12:55:50
【问题描述】:

嘿 StackOverflow 社区,我想就我正在处理的学校作业寻求一些帮助。总结作业,我们基本上要创建一个文本文件,并在一行上写两件事。然后程序要求用户输入,如果该输入与任何一行的第一件事匹配,则打印第二件事。

例子:

行:“brb马上回来” 用户输入:“brb” 输出:“马上回来”

我已经成功了,这是我的代码。你可以运行它来看看它做了什么更清楚。

// The "NetSpeak_raminAmiri" class.
import java.io.*;
public class NetSpeak_raminAmiri
{
    public static void main (String[] args)
    {
        sendLines ();
        readLines ();
    } // main method


    public static void sendLines ()
    {
        try
        {
            FileWriter fw = new FileWriter ("net.txt");
            PrintWriter pw = new PrintWriter (fw);

            pw.println ("brb\tbe right back");
            pw.println ("lol\tlaugh out loud");
            pw.println ("g2g\tgot got go");
            pw.println ("d8\tdate");
            pw.println ("h8\thate");
            pw.println ("luv\tlove");
            pw.println ("pos\tparents over shoulder");
            pw.println ("u\tyou");
            pw.println ("sup\twhat's up");
            pw.println ("yolo\tyou only live once");
            pw.println ("smh\tshake my head");
            pw.println ("lmao\tlaugh my ass off");
            pw.println ("ttyl\ttalk to you later");
            pw.println ("idc\ti don't care");
            pw.println ("idk\ti don't know");
            pw.println ("ily\ti love you");
            pw.println ("bae\tdanish word for poop");
            pw.println ("omg\toh my god");
            pw.println ("tmi\ttoo much information");
            pw.println ("tbh\tto be honest");
            pw.println ("jk\tjust kidding");
            pw.println ("ftw\tfor the win");
            pw.println ("np\tno problem");

            pw.close ();
        }
        catch (IOException e)
        {
        }
    } //sendLines method


    public static void readLines ()
    {
        try
        {
            FileReader fr = new FileReader ("speak.txt");
            BufferedReader br = new BufferedReader (fr);
            String input;
            String line;

            System.out.println ("What net-speak would you like to translate?");
            input = In.getString ();

            while ((line = br.readLine ()) != null)
            {
                String translate[] = line.split ("\t");
                for (int i = 0 ; i < translate.length - 1 ; i++)
                {
                    if (input.equals (translate [i]))
                    {
                        System.out.println (translate [i + 1]);
                    }
                }
            }
        }
        catch (IOException e)
        {
            e.printStackTrace ();
        }
    } //readLines method
} // NetSpeak_raminAmiri class

一切都很好,直到我注意到,用小写字母:“注意:不要使用数组” 现在我被困住了。

我需要帮助来弄清楚如何做与我编写的代码相同的事情,但没有数组。有什么办法吗?

【问题讨论】:

  • 使用Map&lt;String, String&gt;,例如HashMap&lt;String, String&gt;

标签: java arrays filereader filewriter


【解决方案1】:

你可以使用 IndexOf 和 Substring

        while ((line = br.readLine()) != null)
        {
            int p = line.IndexOf('\t');
            string key = line.Substring(0, p);
            if (input.equals(key))
            {
                System.out.println(line.Substring(p+1));
                break;
            }
        }

【讨论】:

    猜你喜欢
    • 2021-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多