Results 1 to 8 of 8
  1. #1
    jblaven is offline Member
    Join Date
    Mar 2014
    Posts
    8
    Rep Power
    0

    Question 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

  2. #2
    jblaven is offline Member
    Join Date
    Mar 2014
    Posts
    8
    Rep Power
    0

    Default Re: char array to list not working...

    Thought I'd post what I found... My code didn't work because char is a primitive type.

    It appears that this works (as I didn't get an error when I ran it):

    Java Code:
    List<char[]> aslist = Arrays.asList(userInputChars);
    Thanks,

    Joe

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,418
    Rep Power
    5

    Default Re: char array to list not working...

    That probably does not do what you want. It creates a list of type char[] which means that each element of the List is a separate array of characters. What you probably want is the following:

    Java Code:
    List<Character> list = Arrays.asList('a','b', 'd');
    System.out.println(list);
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    jblaven is offline Member
    Join Date
    Mar 2014
    Posts
    8
    Rep Power
    0

    Default Re: char array to list not working...

    Thanks Jim,

    Question:

    Since my initial array contains a user's input, would I place your code example within a for loop to populate the list with the user's input from the array? So instead of ('a','b', 'd'), I'd be using some sort of counter for the array index?

    Hope that question makes sense.

    Thanks,

    Joe

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,418
    Rep Power
    5

    Default Re: char array to list not working...

    Yes. That would make the most sense. Because you can't do the following:

    Java Code:
    char[] userInputChars = {'a', 'b','c'};
    List<Character> list = Arrays.asList(userInputChars);  // this won't work.
    Otherwise you would have to do this:

    Java Code:
    for (char c : userInputChars) {
       list.add(c);
    }
    So you may as well do it from the start.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    jblaven is offline Member
    Join Date
    Mar 2014
    Posts
    8
    Rep Power
    0

    Default Re: char array to list not working...

    Would I be thinking correctly to place the following at the bottom of my existing for loop?

    Java Code:
    List<Character> charList = Arrays.asList(userInputChars[counter]);

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,418
    Rep Power
    5

    Default Re: char array to list not working...

    No. Create your list before your for loop.

    Java Code:
    List<Character> charList = new ArrayList<>();
    then in your for loop do this:

    Java Code:
    charList.add(userInputChars[counter]);
    but you could have also done it this way.

    Java Code:
    charList.add(userText.charAt(counter));
    then you don't need the array.

    One question though. Do you want to create a list of characters as I thought, or do you want to save each entered word as an array of characters? If the latter, your first method, List<char[]> charList was correct.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    jblaven is offline Member
    Join Date
    Mar 2014
    Posts
    8
    Rep Power
    0

    Default Re: char array to list not working...

    Quote Originally Posted by jim829 View Post
    No. Create your list before your for loop.

    Java Code:
    List<Character> charList = new ArrayList<>();
    then in your for loop do this:

    Java Code:
    charList.add(userInputChars[counter]);
    but you could have also done it this way.

    Java Code:
    charList.add(userText.charAt(counter));
    then you don't need the array.

    One question though. Do you want to create a list of characters as I thought, or do you want to save each entered word as an array of characters? If the latter, your first method, List<char[]> charList was correct.

    Regards,
    Jim
    Jim,

    Thanks for your help!

    Joe

Similar Threads

  1. Replies: 3
    Last Post: 07-19-2013, 10:40 PM
  2. Char to keycode=not working?
    By maximumtech in forum New To Java
    Replies: 6
    Last Post: 05-20-2012, 10:31 PM
  3. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 11:32 AM
  4. Replies: 2
    Last Post: 03-08-2011, 06:20 AM
  5. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 11:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •