Home > Software engineering >  Transfer the input on string array then to a char array
Transfer the input on string array then to a char array

Time:07-07

Im trying to practice for loops and transferring input to arrays. I have a problem on transferring an input to a string array then to a char array (a single letter). I have tried in.next().charAt(t) but I dont know why I am having an error. First I used charArr[t] = in.nextLine(). It said that I needed to convert it. Then I created a string array and used charArr[t] = string[t]; also said I need to convert it first. Is there anyway to transfer my input from the string array to the character array? Thank you

public static void main(String[] args) throws IOException 
{
    

    Scanner in = new Scanner(System.in);
    int range = 0;

 
    System.out.print("Please enter the range: ");
    range = Integer.parseInt(in.nextLine());

    String[] string = new String[range];
    char[] charArr = new char[range];
    System.out.println("Please enter the characters: ");
    for(int t = 0; t < range; t  ){
        string[t] = in.nextLine(); //First input to the string array
        charArr[t] = string[t]; //Attempt to transfer that letter/input
        
    }
    
}

CodePudding user response:

Consider the following code:

//The character array to store the string characters
char[] charArr = new char[range];

//Get the string
System.out.println("Please enter the characters: ");
String string = in.nextLine();

//Copy the string to the character array one letter at a time
for(int t = 0; t < string.length(); t  ){
    charArr[t] = string.charAt(t); 
}

Note how we use a single string to store the input String string = in.nextLine();, we don't need a string array. Once we have the string we can transfer the characters one at a time into the character array.

If you want to process multiple strings to store in a string array String[] string = new String[range]; then we can use a nested loop:

Scanner in = new Scanner(System.in);
int range = 0;


System.out.print("How many strings do you want to process?");
range = Integer.parseInt(in.nextLine());

String[] string = new String[range];
for(int t = 0; t < range; t  ){
    //We need a new char array for each input string
    char[] charArr = new char[range];

    //Get the text string
    System.out.println("Please enter the characters: ");
    string[t] = in.nextLine();

    //Copy the string to the character array one letter at a time
    for(int i = 0; i < string[t].length(); i  ){
        charArr[i] = string[t].charAt(i); 
    }
    //Print the character array here or add it to an array of arrays...
    //...
}

CodePudding user response:

If I'm understanding what you want to do correctly (convert each inputted string into an array of characters and store them in charArr), you need to make charArr two dimensional and then transfer each character of each inputted string into it. Try something like the following:

public static void main(String[] args) throws IOException {
    Scanner in = new Scanner(System.in);
    int range = 0;

    System.out.print("Please enter the range: ");
    range = Integer.parseInt(in.nextLine());

    String[] string = new String[range];
    // notice that this is now a two dimensional array. the first dimension
    // is range, and the second is the number of chars in each string
    char[][] charArr = new char[range][];
    System.out.println("Please enter the characters: ");
    for (int t = 0; t < range; t  ) {
        string[t] = in.nextLine();
        // String has a function toCharArray that can handle this
        charArr[t] = string[t].toCharArray();
        // alternately, you can write another loop
        charArr[t] = new char[string[t].length()];
        for (int charIndex = 0; charIndex < string[t].length(); charIndex  ) {
            charArr[t][charIndex] = string[t].charAt(charIndex);
        }
    }
}

Note that I outlined two separate ways of doing the same thing above. Please remove the one you don't want to use. Between them, String.toCharArray is the more concise and idiomatic option.

  • Related