Question

I spotted a couple of errors, but I am still not getting this code to work....

I spotted a couple of errors, but I am still not getting this code to work. Can someone help?

public class Bounds1{

int [][] a1;

public Bounds1(){

/*
* Create array Dimension 1
*/
a1 = new int[(int)(Math.random() * 10) + 1][];

for(int i = 0; i < a1.length; ++i){

/*
* Create array Dimensions 2
*/
a1[i] = new int[(int)(Math.random() * 20) + 1];
}
}

public static void main(String[] args){

Bounds1 m = new Bounds1();

for(int i = 0; i < m.a1.length; i++){

for(int j = 0; j < m.a1[i].length; j++){

System.out.print(m.a1[i][j] + " ");
}
System.out.println();
}

try{

/* <= is the error */
for(int i = 0; i <= m.a1.length; i++){
/* <= is the error */
for(int j = 0; j <= m.a1[i].length; j++){

System.out.print(m.a1[i][j] + " ");
}
System.out.println();
}
}
/*
* The best approach
*/
catch(ArrayIndexOutOfBoundsException e){

System.out.println(e);
}
/*
* The following is not the best, but will work
catch(Exception e)
   */
   {

System.out.println(e);
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
// Take "catch(Exception e)" out of the comment
public class Bounds1{
    int [][] a1;
    public Bounds1(){
        /*
         * Create array Dimension 1
         */
        a1 = new int[(int)(Math.random() * 10) + 1][];
        for(int i = 0; i < a1.length; ++i){
            /*
             * Create array Dimensions 2
             */
            a1[i] = new int[(int)(Math.random() * 20) + 1];
        }
    }

    public static void main(String[] args){
        Bounds1 m = new Bounds1();
        for(int i = 0; i < m.a1.length; i++){
            for(int j = 0; j < m.a1[i].length; j++){
                System.out.print(m.a1[i][j] + " ");
            }
            System.out.println();
        }
        try{
            /* <= is the error */
            for(int i = 0; i <= m.a1.length; i++){
                /* <= is the error */
                for(int j = 0; j <= m.a1[i].length; j++){
                    System.out.print(m.a1[i][j] + " ");
                }
                System.out.println();
            }
        }
        /*
         * The best approach
         */
        catch(ArrayIndexOutOfBoundsException e){

            System.out.println(e);
        }
        /*
        * The following is not the best, but will work
        */
        catch(Exception e)
        {

            System.out.println(e);
        }
    }
}

Add a comment
Know the answer?
Add Answer to:
I spotted a couple of errors, but I am still not getting this code to work....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am working on this code and keep getting errors like "ERROR: Syntax error: Encountered "<EOF>"...

    I am working on this code and keep getting errors like "ERROR: Syntax error: Encountered "<EOF>" at line 1, column 169." and "ERROR: Table/View 'STUDENT' does not exist. ERROR: Table/View 'STUDENT' does not exist. ERROR: Table/View 'STUDENT' does not exist." I do not know why this isn't working. Here is my code: DTCCDatabase.java import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; /** * This program creates the CoffeeDB database. */ public class DTCCDatabase {    public static void main(String[] args)...

  • My Question is: I have to modify this program, even a small modification is fine. Can...

    My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...

  • Need Help ASAP!! Below is my code and i am getting error in (public interface stack)...

    Need Help ASAP!! Below is my code and i am getting error in (public interface stack) and in StackImplementation class. Please help me fix it. Please provide a solution so i can fix the error. thank you.... package mazeGame; import java.io.*; import java.util.*; public class mazeGame {    static String[][]maze;    public static void main(String[] args)    {    maze=new String[30][30];    maze=fillArray("mazefile.txt");    }    public static String[][]fillArray(String file)    {    maze = new String[30][30];       try{...

  • draw a flew chart for this code // written by Alfuzan Mohammed package matreix; import java.util.Scanner;...

    draw a flew chart for this code // written by Alfuzan Mohammed package matreix; import java.util.Scanner; public class Matrix {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);    System.out.print("Enter number of rows: first matrix ");    int rows = scanner.nextInt();    System.out.print("Enter number of columns first matrix: ");    int columns = scanner.nextInt();    System.out.print("Enter number of rows: seconed matrix ");    int rowss = scanner.nextInt();    System.out.print("Enter number of columns seconed matrix:...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • I am working on this switch statement code and I am getting errors. Please help. import...

    I am working on this switch statement code and I am getting errors. Please help. import java.util.Scanner; public class Switchstatement { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here } class Convertor { public static void main(String[] args) {    int choice;    Double Yen, UsDollars, Kilometer, Miles, Kilograms, Pounds, Inches; Double Centimeters, result;       Scanner scanner = new Scanner(System.in);    System.out.print("Enter 1\t UsDollars to Yen:...

  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

  • I am almost done with this code, but for some reason I am still getting an...

    I am almost done with this code, but for some reason I am still getting an error message: Some of the instructions were: Using a loop, prompt for a currency to find. Inside the loop that manages the input, call the lookUpCurrency() method can pass the inputted currency code value. The lookUpCurrency() method needs to loop through the list of currencies and compare the passed currency code with the code in the array. If found, return true - otherwise return...

  • I keep getting an Error after I ask the user for test scores. What is missing?...

    I keep getting an Error after I ask the user for test scores. What is missing? package testscores; import java.util.Scanner; public class TestScores { /** * @param args the command line arguments */ private double[] scores;    public TestScores(double[] score) throws IllegalArgumentException { scores = new double[scores.length]; for (int i = 0; i < scores.length; i++) { if (score[i] < 0 || score[i] > 100){ throw new IllegalArgumentException(); } scores[i]=score[i];    }    } public double getAverage() { int sum=0;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT