3

I have a character array, which is declared as char[ ] array = new char[20]; Regarding this, I have the following queries:

  • Is the default value at each location, '\0000' ?

  • How do i append characters into this array ? ( like for instance, each time i ask the user to input a alphabet and i have store the alphabets in order inside the array)

Please bear with my queries. I can easily implement these in C++, but i am new to java. Thanks in advance!

3
  • User input could be String? Then you convert String to Byte array? I didn't understand your question. Commented Jul 10, 2013 at 10:49
  • It's really not much different from C++. There is, eg, System.arraycopy(), which is the equivalent of memcpy. And several different object classes that can be used, similar to the collection classes in C++. Commented Jul 10, 2013 at 10:53
  • user input is a char input Commented Jul 10, 2013 at 10:59

4 Answers 4

1

You need to read more about Reading from input streams. You can start here, but there are other wonderful tutorials out there.

Not exactly answering your question (because it's still ambiguous), but in the following I've read 5 characters from input and output the sorted character array. This will help you get started.

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

        char arr[] = new char[5];
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter input characters and press enter: "); 
        for(int i=0; i<arr.length; i++) {
            arr[i] = (char) br.read();
        }       
        br.close();
        Arrays.sort(arr);
            System.out.println("Sorted Array: "+String.valueOf(arr));   
    }

Console I/O:

Enter input characters and press enter:
zeros
Sorted Array: eorsz

Further as Rohit has already mentioned you should look up the String class and its extensive utilities functions. Also, as you also mention that you don't probably know the number of elements in your array, you might want to look at it as a List, in which case you should use List<Character>.

Sign up to request clarification or add additional context in comments.

Comments

1

Arrays are initialized to 0 in Java.

Arrays are also fixed size, so if you want to add more than 20 characters, you'll have to either create a new, bigger array and copy the old values there, or use a more suitable class like StringBuilder.

5 Comments

Ok suppose, i have 'a' and 'e' inside my array. How do i add a letter 'i' inside the array? Any direct method?
@ArjunBala Do you intend to sort the array?
@ArjunBala Assuming you've declared it as a char[20] you can just say array[2]='i'
correct! but suppose i dont know how many locations are filled? How will i know where to fill the next character? Will this work?: int len=0; int i=0; while(array[i] != '\u0000') { len++; i++; } array[len] = input;
You'll need to keep track of the inserted characters somewhere. I wouldn't recommend your solution though, since you could just as easily keep a single variable that you increment when you add a character.
0

When you create an array, it's indices are filled with the default value for the type you use for the array.

So, for an int[], value 0 will be filled, and for char[] value \u0000 - null character will be filled. See JLS - Initial Value of Variables, for default value for other types.

How do i append characters into this array ?

For this, you would have to maintain the current index which is empty starting with 0, and every time you insert an element at that index, increase it by one.

This process is often not feasible enough, if you have variably increasing array. You should rather consider using a List<Character> instead.


Just for a side note, if you are reading a string from the user, and trying to add each character in that string to a character array, then you don't need to do it manually. String class provides a method - String.toCharArray(), which creates a character array out of your String object.

4 Comments

int len=0; int i=0; while(letters_guessed[i] != '\u0000') { len++; i++; } :Is this appropriate for finding the location at which to insert?
@ArjunBala. You don't need to find empty location everytime you want to insert something. Just start by inserting at i = 0, then when you insert a value at guessed[i], increment i by 1 - i++, then the next time you know that you will be inserting at guessed[1], when you do guessed[i]. You just need to check for range. i should not move further than the maximum allowed index.
Suppose i need to find out the empty location, is my code right?
@ArjunBala. Yeah. Seems to be alright. Just you don't need that len variable to be 0 initially. Make it equal to length of array, and check for i < len also in the while loop.
0

You can append all char in array as follows

    char[ ] array = new char[20];
    StringBuilder t=new StringBuilder();
    for (char i:array){
        t.append(i);
    }

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.