• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array initialization

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Brian,

Don't read it as a number using nextInt(); read the whole thing as a String using nextLine() .
 
Brian Pianczk
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic