Results 1 to 6 of 6
  1. #1
    hafizul is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Post 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;
    }


    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  3. #3
    hafizul is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    Yes, t.q for remind me the curly brace.. miss looking.

  4. #4
    hafizul is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    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;
    }
    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    i had check and compile my program, but still error with IllegalArgumentException..
    This doesn't tell us much. What's working correctly? How is it not working correctly? In other words, exactly what error is happening?

    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]

  6. #6
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default 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

  1. i need an example of JSR179 ((Location based Ser)implementation for CDC based device
    By talk_to_vivekmishra in forum CDC and Personal Profile
    Replies: 3
    Last Post: 12-30-2010, 10:07 AM
  2. Stack layout
    By blue404 in forum SWT / JFace
    Replies: 0
    Last Post: 03-22-2009, 01:15 PM
  3. Stack problem..pls help
    By Mika in forum New To Java
    Replies: 1
    Last Post: 02-16-2009, 08:10 AM
  4. Stack not popping
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 04:59 PM
  5. Replies: 1
    Last Post: 07-23-2007, 11:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •