简体   繁体   中英

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. Then, the program takes those strings and splits them up into single char characters and places those characters into a new char array.

An example of what I'm trying to do is below:

  • Input n : 3
  • Input strings: "Cat", "Dog", "Mouse"
  • Original Input Array: [Cat][Dog][Mouse]
  • Desired Output Array: [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();
  2. Netbeans IDE is telling me it can not find the symbol for the split function I'm trying to use.

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 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 .
  2. At line 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. So you would need to call exp[i].split
  3. split method is not doing what you think it's doing. I think you are looking for the toCharArray() method.
  4. tokens array should have the length of all the strings that you scanned.

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. Value of n will be 0 by default and it remains so by the time exp and tokens get initialised. Hence, when you try to assign the input strings to the elements of exp , you get 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[] ).

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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