如何将字符串分割成数组

时间:2023-02-02 21:37:49

I am trying to split each line of a String read by my FileReader and BufferedReader. I am getting a syntax error at String [] x = Login.Split("");

我正在尝试分割由我的FileReader和BufferedReader读取的每一行字符串。我在String [] x = log . split("")处得到一个语法错误;

What method can I use to split the String? I will be adding a method to see if the string matches made by another input, but for now I need to figure out how to split a string and still have each line Separate.
(I am trying to find another way to make a login since I have java 8 and cannot use sql and the ucanaccess didn't seem to help either ) I also attempted to use a delimiter, but couldn't find a good applicable example.

我可以用什么方法来分割字符串?我将添加一个方法来查看字符串是否与另一个输入匹配,但是现在我需要弄清楚如何分割一个字符串,并且仍然要将每一行分开。(我正在寻找另一种登录方式,因为我有java 8,不能使用sql,而且ucanaccess似乎也没有帮助)我也尝试使用分隔符,但找不到一个合适的示例。

private void SubmitActionPerformed(java.awt.event.ActionEvent evt) {      
    try {
        FileReader FR = new FileReader("Login.txt");
        BufferedReader Login = new BufferedReader(FR);
        StringBuilder sb= new StringBuilder();

        String str;
        String [] x = Login.Split("");
        while ((str = Login.readLine()) !=null ) { 
            sb.append(str);
            sb.append("\n");
        }
        Login.close();
        FR.close();

        System.out.println(sb.toString());
        Scanner read = new Scanner(Login);

        } catch (Exception ex) {
            System.out.println("exception 2, " );
            ex.printStackTrace();
        }
    }  

3 个解决方案

#1


2  

I am not clear on what you want?

我不清楚你想要什么?

A : You want to put single seperate lines from your reader into an aray? Well as far as I know that the readline() method reads a line of text already.

A:你想要把你的读者的单行写成一行吗?就我所知,readline()方法已经读取了一行文本。

or

B You want to split the words in the line in that case you can try to use this : String[] x = Login.trim().split("//s"); //The RegEx will account for more than one space between words and the trim() will cut of spaces at the start and end of the line if any.

B在这种情况下,你想把单词分在一行中,你可以试着使用:String[] x = log .trim().split("//s");// RegEx将在单词之间占用一个以上的空间,如果有的话,trim()将在行的开始和结束处切割空间。

BUT I can see that you are not reading a line into your reader before trying to split the words in the array. So it is trying to split something that doesn't have any data in it.

但是我可以看到,在尝试分割数组中的单词之前,您并没有在读入器中读一行。它试图分割一个没有任何数据的东西。

Note I'm just starting in Java but I hope this helps.

注意,我刚开始使用Java,但我希望这能有所帮助。

#2


1  

Based upon your new comment

基于你的新评论

String str;
while ((str = Login.readLine()) !=null ) { 

    String [] x = str.Split(" ");

    // ?? new method to scan string array
    scanThis (x);
    sb.append(str);
    sb.append("\n");

}

}

void scanThis (String arr[]) 
{
  for (int x = ; x < arr.length; x++) {
  {
      if (arr[x].equals ("login")) {
          System.out.println ("Hurray");
          break;
      }
   } 
}     

#3


1  

I am getting a syntax error at String [] x = Login.Split("");

我在String [] x = log . split("")处得到一个语法错误;

No you aren't, you are getting a compiler error reading something like 'no such method'. That's not a syntax error.

不,你不是,你会得到一个编译错误,它会读取“没有这样的方法”之类的东西。这不是语法错误。

Login (stupid name) is a BufferedReader, and BufferedReader doesn't have a Split() method. String on the other hand does have a split() method (note the case). Presumably that is what you're looking for.

Login(愚蠢的名字)是BufferedReader, BufferedReader没有Split()方法。另一方面,String确实有一个split()方法(请注意这种情况)。大概这就是你要找的。

But splitting on an empty string isn't going to accomplish anything useful.

但是在一个空字符串上拆分并不能完成任何有用的工作。

#1


2  

I am not clear on what you want?

我不清楚你想要什么?

A : You want to put single seperate lines from your reader into an aray? Well as far as I know that the readline() method reads a line of text already.

A:你想要把你的读者的单行写成一行吗?就我所知,readline()方法已经读取了一行文本。

or

B You want to split the words in the line in that case you can try to use this : String[] x = Login.trim().split("//s"); //The RegEx will account for more than one space between words and the trim() will cut of spaces at the start and end of the line if any.

B在这种情况下,你想把单词分在一行中,你可以试着使用:String[] x = log .trim().split("//s");// RegEx将在单词之间占用一个以上的空间,如果有的话,trim()将在行的开始和结束处切割空间。

BUT I can see that you are not reading a line into your reader before trying to split the words in the array. So it is trying to split something that doesn't have any data in it.

但是我可以看到,在尝试分割数组中的单词之前,您并没有在读入器中读一行。它试图分割一个没有任何数据的东西。

Note I'm just starting in Java but I hope this helps.

注意,我刚开始使用Java,但我希望这能有所帮助。

#2


1  

Based upon your new comment

基于你的新评论

String str;
while ((str = Login.readLine()) !=null ) { 

    String [] x = str.Split(" ");

    // ?? new method to scan string array
    scanThis (x);
    sb.append(str);
    sb.append("\n");

}

}

void scanThis (String arr[]) 
{
  for (int x = ; x < arr.length; x++) {
  {
      if (arr[x].equals ("login")) {
          System.out.println ("Hurray");
          break;
      }
   } 
}     

#3


1  

I am getting a syntax error at String [] x = Login.Split("");

我在String [] x = log . split("")处得到一个语法错误;

No you aren't, you are getting a compiler error reading something like 'no such method'. That's not a syntax error.

不,你不是,你会得到一个编译错误,它会读取“没有这样的方法”之类的东西。这不是语法错误。

Login (stupid name) is a BufferedReader, and BufferedReader doesn't have a Split() method. String on the other hand does have a split() method (note the case). Presumably that is what you're looking for.

Login(愚蠢的名字)是BufferedReader, BufferedReader没有Split()方法。另一方面,String确实有一个split()方法(请注意这种情况)。大概这就是你要找的。

But splitting on an empty string isn't going to accomplish anything useful.

但是在一个空字符串上拆分并不能完成任何有用的工作。