Casting a String to a Character Array

 

Introduction.  Back when we were working with switch code segments I needed to make use of arrays in order to turn a user's string input into a character for a switch.  I want to revisit that program a bit to make certain you understand why we based the switch on element choice[0] in the character array.

The next program will illustrate a few things we have worked with so far this semester in terms of llops, built-in methods of strings and characters, arrays and displaying output.

This application should be called StringToArray.java.

 

import javax.swing.JOptionPane;
import javax.swing.JTextArea;

public class StringToArray
{

public static void main( String args[ ])
{

String output = "";
String userInput;
char userInputArray[ ];

userInput = JOptionPane.showInputDialog("Please enter a string with some spaces");

// casting the string to a character array
userInputArray = userInput.toCharArray();

output = "You typed in:\n" + userInput + "\n\n";

output = output + "Printing out the character array\nwith a new line for each blank:\n";

// looping through the character array
for(int i=0; i < userInputArray.length; i++)
{

// need to find blanks in a chracter array
if (Character.isSpaceChar(userInputArray[i]))

// output a new line for every blank
output = output + "\n ";

else

// output the character in the array
output = output + userInputArray[i] + " ";

}

// putting the output into a JTextArea
JTextArea outputArea = new JTextArea();
outputArea.setText(output);

// displaying the JTextArea in a showMessageDialog
JOptionPane.showMessageDialog(null, outputArea,
"Casting a string to a char array",
JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

}

}

 

I ran the program with the following input string.

 

 

This resulted in the following output.

 

 

Code Discussion.  We will use our usual outline form to discuss the code.

  • import javax.swing.JOptionPane
  • import javax.swing.JTextArea
  • declare the overall inclusive class StringToArray

     

  • declare method main( )
    • declare and initialize a string for output
    • declare a string for userInput
    • declare a char array to eventually contain the user's input
    • use the showInputDialog( ) method of JOptionPane to get a string with spaces from the user
    • use the toCharArray( ) method of String class to convert the string to an array of characters
    • echo the user's inputs in the output string
    • append a brief description of how the user's input will be manipulated and displayed
      • blanks result in new lines
      • all other characters are echoed with an extra space in between
    • use a for loop to run through the character array userInputArray
      • use the isSpaceChar( ) method in the character class to test whether each element in the array is a blank
        • if it is blank
          • append a new line to the output string
        • if it isn't blank
          • append the array element plus a blank to the output string
    • declare and initialize a JTextArea to be used for output display
    • setText( ) of the outputArea to the output string
    • display the outputArea using the showMessageDialog( ) method of the JOptionPane class
  • end the program with System.exit(0)

You can play with this all you want.  It provides another example of working with arrays.  It should also help make more sense of the earlier code for getting user inputs into a useful form in order to get a switch code segment to work.

You should now be able to think of other ways to get this to work by using some sort of built-in string method to grab the first character or the user's input and assigning it to a character for switching.