2014-02-23 204 views
0

我不知道如何让程序明白文本文件中有三个不同的字符串,我如何将它添加到我的代码中?我从来没有建立,虽然我相当与创建有趣的Java程序(如计算器和等)之前经历了一个数组,并要移动到下一个步骤创建一个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 
    } 
} 

和我所试图做的是创建一个数组,它需要三个输入(字符串,这将是代码中的字符串1,2和3),如下文所示:您好,您好吗(字符串1) 我很好(字符串2) 很棒(stirng 3)

我不知道如何让程序明白文本文件中有三个不同的字符串,并且如何将它添加到我的代码中?我从来没有建立,虽然我相当与创建有趣的Java程序(如计算器和等)经验丰富,要移动到下一个步骤

回答

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类。

+0

也许增加一个他们的用法的例子。如果我是新人,你的解释对我来说没有多大意义。 – christopher

相关问题