Results 1 to 6 of 6
Thread: stack based program
- 03-27-2009, 08:15 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 3
- Rep Power
- 0
stack based program
Sorry for my noob, my IllegalArgumentException cannot run..
Can anybody help?:(
import java.util.*;
public class test2 {
public static void main(String[] args) {
Stack stack = new Stack();
Scanner keyIn = new Scanner (System.in);
int i = 0;
int j = 0;
int temp0;
int temp1;
while (true){
System.out.println("Enter numbers to find GCD of (0 to stop):");
System.out.println("Input i ");
i = keyIn.nextInt();
if (i == 0)
break;
if (i > -1){
stack.push(i);
} else {
throw new IllegalArgumentException("negative number" );
}
System.out.println("Input j ");
j = keyIn.nextInt();
if (i == 0)
break;
if (i > -1){
stack.push(j);
} else {
throw new IllegalArgumentException("negative number" );
}
temp0 = (Integer) stack.pop();
temp1 = (Integer) stack.pop();
//display value gcd, call GCD method, pass value temp0 and temp1
System.out.println("The GCD : " + gcd(temp0, temp1));
}
}
}
private static int gcd(int i, int j){
int r;
r = i % j;
return r;
}
}
-
Does it compile? Have you checked to be sure that all curly braces match correctly?
Also, after the user enters the second int, j, make sure that your testing j's value, and not re-testing i's value.Last edited by Fubarable; 03-27-2009 at 08:43 PM.
- 03-27-2009, 08:56 PM #3
Member
- Join Date
- Mar 2009
- Posts
- 3
- Rep Power
- 0
Yes, t.q for remind me the curly brace.. miss looking.
- 03-27-2009, 09:03 PM #4
Member
- Join Date
- Mar 2009
- Posts
- 3
- Rep Power
- 0
i had check and compile my program, but still error with IllegalArgumentException..
I send back the program..
import java.util.*;
public class test2 {
public static void main(String[] args) {
Stack stack = new Stack();
Scanner keyIn = new Scanner (System.in);
while (true){
int i = 0;
int j = 0;
int temp0;
int temp1;
System.out.println("Enter numbers to find GCD of (0 to stop):");
System.out.println("Input i ");
i = keyIn.nextInt();
if (i == 0)
break;
if (i > -1){
stack.push(i);
} else {
throw new IllegalArgumentException("negative number" );
}
System.out.println("Input j ");
j = keyIn.nextInt();
//detect when user key in 0, break = exit
if (j == 0)
break;
if (j > -1){
stack.push(j);
} else {
throw new IllegalArgumentException("negative number" );
}
temp0 = (Integer) stack.pop();
temp1 = (Integer) stack.pop();
System.out.println("The GCD : " + gcd(temp0, temp1));
}
}
private static int gcd(int i, int j){
int r;
r = i % j;
return r;
}
}
-
This doesn't tell us much. What's working correctly? How is it not working correctly? In other words, exactly what error is happening?i had check and compile my program, but still error with IllegalArgumentException..
Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?
To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
- 03-28-2009, 03:49 PM #6
Member
- Join Date
- Feb 2009
- Posts
- 92
- Rep Power
- 0
works for me
I cut and pasted your code into eclipse. The data entry worked fine.
Your GCD algorithm is incorrect, but you may not have gotten that far to find out.
Hint: GCD is a recursive algorithm that ends when r becomes 0 (GCD has been found and is nontrivial) or 1 (GCD is in fact trivially 1);
Similar Threads
-
i need an example of JSR179 ((Location based Ser)implementation for CDC based device
By talk_to_vivekmishra in forum CDC and Personal ProfileReplies: 3Last Post: 12-30-2010, 10:07 AM -
Stack layout
By blue404 in forum SWT / JFaceReplies: 0Last Post: 03-22-2009, 01:15 PM -
Stack problem..pls help
By Mika in forum New To JavaReplies: 1Last Post: 02-16-2009, 08:10 AM -
Stack not popping
By bugger in forum New To JavaReplies: 2Last Post: 01-28-2008, 04:59 PM -
difference between code based security and role based security
By boy22 in forum New To JavaReplies: 1Last Post: 07-23-2007, 11:59 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks