programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Beginner need help with Array with double and Int

Brian-kurt Kibbler
Greenhorn
Posts: 10
Okey so i want to make an array like this

km = miles
10 6.214
15 9.321
20 12.428

and so on its a converter between km and miles
and i have no clue how to do this i have been searching the webb for about 2 hours or so.

Any help is appreciated, and if you want to send me the whole code it would be to help aswell as i would gain understanding how how its build by mixing
around with it.

Tim Cooke
Marshal
Posts: 4041
239
Hi, and welcome to the Ranch!

The conversion between kilometres and miles is a well defined formula so I wouldn't be bothered with using an Array. Why not just create a method that takes miles and returns kilometres, and another method that takes kilometres and returns miles. Your method signatures might look like this:

As for sending you the whole code. We will help you as much as we can to arrive at the solution yourself, but we are NotACodeMill so nobody is going to do the work for you.

Brian-kurt Kibbler
Greenhorn
Posts: 10
Hi Tim !

Okey , well why i need to make an array is beacuse it needs to be listed like
km = Miles
10 6.214
15 9.321

like this km increasing by 5 up until 100.
And the calculation should be like miles = km * 0.6214;
I've been stuck on this for several hours now.
I have no clue how to make an array and to loop it like listed above.
Do you think you can explain it more basic for a newbee like me so that i can make this program ?

Tim Cooke
Marshal
Posts: 4041
239
Odd requirement. Is this homework?

Brian-kurt Kibbler
Greenhorn
Posts: 10
No it's a test excercise

Brian-kurt Kibbler
Greenhorn
Posts: 10
Okey i have done this so far by searching the webb

these two gets error messages

Mike. J. Thompson
Bartender
Posts: 689
17
If you're getting error messages then you should print them here so we can show you what it means.

Do note though that your method of printing the arrays won't produce two columns side by side, but rather one above the other.

Campbell Ritchie
Marshal
Posts: 56536
172
Welcome to the Ranch

Beware of parallel arrays like that; they are very error‑prone.
They are also not object‑oriented programming.

Actually, as Tim has hinted, miles↔km is a function, so you don't need to be object‑oriented just at the moment.

Tim Cooke
Marshal
Posts: 4041
239
Trying to manage two independent arrays with miles in one and km's in the other is going to cause you nothing but trouble. It's far too easy to get the indexes messed up. You could use a Map instead to map the mile values against the corresponding km values. For example:
Then load it up with the values you want.

Having just written the program myself, here's the pseudo-code for my program:

I also added code tags to your post (see UseCodeTags), see how much better it looks.

Campbell Ritchie
Marshal
Posts: 56536
172
If you look up Map in the Java® Tutorials, you find that a Map
... models the mathematical function abstraction.
So Tim and I both seem to be thinking on the same lines.

Tim Cooke
Marshal
Posts: 4041
239
You could achieve the same output of course without using a map at all. Try the following pseudo-code for that:

Much simpler.

Brian-kurt Kibbler
Greenhorn
Posts: 10
Okey so you suggest a map instead of a array.
As i'm new in java i can't really convert your pseudocode to pure program language.
Is there any hints i can get how to proceed ?

Brian-kurt Kibbler
Greenhorn
Posts: 10
I changed up abit in my program and tried a new tactic...

The question is if i undo this error. Will this code result in what in the program i mentioned earlier?

Tim Cooke
Marshal
Posts: 4041
239
Again, dealing with two independent arrays will almost certainly end in tears, but not before much frustration.

Here's the Java constructs you'll need to implement my pseudo-code:
- Map (Java Tutorial, which includes how to iterate over a Map)
- for loop (Java Tutorial)

See how you get on.

Brian-kurt Kibbler
Greenhorn
Posts: 10
Okey thanks Tim i will try it out.

Map<Miles, double> m = new HashMap<Km, int>();

Is that a vaible map cration ?

Tim Cooke
Marshal
Posts: 4041
239
Your compiler will answer that question for you.

The Map interface lets you define the Java Type for the key and value. So in your example, you've declared Miles as a type which doesn't exist. If you look back over this thread, you'll see I've already given you the code for creating a Map. For the rest of it, the links I gave will provide the learning topics required.

As a general rule, the compiler is your friend. Don't write more than a few lines without being able to compile your code. Tackle your program in small chunks, and don't move on to the next part until the current part compiles and works as expected.

Have fun!

Tim Cooke
Marshal
Posts: 4041
239
OK, let's start at the beginning and build upwards.

Challenge #1:
Using a for loop, print out the integers 0 to 10.

The output of your program should be:

To get you started, here's a template of a runnable Java program that does nothing.

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?