Results 1 to 10 of 10

Thread: array of char

  1. #1
    sinisab is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default array of char

    Hi,
    I have a problem with array. Inputs are size of binary number and integer. I need to create char array from that integer with leading zeroes (I need those 0`s later for a logical operations). Problem is that I can not get those 0`s as a result. Can anyone help me, please?

    Java Code:
    public class adder {
    	
    	static int size;
    	
    	public static void main (String[]args){
    	
    	Scanner input = new Scanner (System.in);
    	System.out.println("size of binary number");
    	size = input.nextInt();
    	System.out.println("enter number?");
    	int firstNumber = input.nextInt();
    	String binaryNumber = Integer.toBinaryString(firstNumer);
    	addZeroes (binaryNumber);
    	char[] firstList = new char [size];
    	firstList = binaryNumber.toCharArray();
    	System.out.println(firstList);
    	}
    	
    	public static String addZeroes(String a) {
    		int i;
    		i = a.length();
    		if (i == size)
    			return a;
    		else
    		{
    			int j = size - i;
    			for (int k=0; k<j; k++)
    			{
    				a = "0" + a;				
    			}
    			return a;		
    		}
    	}
    
    }
    Last edited by Fubarable; 01-04-2010 at 11:15 PM. Reason: code tags added

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Java Code:
    main {
    binaryNumber = addZeroes(binaryNumber);
    }
    
    
    public static String addZeroes(String a) {
    		while (a.length() != size)
                        a = "0"+a;
                    return a;		
    	}
    should work.

    EDIT: Btw, printing an array will just show you the name of the object. You should loop through the array and print each element to print the contents.
    Last edited by Supamagier; 01-04-2010 at 11:22 PM.
    I die a little on the inside...
    Every time I get shot.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Remember that Strings are immutable. So this
    Java Code:
    addZeroes(binaryNumber);
    creates a String with leading zeros but then discards the String.

    You want to get that String returned and use it. i.e.,
    Java Code:
          //!! addZeroes(binaryNumber);
          //!! char[] firstList = new char[size];
          //!! firstList = binaryNumber.toCharArray();
          String addedZeros = addZeroes(binaryNumber);  // get the result of the method
          char[] firstList = addedZeros.toCharArray();   // and use it here
    Also, note that I added code tags to your original post to help make it readable. Please see my signature to learn how to do this yourself.

    Oh, and welcome to the forum!

  4. #4
    sinisab is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default

    Fubarable,
    thank you!
    Just, in last line char[] is not needed (duplicate local variable).
    Thank you again!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sinisab View Post
    Fubarable,
    thank you!
    You're quite welcome.

    Just, in last line char[] is not needed (duplicate local variable).
    That's because in my code I declared the char array variable on the same line that I created it (via String#toCharArray() method).

  6. #6
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    I feel ignored xD

    Please mark the thread as SOLVED. :)
    I die a little on the inside...
    Every time I get shot.

  7. #7
    sinisab is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default

    Supamagier,
    Sorry! Thank you, also. Your method AddZeros is efficient than mine, so I will use that.
    I will mark this as solved, just to see how to do that. I`m new in forum and Java also. Forgive me.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  9. #9
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    lol supa...I too feel ignored .:( ...I want a bear hug Fuda.;)

  10. #10
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    if you only need a string representation then this is your friend

    Java Code:
    		NumberFormat nf = NumberFormat.getInstance();
    		nf.setMinimumIntegerDigits(11);
    		nf.setGroupingUsed(false);
    		System.out.println(nf.format(1234));
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

Similar Threads

  1. Converting a char to an int
    By michail in forum New To Java
    Replies: 10
    Last Post: 01-06-2010, 09:09 PM
  2. fill object of array with char
    By begginer12 in forum New To Java
    Replies: 1
    Last Post: 12-05-2009, 02:11 AM
  3. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 11:41 AM
  4. drawing char by char with Graphics
    By diggitydoggz in forum New To Java
    Replies: 5
    Last Post: 12-27-2008, 12:49 PM
  5. Replies: 9
    Last Post: 09-11-2008, 02:51 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
  •