wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How to store user input(int) into an array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to store user input(int) into an array?" Watch "How to store user input(int) into an array?" New topic
Author

How to store user input(int) into an array?

Tavo Gtz
Greenhorn

Joined: Mar 29, 2013
Posts: 8
Hey guys, need some serious help. Im building a program that can convert a binary number to decimal. My first thought was, that I could create an array containing the values of 1 depending of its position and then have the number typed in a textbox by the user, stored in an array, and after that, multiply both arrays, and finally, adding all the values up, giving me the conversion.
What I need to know is...

How do I store the int value into an array? for example, if the user types 1101, be stored like this:
Array[0]=1
Array[1]=0
Array[2]=1
Array[3]=1

after that, I want to multiply that array with another array already declared as:
Array[0]=1
Array[1]=2
Array[2]=4
Array[3]=8

So I can have the answer of the conversion as = 13

Please help, any example code is highly appreciated, Im new to java.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

That is a real interesting route to go about it. I think you should take a look at the API for the Integer class. At the very least it will show you a Method of converting the user input String or character into a number. But you may find another useful method In there as well.


Steve
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11356
    
  16

One thing all programmers need to learn is to break down problems into their discrete parts. I would never approach a problem like this as one thing to to, but at several. For example:

generating the numbers in the series 1,2,4,8... in a way I can get as few or as many as I need.
break a string down into individual characters
iterate through an array, looking at each element
get the input from a user
print some output.
store a bunch of characters into individual array elements


now...this is not the correct order that these will be used, but these are some of the things I need to do. If I can do each of these things (and notice how each and every one is completely independent of anything else), I can write the entire program you need. So now, I focus on writing a method for each of these. Note that some of these may in themselves need to be broken down into several steps/methods as well. But now I have a much smaller problem to focus on each time, making my job easier.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Tavo Gtz wrote:Hey guys, need some serious help. Im building a program that can convert a binary number to decimal.

And just to add to Fred's good advice, you need to define your requirements precisely.

For example: do you want to convert a binary number, or a binary integer?
If the first, I would presume that someone could enter 11.11, and I would expect the answer to be 3.75.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Tavo Gtz
Greenhorn

Joined: Mar 29, 2013
Posts: 8
mmm, good point. Im trying to convert only integer.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Tavo Gtz wrote:mmm, good point. Im trying to convert only integer.

OK, so now the question is: Are you doing this as a mental exercise, or is this part of some much larger problem?

If the first, then you should follow Fred and Steve's advice; if the latter, have a look at Integer.valueOf(String, radix) (all "integer" classes have something similar).

Winston
Tavo Gtz
Greenhorn

Joined: Mar 29, 2013
Posts: 8
Actually, its part of a much bigger problem. I previously thought of using some type of conversion as the one you mention like : "Integer.toBinaryString(Variable)" but that type of solution is something that I can not use right now. Its part of a school assignment where you have to convert numeric systems, just the way you do it on a piece of paper.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to store user input(int) into an array?