Sunday, March 30, 2014

char array to list not working...
































































Example code that works:
































































































Java Code:






































































List<String> list = Arrays.asList(strDays);




































































My code that doesn't work:































































Java Code:






































































List<char> list = Arrays.asList(userInputChars);




































































Error that I get:































Assignment6_2.java.java:45: error: unexpected type































































List<char> list = Arrays.asList(userInputChars);































































^































































required: reference































































found: char































































1 error
































































The entire program (that works, except for the error above):
































































































Java Code:






































































import java.util.Collections;
import java.util.List;
import java.util.Arrays;
import java.util.Scanner;

class Assignment6_2 {
public static void main(String[] args) {

// receive user input and store as string 'userText'
Scanner user_input = new Scanner(System.in);
System.out.print("Enter some text:");

// note: to store a string with spaces, you need to use .nextLine
String userText = user_input.nextLine();

// display the number of characters of 'userText'
System.out.println(userText + " has " + userText.length() + " characters (including spaces if any).");

// create character array and set length based on length of 'userText'
char[] userInputChars = new char [userText.length()];

// for the length of the string 'userText'
for (int counter = 0; counter < userInputChars.length; counter++) {

// store each character of 'userText' in the character array 'userInputChars'
userInputChars[counter] = userText.charAt(counter);

// print each individual character of the array on its own line
System.out.println(userInputChars[counter]);

}

// create a list from char array
List<char> list = Arrays.asList(userInputChars);

}

}




































































Thanks,































Joe































































































































No comments:

Post a Comment