繁体   English   中英

创建一个Java字符串数组

[英]Creating a Java String array

我不确定如何使程序理解文本文件中存在三个不同的字符串,以及如何将其添加到代码中? 尽管我对创建有趣的Java程序(如计算器等)有相当的经验,并且想继续进行下一步,但我从未创建过数组。

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

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

这是工作代码(我正在用“准备编程”运行-不确定为什么代码中不包含第一位):

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程序(如计算器等)有相当的经验,并且想继续进行下一步,但我从未创建过数组。

您使用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类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM