1

So I'm trying to fill a char array with user input. However I do not want to specify the length of the array myself, thus limiting the user to the amount they can input. For example my code below will only allow the user to enter 5 characters and then will exit. Code in any language would be fine. (The below is C#).

Console.Write("Enter a number of characters of your choice...click 1 to exit: ");
bool exitCondition;
int counter = 0;
char[] character = new char[5]; 
do
{
    exitCondition = false; 
    try
    {
        character[counter] = char.Parse(Console.ReadLine());
        if (character[counter] == '1')
            exitCondition = true; 
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error: {0}", ex.Message); 
    }
    counter++;
}
while (exitCondition == false && counter < 5); 
2
  • Please, just use the C# tag. Java!=C# Commented Dec 16, 2012 at 15:50
  • The OP specifies Code in any language would be fine. I think this may be to limit the bounds of code provided. Commented Dec 16, 2012 at 15:53

4 Answers 4

3

You need to use a collection, which allows the collection of elements to grow or shrink. In Java an ArrayList would be appropriate for your scenario. In C# you would most likely use a List.

C#

List<char> list = new List<char>();
list.Add('a');
list.Add('b');

Java

List<Character> list = new ArrayList<Character>();
list.add('a');
Sign up to request clarification or add additional context in comments.

Comments

3

Array's are static in size. Use java.util.ArrayList ( from Collection framework) which is resizable-array.

List<Character> chars = new ArrayList<Character>();
chars.add('a');
chars.add('c');
chars.add('d');

Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list.

Ref

4 Comments

Okay, interesting, I got to read up on lists I believe. Could you post an implementation of lists to solve my problem.
@sragavan ArrayList is implementation of List. Its having add method by which you can add values to List.
As a note, if you're using Java 7 or above, you don't need to specify the extending class on both sides, and List<Character> chars = new ArrayList<Character>(); can become: List<Character> chars = new ArrayList<>();.
@MrLore yap that is called diamond... :)
1

StringBuilder is all you need

StringBuidler sb = new StringBuilder();
sb.append(c);
...

then you can get char array from it if you really want it

char[] a = sb.toString().toCharArray();

though typically it is just converted into a String

 String s = sb.toString()

there is also a method to work with StringBuilder internal char[] directly

 char c = sb.charAt(i)

Comments

0

Alternatively if you really want to use only array for some hidden reason :), then you can ask user for number of input items and initialize array accordingly.

Thanks, Naval

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.