• 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

Beginner need help with Array with double and Int

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Odd requirement. Is this homework?
 
Brian-kurt Kibbler
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No it's a test excercise
 
Brian-kurt Kibbler
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okey i have done this so far by searching the webb

these two gets error messages
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79183
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 79183
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okey thanks Tim i will try it out.

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

Is that a vaible map cration ?
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic