aspose file tools*
The moose likes Beginning Java and the fly likes Array initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array initialization" Watch "Array initialization" New topic
Author

Array initialization

Brian Pianczk
Ranch Hand

Joined: Jan 26, 2009
Posts: 45
Hi all, not sure if this is the correct place for this question, but here goes.

I am trying to scan a binary string into an array, and then display that number back, but my problem is if I start with a 0 or multiple 0s it wont display the 0/s.


If i enter 01010101 it wont display that first 0, but if i enter 10101010 it is fine, any suggestions?

Thanks for the help.

[Edited : Added code tag around source]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Brian,

Don't read it as a number using nextInt(); read the whole thing as a String using nextLine() .


[Jess in Action][AskingGoodQuestions]
Brian Pianczk
Ranch Hand

Joined: Jan 26, 2009
Posts: 45
Thanks for the quick response. Is there a reason nextInt(); wont work?

Brian

Edit: Just tried nextLine(); and get an error type missmatch, cannot convert from string to int.

not sure how a nextLine would initialize each index of the array?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40


The problem is... there is no formatting data in an int. It doesn't have any concept of leading zeros, etc. So, when you print out the int, you have to add the format back, which includes adding any leading zeros.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

Just tried nextLine(); and get an error type missmatch, cannot convert from string to int.


I think the suggestion was to use a string instead of an int. This way, the formatting is retained, and you can print out the leading zeros. If you convert the string to an int, then you are back to your original problem, you lost any formatting data.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

And BTW, you do know that in your example, your radix is 10 right? So, when 10101010 is read as an int, it is stored as 10 million, one hundred and one thousand, and ten ? And not stored as 170 (decimal equiv, I think) ?

Henry
Brian Pianczk
Ranch Hand

Joined: Jan 26, 2009
Posts: 45
Yes I know the number as an int is 10million plus. I don't want the number as an int I want each individual 1 or 0 to be an index of the array. I guess maybe my issue is initialing the array with the user input, maybe there is a better way?

I see what you are saying Henry, so when I use my code the first a[i] is = to the number I type in. , and doesnt actually index each individual number as a 0 or 1. now How do I overcome that issue?


That is a method for displaying an or of two binary numbers.(at least i think) I am trying to read in two binary numbers each 1 byte long, and then use that method to output their or.

Brian

edit: OK I got the initialization to work if I prompt the user to add a space, using nextInt(); so 0 1 0 1 1 1 1 1 will display as that when returned. A little bit unorthodox for the user, but I cant come up with a better solution atm.


so much to learn!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Having come late to the discussion, I am a bit behind things, but your last method won't work.
You are creating a new array, then iterating through it, then returning a number, which will be the last number in the array.
A new int[] array is filled with 0s by default, so that method will always return a 0.

Go through the Integer class, and you find methods which will return their binary/octal/hexadecimal representation, but always without leading 0s.
Brian Pianczk
Ranch Hand

Joined: Jan 26, 2009
Posts: 45
Thanks all for the wonderful help. After much thought I figured I wouldnt be able to use an int array to do what i needed to do. At least not with my current knowledge of Java, or any programming language atm.

I have changed to this current code and for the or method it is working.



The converting to and from char to string then back again seems a bit cumbersome, but until I can figure out a better way this works.

I still have one issue, and that is being able to tell if a 1 or 0 is being used exclusively from the user. I am on to that bit now.

Again thanks all

Brian
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Array initialization