• 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

Java Help... so frustrated!!

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My problem is in the code. What its suppose to do is have the user enter 20 numbers and the program will spit out the difference between that number and the last before it. But I can't figure out what "Difference" should equal. Also... I've tried to make the text that spits out after the 20 numbers more appealing but everythings just crammed up... can somebody help me? javascript: x()
banghead





EDIT by mw: Added Code Tags to restore original poster's formatting.

[ March 26, 2006: Message edited by: marc weber ]
(Marilyn broke long line)
[ March 26, 2006: Message edited by: Marilyn de Queiroz ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

I see two issues that are preventing this from compiling. The first is...

array calc = new array();

In Java, the syntax for creating arrays is a little different than for most other objects: You don't call a constructor called "Array." Instead, arrays are created as you already did 4 lines above this...

int[] array = new int[20];

So you've already got your array created, and you can remove the "new array()" line.

The next issue is when you try to call...

calc.display(array);

The method "display" is an instance method in a separate class called Two. So to use this method, you need to create an instance of Two and use that to call the method. For example...

Two myTwo = new Two();
myTwo.display(array);

These things will allow the code to compile. After that, see what the output tells you when you run.

PS: Note that I added Code Tags to your original post. This keeps your code formatting intact so it's easier for others to read. Please use these in future posts.
[ March 26, 2006: Message edited by: marc weber ]
 
Martin vanPutten
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have had the program compile fine... I've just been adding things that may take my run-time error away. My problem is when its running... but thanks for the information on the two mistakes I have done. javascript: x()
Smile
 
Martin vanPutten
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there anybody out there who can take this program and fix the entire thing? I've asked people for this already but nobody has helped me because there suggestions don't work. I've done everything I know... and I've even looked in my java book and I still can't get it. Can somebody please take the time and make it work? All it has to do is spit out the difference of the number and the last.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can somebody please take the time and make it work?



No. That's not what we do here. We're trying to help people learn Java, and you won't learn anything by someone else fixing your code.
Analyse what it does, and which part of what it's doing you don't understand, and we'll try to help you get going again.
[ March 27, 2006: Message edited by: Ulf Dittmer ]
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you're supposed to take 20 numbers at execution? AS in "java DifferenceMaker 1 2 3 4 5 6 ... 20" and spit out the differences between the last number and the one before it (not sure what that means, but I'll give it a shot) as in "output: 1 (20-19), 1 (19-18), 1 ... , 1"?

If that is the problem then here is a pseudo-code solution:
1. Take in the array (in the main method)
2. Print out some initial text like "the output is: "
2. run a backwards for loop over the length of the array (start at length-1, end at 1 (not 0 or you'll get an ArrayOutOfBoundsException), do index-- at each iteration)
3. For each member of the array (also each iteration of the loop) print out a line with something like: "the difference between input number "+index+" and input number "+index-1+" is "+array[index]-array[index-1]

Your output should be 19 lines beginning with: "the output is: " and each line looking like:
"the difference between input number 20 and input number 19 is 1"
"the difference between input number 19 and input number 18 is 1"

....

etc. till the end (of course the 1s will be replaced by the actual differences).

Hope it helps
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main runtime problem is an ArrayOutOfBoundsException when you try to access array[20] in the do-while loop.

Hint: Note that a do-while loop executes the "do" block first, and then checks the condition (Count < 20) before beginning another iteration. So by the time it recognizes that (Count < 20) is false, it's already tried to access array[20].
 
No matter how many women are assigned to the project, a pregnancy takes nine months. Much longer than this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic