Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: need your help

  1. #1
    richard's Avatar
    richard is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default need your help

    this is the problem...


    Write a java application named GoTooFar that declares an array of five integers and store five integers values in the array. Write a try block in which you loop to display each successive element of the array, increasing a subscript by one on each pass through the loop. Create a catch block that catches the eventual ArrayIndexOutofBoundsException and display the message "Now you have gone too far!".Name the program as GotooFar.java.


    Kindly post your program here....


    thanks.
    Last edited by richard; 01-06-2011 at 05:53 AM.

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Talking

    Would you tell us what have to tried so far to achieve this?

    We cant solve the assignments for you, but we can try to help you if you are stuck up somewhere in your code.

    Hope thats clear,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    jmb272 is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    I'm not great with throwing exceptions just yet, I had a go anyway

    Java Code:
    public class GoTooFar
    {
    	public static void main(String[] args)
    	{
    		int a = 0;
    		int[] n = {1,2,3,4,5};
    		boolean done = false;
    		
    		while (done == false)
    		{
    			try {
    				System.out.println("(" + a + ") " + n[a]);
    			}
    			catch (Exception e)
    			{
    				done = true;
    				System.out.println("Now you have gone too far!");
    			}
    			
    			a++;
    		}
    	}
    }

  4. #4
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    why keep throwing the exception inside the loop?

  5. #5
    jmb272 is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by JavaHater View Post
    why keep throwing the exception inside the loop?
    Where would you put it?

  6. #6
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    bring the loop inside the try block. and why put a condition with the while loop? The purpose is to let the try catch the error, so just increment the count will do.

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by JavaHater View Post
    bring the loop inside the try block. and why put a condition with the while loop? The purpose is to let the try catch the error, so just increment the count will do.
    You can go this way,

    Java Code:
    try {
    while (true) {
    System.out.println("(" + a + ") " + n[a]);
    a++;
    }
    }
    And then let the catch display the message.

    Goldest
    Last edited by goldest; 01-06-2011 at 07:01 AM.
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  8. #8
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Java Code:
                    try {
                            for(;;count++){System.out.println( array[count] ); }
                    }catch ( ArrayIndexOutOfBoundsException ex ){
                            System.err.println("Now you have gone too far!");
                    }

  9. #9
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Anything can be done!

    My signature really makes some sense. :D

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  10. #10
    richard's Avatar
    richard is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    Just put the looping in the try block.....bro just re-write ur program so that i can try it to my program...

    thanks.

  11. #11
    richard's Avatar
    richard is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    but bro.....i have my own program and I'm not sure if this program is correct..

  12. #12
    richard's Avatar
    richard is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    This is my program and I'm using the for loop structure...


    import javax.swing.*;

    public class GotooFar
    {
    public static void main (String []args){
    int [] arr = new int [5];

    try{
    int x = 0;
    for(x=0;arr[x]<6;x++){
    String a = JOptionPane.showInputDialog("Input a Number");
    int b = Integer.parseInt(a);
    System.out.println(x);}
    }
    catch(ArrayIndexOutofBoundsException ae){
    System.out.println();
    System.out.println("Now you have gone too Far!"
    }
    }
    }
    Last edited by richard; 01-06-2011 at 07:49 AM.

  13. #13
    richard's Avatar
    richard is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    that is my program...


    now tell me if my program is correct...

    and compare to your program...

    thanks.

  14. #14
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by richard View Post
    but bro.....i have my own program and I'm not sure if this program is correct..
    Wow!

    Would you be kind enough to post your program?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  15. #15
    richard's Avatar
    richard is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    No im just posting my program bro...

    so kindly post your program...


    i need help you know...

  16. #16
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Unhappy

    Why are you displaying the loop integer x in above code?

    And where are you doing following part of your assignment?

    Quote Originally Posted by richard View Post
    store five integers values in the array.

    You never told us that you wanted Swings?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  17. #17
    richard's Avatar
    richard is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    x is just a variable bro...


    So, i need your help xD..


    please post your program i will it as my reference program..

  18. #18
    richard's Avatar
    richard is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    Bro

    Look at my program above.

    Thanks.
    Java Programming is Challenging:)

  19. #19
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by richard View Post
    x is just a variable bro...
    I know that. But there is no point in printing it, as its simply displaying the loop count from 0 to 4.

    And what about the integer value you are geting from the dialouge box, you are not even using it anywhere.


    Quote Originally Posted by richard View Post
    please post your program i will it as my reference program..
    Instead of asking me to post my code, why don't you try to feed the array? And then display the array elements as per the index entered in the input? But that would be prone to errors as the inputs needs to be in sequence from 0 to 5 where at 5 you would get the exception. But thats totally manual execution.

    Or you can use the input values to get stored in the array. And when you try to feed the 6th element you will get exception.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  20. #20
    richard's Avatar
    richard is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    Please bro post your code here...

    please i need to pass this tommorow..

    thanks.
    Java Programming is Challenging:)

Page 1 of 2 12 LastLast

Posting Permissions

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