Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Can anyone tell me what's wrong with my program?

Options
2»

Comments

  • Closed Accounts Posts: 1,959 ✭✭✭Nala


    It means she can't program. Or drive. Or go to a toliet without a support group. Everyone knows that.

    I'm actually the ONLY girl I know who goes to the toilet by herself!


  • Closed Accounts Posts: 1,959 ✭✭✭Nala


    Hobbes wrote:
    There is no point giving someone working code, because it stops them from learning and building a 'cut and paste' mentality to exams.

    If they can explain what the code is doing even if it is wrong is better then just handing over code. That way you can explain what parts are wrong.

    That's a good point but sometimes I need a bit of code, like methods.


  • Closed Accounts Posts: 1,959 ✭✭✭Nala


    OK...

    This is the finished program. Ot will compile but throws an ArrayIndexOutOfBoundsException when I run it. Has anyone any ideas on how to fix this?

    1. //Write a Java program that randomly generates 10 integers
    2. //in the range 1 – 20 and stores them in an array.
    3. //You should then ask the user to enter a number and search
    4. //the array to determine if the number entered is in the array.
    5. //If the number is found in the array display the location
    6. //the number was found at, otherwise display a message
    7. //stating the number entered is not in the array.

    8.
    9. class sec6q1
    10. {
    11.
    12. //Initialise read only variables
    13. final static int SIZE=10;
    14. final static int END=20;
    15.
    16. public static void main(String[]args)
    17. {
    18.
    19. //Initialise variables
    20. boolean valid = false;
    21. boolean found = false;
    22. int number = 0;
    23. int index = 0;
    24. int i = 0;
    25.
    26. //Initialise the array
    27. int numArray[] = new int[SIZE];
    28. for (i=0; i<numArray.length; i++);
    29.
    30. //Randomly generate 10 numbers in the range for the array
    31. for (i = 0; i<numArray.length; i++); {
    32. numArray = (1 + (int) (Math.random() * END) );
    33. }
    34.
    35. //Get user input, ensuring the number is between 1-20,
    36. // i.e. it is valid

    37.
    38. //Use a while-loop to ensure the number is valid.
    39. //If !valid = false, the number is not in the range.

    40.
    41.
    42. while (!valid) {
    43.
    44. System.out.println("Please enter a number between 1-20");
    45. number=SavitchIn.readLineInt();
    46.
    47. //Determine it is in the range
    48. if(number>0 && number<=END)
    49. valid = true;
    50.
    51. }
    52. //ends while
    53.
    54. //Search the array for the user's input while we
    55. //are still within bounds and we have not found
    56. //what we are looking for.

    57. // !found = true because found = false.
    58.
    59. for(i = 0; i < numArray.length && !found; i++) {
    60.
    61. if(numArray == number) {
    62. index = i;
    63. found = true;
    64. }
    65. } //ends for
    66.
    67. //Display the results

    68. if(found)
    69. System.out.println(number + " found at position "+index+ " in the array");
    70. else
    71. System.out.println(number + " not found in the array");
    72. }
    73. }


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    neev wrote:
    OK...

    This is the finished program. Ot will compile but throws an ArrayIndexOutOfBoundsException when I run it. Has anyone any ideas on how to fix this?

    No one can run you code if you put line numbers on it like that. Use the [ C O D E ] tags.

    Your Exception should of told you what line number is causing the problem.


  • Registered Users Posts: 2,426 ✭✭✭ressem


    Line 31, you left a semicolon in the wrong place after cutting and pasting the previous line, so at line 32, i = 10.
    Therefore out of bounds.

    Have you learned about using an IDE, like Eclipse (free download), for your course,
    to let you step through your application line by line, and see, as in this exercise, that line 32 wasn't hit till the loop was finished, and the value of i used when the exception occurred on line 32 will be available in the variables window.

    That was day 1 of the course when I was learning C back in the day and is essential for finding mistakes easily.


  • Advertisement
  • Closed Accounts Posts: 1,959 ✭✭✭Nala


    UPDATE

    Got the results this morning............





    I PASSED!!!!!!!!!

    Can't believe it tbh, exam was tough and I only got 60% of it done.
    Thanks to everybody for helping me out!

    Neev


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Congrats. :) Free positive rep for you (when the board lets me)


Advertisement