Results 1 to 10 of 10
Thread: array of char
- 01-04-2010, 11:08 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 3
- Rep Power
- 0
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
- 01-04-2010, 11:19 PM #2
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
should work.Java Code:main { binaryNumber = addZeroes(binaryNumber); } public static String addZeroes(String a) { while (a.length() != size) a = "0"+a; return a; }
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.
-
Remember that Strings are immutable. So this
creates a String with leading zeros but then discards the String.Java Code:addZeroes(binaryNumber);
You want to get that String returned and use it. i.e.,
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.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
Oh, and welcome to the forum!
- 01-04-2010, 11:32 PM #4
Member
- Join Date
- Jan 2010
- Posts
- 3
- Rep Power
- 0
Fubarable,
thank you!
Just, in last line char[] is not needed (duplicate local variable).
Thank you again!
-
- 01-05-2010, 12:14 AM #6
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
I feel ignored xD
Please mark the thread as SOLVED. :)I die a little on the inside...
Every time I get shot.
- 01-05-2010, 05:43 AM #7
Member
- Join Date
- Jan 2010
- Posts
- 3
- Rep Power
- 0
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.
-
Yep, super was first with the good idea and should get credit. Thank you, sinisab for correcting this.
- 01-05-2010, 09:40 AM #9
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
lol supa...I too feel ignored .:( ...I want a bear hug Fuda.;)
- 01-05-2010, 09:48 AM #10
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
-
Converting a char to an int
By michail in forum New To JavaReplies: 10Last Post: 01-06-2010, 09:09 PM -
fill object of array with char
By begginer12 in forum New To JavaReplies: 1Last Post: 12-05-2009, 02:11 AM -
Convert Char Array to String Array
By Mayur in forum New To JavaReplies: 8Last Post: 10-12-2009, 11:41 AM -
drawing char by char with Graphics
By diggitydoggz in forum New To JavaReplies: 5Last Post: 12-27-2008, 12:49 PM -
Anyone know how to take info from a txt file and convert it to a char array?
By 2potatocakes in forum New To JavaReplies: 9Last Post: 09-11-2008, 02:51 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks