[英]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: 我正在尝试做的一个例子如下:
n
: 3 n
:3 "Cat", "Dog", "Mouse"
"Cat", "Dog", "Mouse"
[Cat][Dog][Mouse]
[Cat][Dog][Mouse]
[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: 运行代码时,我的代码会出现一些问题:
exp[i] = input.nextLine();
exp[i] = input.nextLine();
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: 您的代码存在多个问题:
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
之后声明数组。 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[i].split
exp[i].split
split
method is not doing what you think it's doing. split
方法未按照您认为的去做。 I think you are looking for the toCharArray()
method. toCharArray()
方法。 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. 要将呼叫
SetExpressions
在main
不会改变成员n
, exp
和tokens
,你期待。 Value of n
will be 0 by default and it remains so by the time exp
and tokens
get initialised. n
的值默认情况下为0,并且在时间exp
和tokens
初始化之前一直保持不变。 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.