关于字符串数组的问题 [英] Question about String Arrays

查看:63
本文介绍了关于字符串数组的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.编写具有String数组且具有全局可见性的Java程序. 2.添加一种将给定字符串添加到字符串数组的方法.
3.添加一种在字符串数组中搜索给定字符串的方法.
4.添加一种在字符串数组中搜索给定字符的方法.该方法应计数并返回给定字符的出现.
5.编写一个适当的main方法,该方法从名为"input.txt"的文本文件中读取一些字符串,并将它们添加到String数组中.然后测试上面3和4中的方法.

这是我尝试过的代码,但是没有用!!

我该怎么办???

1.Write a Java program having a String array, with global visibility.`
2.Add a method that adds a given string to the string array.
3.Add a method that searches for a given string in the string array.
4.Add a method that searches for a given character in the string array. The method should count and returns the occurrence of the given character.
5.Write an appropriate main method that reads from a text file named "input.txt" some strings and adds them to the String array. Then test the methods in 3, and 4 above.

here is the code I tried but it is not working!!

What should I do????

package arrays;
import java.util.Scanner;
import java.io.*;

public class Arrays 
{
    static String [] testArray;
    
    public static void addString (String str, int index)
    {
        testArray[index] = str;  
    }
    
    public static void searchString(String x)
    {
       System.out.println(x.indexOf("this"));
    }
    
    public static void characterString(String str, String sub)
    {
        System.out.println(str.indexOf('s'));
    }

    public static void main(String[] args) throws FileNotFoundException 
    {           
        testArray = new String[6];
 
        Scanner in = new Scanner (new FileReader("input.txt"));
        int i=0;
        while (in.hasNext())
        {
          String j = in.next();
          addString (j,i);
          i++;  
        for(String s: testArray)
            System.out.println(s);
        }
    }
}


这是输入文件信息.


This is the input file info.

Hello
Girls!
This
is
Programming
class..

推荐答案

1.编写一个具有String数组且具有全局可见性的Java程序.
是的,我可以在那里看到它.您也应该将其标记为公开".这不是必需的,但通常这样做是指出该特定变量是公共的.
1.Write a Java program having a String array, with global visibility.
yepp, I can see that in there. You should mark it "public" too. That is not necessary, but it''s common done that way to point out that this specific variable is public.
public static String [] testArray;



2.添加一种将给定字符串添加到字符串数组的方法.
是的,满分在这里.
3.添加一种在字符串数组中搜索给定字符串的方法.
我希望这是一个返回布尔值的方法,该值指示所请求的字符串是否在数组中.



2.Add a method that adds a given string to the string array.
yepp, full points here.
3.Add a method that searches for a given string in the string array.
I would expect that to be a method that returns a boolean value indicating if the requested String is in the Array.

public static boolean searchString(String strRequest)
{
  foreach(String str: testArray){
    if(str.equlas(strRequest) return true; // direct return of find!
  }
  return false; // nothing found
}


但是我很确定您是否已经有返回值的函数?
您的代码也不起作用,您至少需要询问"该数组.

4.添加一种在字符串数组中搜索给定字符的方法.该方法应计数并返回给定字符的出现.
在您的代码上也不起作用.如果给定参数("x")在其中,则需要在数组中搜索每个String.并需要计数.因此,我希望该方法将一个int值返回给main.

5.编写一个合适的main方法,该方法从名为"input.txt"的文本文件中读取一些字符串,并将它们添加到String数组中.然后测试上面3和4中的方法.
您不调用函数.但是读取输入文件是可行的-继续添加其余部分!

只是一个很小的-以太变量和main位于最底端或两个都位于最上端.不要拆开.


But I''m nut sure if you already had functions returning a value?
Your code also does not work, you need to at least "ask" the Array.

4.Add a method that searches for a given character in the string array. The method should count and returns the occurrence of the given character.
Also non functional at your code. You need to search in the Array, each String if the given argument ("x") is in there. and counting is needed. So I expect that method to return a int value to the main.

5.Write an appropriate main method that reads from a text file named "input.txt" some strings and adds them to the String array. Then test the methods in 3, and 4 above.
You do not call your functions. But the reading of input file works - so go on and add the rest!

Just a small one - ether variables and main on bottom end or both on top end. Don''t split that.


这篇关于关于字符串数组的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆