简体   繁体   English

将字符串输入数组,然后将字符串拆分为单个字符数组

[英]Input Strings into Array and then Splitting the Strings into a Single Char Array

I am writing a program in java that allows a user to enter n and puts them strings into an array. 我正在用Java编写一个程序,该程序允许用户输入n并将其字符串放入数组中。 Then, the program takes those strings and splits them up into single char characters and places those characters into a new char array. 然后,程序将这些字符串分割成单个char字符,并将这些字符放入新的char数组中。

An example of what I'm trying to do is below: 我正在尝试做的一个例子如下:

  • Input n : 3 输入n :3
  • Input strings: "Cat", "Dog", "Mouse" 输入字符串: "Cat", "Dog", "Mouse"
  • Original Input Array: [Cat][Dog][Mouse] 原始输入数组: [Cat][Dog][Mouse]
  • Desired Output Array: [C][a][t][D][o][g][M][o][u][s][e] 所需的输出数组: [C][a][t][D][o][g][M][o][u][s][e]

There are a few problems that occur with my code when I run it: 运行代码时,我的代码会出现一些问题:

  1. I get an exception error with the line of code that takes in my input strings. 我的输入字符串中的代码行出现异常错误。 Line of code: exp[i] = input.nextLine(); 代码行: exp[i] = input.nextLine();
  2. Netbeans IDE is telling me it can not find the symbol for the split function I'm trying to use. Netbeans IDE告诉我找不到我要使用的split函数的符号。

I am not sure what is wrong with my code, but I feel like at least the part where I input the strings should be working. 我不确定我的代码有什么问题,但是我感觉至少我输入字符串的那部分应该可以正常工作。 I realize I don't have any output code yet, as I am just trying to get the input part working right now. 我意识到我还没有任何输出代码,因为我只是想立即使输入部分正常工作。 Any suggestions would be appreciated! 任何建议,将不胜感激!

public class Strings {
  Scanner input = new Scanner(System.in);
  int n; //number of strings
  String[] exp = new String[n]; //input strings
  char[] tokens = new char[n]; //individual char characters

  //Gather data

  public void SetNumberStrings(){
      n = input.nextInt();
  }

  public void SetExpressions(){
      for (int i = 0; i < n; i++){
          exp[i] = input.nextLine();
      }
  }

  public void SplitExpressions(){
      for (int i = 0; i < n; i++){
          tokens[i] = exp.split(" ");
      }
  }
  public static void main(String[] args) {
       Strings exp1 = new Strings();
       exp1.SetNumberStrings();
       exp1.SetExpressions();
       exp1.SplitExpressions();

  }
}

There are multiple issues with your code: 您的代码存在多个问题:

  1. The array initialization is not working like that. 数组初始化无法像这样工作。 Having int n; //number of strings 具有int n; //number of strings int n; //number of strings as a class field, means it will be initialized with 0, resulting in your arrays having a length of 0. To fix this you may declare your arrays after giving a value to n . int n; //number of strings作为类字段int n; //number of strings ,表示它将用0初始化,从而导致数组的长度为0。要解决此问题,可以在将值赋给n之后声明数组。
  2. At line tokens[i] = exp.split(" "); 在行tokens[i] = exp.split(" "); there is indeed a compilation error, because you are trying to call the split method on the exp array, but the split method is from String class. 确实存在编译错误,因为您试图在exp数组上调用split方法,但是split方法来自String类。 So you would need to call exp[i].split 因此,您需要调用exp[i].split
  3. split method is not doing what you think it's doing. split方法未按照您认为的去做。 I think you are looking for the toCharArray() method. 我认为您正在寻找toCharArray()方法。
  4. tokens array should have the length of all the strings that you scanned. tokens数组应具有您扫描的所有字符串的长度。

There are several issues with your program. 您的程序存在几个问题。 The initialisation of class member variables seems to be wrong (considering your requirement). 类成员变量的初始化似乎是错误的(考虑您的要求)。 The call to SetExpressions in main will not alter the members n , exp and tokens as you are expecting. 要将呼叫SetExpressionsmain不会改变成员nexptokens ,你期待。 Value of n will be 0 by default and it remains so by the time exp and tokens get initialised. n的值默认情况下为0,并且在时间exptokens初始化之前一直保持不变。 Hence, when you try to assign the input strings to the elements of exp , you get java.lang.ArrayIndexOutOfBoundsException . 因此,当您尝试将输入字符串分配给exp的元素时,将得到java.lang.ArrayIndexOutOfBoundsException

Further, you are trying to invoke split method on a String[] object ( exp ), which is wrong (a String has split() method not String[] ). 此外,您尝试在String[]对象( exp )上调用split方法,这是错误的( String具有split()方法而不是String[] )。

If you just trying to take a string of words and convert them all into a char[] , then may be you can simply concatenate all the words into a single String object, and then do a toCharArray() on that. 如果您只是尝试获取一串单词并将其全部转换为char[] ,则可以将所有单词简单地连接到单个String对象中,然后toCharArray()对象执行toCharArray() Hope this helps. 希望这可以帮助。

import java.util.Scanner;

public class Strings {
  Scanner input = new Scanner(System.in);
  int n; //number of strings
  String[] exp; //Fix...This line was causing exception in your program We should not initialize array here because at this point we don't have the length from user
  char[] tokens; //Fix...We are not confirmed of the length of token array at this point

  //Gather data

  public void SetNumberStrings(){
      n = input.nextInt();
  }

  public void SetExpressions(){
      exp= new String[n];
      for (int i = 0; i <n; i++){
          exp[i] = input.next();//.next() function should be used.
      }
  }

  public void SplitExpressions(){
      int length=0;
      int l=0;
      for(int i=0; i<exp.length; i++) {
          length+= exp[i].length();
      }//this loop will calculate the required length for character array
      tokens =new char[length];//Giving length of array here
      for(int i=0; i<exp.length; i++) {
          for(int j=0; j<exp[i].length(); j++) {
              tokens[l]=exp[i].charAt(j);//
              l++;
          }
      }
  }
  public static void main(String[] args) {
       Strings exp1 = new Strings();
       exp1.SetNumberStrings();
       exp1.SetExpressions();
       exp1.SplitExpressions();

  }
}

I have cleared all the problems in the code. 我已经清除了代码中的所有问题。 As mentioned in comments.You had initialized all class variables in class, this is not a good programming practice. 如注释中所述。您已经初始化了类中的所有类变量,这不是一个好的编程习惯。

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

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