*
The moose likes Beginning Java and the fly likes Java Help... so frustrated!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Help... so frustrated!!" Watch "Java Help... so frustrated!!" New topic
Author

Java Help... so frustrated!!

Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
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 ]

Live And Let Learn.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
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

Joined: Mar 26, 2006
Posts: 124
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.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39568
    
  27
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 ]

Ping & DNS - updated with new look and Ping home screen widget
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
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

Joined: Aug 31, 2004
Posts: 11343

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].
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Help... so frustrated!!
 
Similar Threads
Java Help... so frustrated.... again!!
single arrays in java
Kindly help me
Please help!
Reordering Alphabets