创建Java String数组

时间:2014-02-23 19:57:23

标签: java arrays string

我不确定如何让程序理解文本文件中有三个不同的字符串,如何将其添加到我的代码中?我之前从未创建过数组,尽管我在创建有趣的Java程序(如计算器等)方面经验丰富,并希望进入下一步

我制作了一个执行以下操作的程序:

  • 程序功能:    要求用户输入字符串。    要求用户输入第二个字符串,该字符串将替换第一个字符串中每个单词的最后两个字符。    要求用户输入第三个字符串,第一个字符将替换第一个字符串中每个单词的每个字母“I”。 *如果第一个字符串中的单词少于两个字符且不包含I-则字符串将保持不变。

这是工作代码(我正在运行Ready to Program - 不知道为什么第一位不包含在代码中):

import java.io.*;
import java.util.*;


public class StringModifications

{

 private String input1, input2, input3; // Values only used within this method
    public String information; 

    // Constructor
    public StringModifications ()
    {
        // Initialize class data to 0
        this.input1 = "";
        this.input2 = "";
        this.input3 = "";
    }

    public void setInputStrings (String s1, String s2, String s3)
    {
        // Method to set class data
        this.input1 = s1; // Equal to string 1
        this.input2 = s2;
        this.input3 = s3;
    }

    public String processStrings ()
    {
        StringTokenizer stok = new StringTokenizer (this.input1); // Splits first input string (word by word)
        StringBuffer strBuff = new StringBuffer ("");

        String outstring = ""; // Initialize variable to 0

        while (stok.hasMoreTokens () == true) // As long as there are more words in the string:
        {
            String word = stok.nextToken ();
            if (word.length () > 2)
            {

                word = word.substring (0, word.length () - 2); // Removes the last two letters of each word in the first string
                word = word.concat (this.input2); // Adds the second input to the end of the first string

                char letter = input3.charAt (0); // Finds the first letter of the third input
                word = word.replace ('I', letter); // Replaces letter I in first string with first letter of third input
            }


            outstring = outstring + word + " "; // Adds a space between each word when output
        }

        return outstring;
    }


    public static void main (String[] args) throws IOException
    {

        String string1, string2, string3;

        BufferedReader keyboard = new BufferedReader (  // // Define the input stream reader
                new InputStreamReader (System.in));

        System.out.println ("Enter first string"); // User inputs the first string
        string1 = keyboard.readLine ();

        System.out.println ("Enter second string"); // User inputs the econd string
        string2 = keyboard.readLine ();

        System.out.println ("Enter third string"); // User inputs the third string
        string3 = keyboard.readLine ();

        StringModifications strProc = new StringModifications ();

        strProc.setInputStrings (string1, string2, string3); // Sends values to method (e.g. this.input1 = stirng 1)

         PersonalInfo pi = new PersonalInfo();

        String out = strProc.processStrings (); // String (e.g. this.input1) sent through processStrings method before output

        System.out.println ("Original Input: " + string1); // Displays the original input
        System.out.println ("Modified Input: " + out); // Displays the modified input
    }
}

我想要做的是创建一个数组,它接受三个输入(字符串,代码中的string1,2和3),如下文所示: 1 你好,你好(字符串1) 我很好(字符串2) 很棒(搅拌3)

我不确定如何让程序理解文本文件中有三个不同的字符串,如何将其添加到我的代码中?我之前从未创建过数组,尽管我在创建有趣的Java程序(如计算器等)方面经验丰富,并希望进入下一步

1 个答案:

答案 0 :(得分:0)

您使用String[] str = new String[n]声明并初始化新的String数组。这是一个固定长度为n的静态数组,其中n在初始化期间必须知道。通过str[i]访问单个元素,其中i是区间[ 0,n )中元素的索引。

使用示例:

String[] phrases = new String[3];
phrases[0] = "Hello, how are you?";
phrases[1] = "I am good";
phrases[2] = "Great";

System.out.println("What phrase would you wish to see?");
Scanner in = new Scanner(System.in);
System.out.println(phrases[in.nextInt()]);
in.close();

如果您需要一个包含可变数量元素的动态数组,我建议您查看ArrayList类。