Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I cannot get the loop to work in the buildDimArray method to store the number combinations "11+11", "11+12", "11+21", "11+22", "12+11", "12+12", "12+21", "12+22", "21+11", "21+12", "21+21", "21+22", "22+11", "22+12", "22+21", and "22+22" into the 2d arraylist with each expression going into one column of the index dimBase-1 row. The loop may work for other people, but for some reason mine isn't functioning correctly. The JVM sees the if dimBase==1 condition, but refuses to check the other conditions. The "WTF" not being printed as a result from the buildDimArray method. If dimBase=1, it prints successfully, but doesn't for the other integers. The dimBase==3 condition needs a loop eventually. The "WTF" is for illustrative purposes. I could get away with a 1d arraylist, but in the future I will likely need the 2d arraylist once the program is completed.


package jordanNumberApp;
import java.util.Scanner;
import java.util.ArrayList;
/*
 * Dev Wills
 * Purpose: This code contains some methods that aren't developed.  This program is supposed to    
 *  store all possible number combinations from numbers 1-dimBase for the math expression
 *  "##+##" into a 2d arraylist at index row dimBase-1 and the columns storing the 
 *  individual combinations.  After storing the values in the arraylist, the print method 
 *  pours the contents in order from the arraylist as string values. 
 */

public class JordanNumberSystem {


    // a-d are digits, assembled as a math expression, stored in outcomeOutput, outcomeAnswer    
    public static int dimBase, outcomeAnswer, a, b, c, d; 
    public static String inputOutcome, outcomeOutput;
    public static final int NUM_OF_DIMENSIONS = 9; //Eventually # combinations go up to 9 
    public static ArrayList<ArrayList<String>> dimBaseArray;
    public static Scanner keyboard;

    /*
     * Constructor for JordanNumber System
     * accepts no parameters
     */
    public JordanNumberSystem() // Defunct constructor
    {


// Declare and Initialize public variables
        this.dimBase = dimBase;
        this.outcomeOutput = outcomeOutput;
        this.outcomeAnswer = outcomeAnswer;


    }

    // Set all values of variable values
    public static void setAllValues()
    {
        // Initialize
        dimBase = 1; 
        outcomeAnswer = 22;  // variables not used for now
        outcomeOutput = "1"; // variables not used for now
        //a = 1; 
        //b = 1;
        //c = 1; 
        //d = 1;
        dimBaseArray = new ArrayList<ArrayList<String>>();
        keyboard = new Scanner(System.in);
    }


    public static void  buildDimArray(int dim)
    {
        dimBase = dim;  

        try 
        {

           //create first row 

           dimBaseArray.add(dimBase-1, new ArrayList<String>());


           if( dimBase == 1)
           {

               a = b = c = d = dimBase ; 
               dimBaseArray.get(0).add(a+""+b+"+"+c+""+d);
               System.out.println("WTF");       // SHOWS
           }


           else if (dimBase == 2)
           {  // dim = 2
               a = b = c = d = 1 ;
               System.out.println("WTF");       // doesn't show
          //     dimBaseArray.get(dimBase-1).add(a+""+b+"+"+c+""+d);

                   for( int i = 1 ; i <= dim ; i++)
                       a=i;

                       for( int j = 1 ; j <= dim ; j++) 
                           b=j;

                           for( int k = 1 ; k <= dim ; k++) 
                               c=k;

                               for( int l = 1 ; l <= dim ; l++) 
                               {
                                   d=l;

                                  dimBaseArray.get(dim-1).add(a+""+b+"+"+c+""+d);

                               }

           }
           else if (dimBase == 3)
           {
               a = b = c = d = dimBase;
               dimBaseArray.get(2).add(a+""+b+"+"+c+""+d);
               System.out.println("WTF");
           }



        }catch (IndexOutOfBoundsException e) 
        {
          System.out.println(e.getMessage());
        } 

    }

    public static void printArray(int num) // Prints the contents of the array
    { // Fixing the printing method
        try 
        {
           int i = num-1; 

           for( String string : dimBaseArray.get(i)) 
           {
              System.out.println(string);
              System.out.println("");
           }
        } catch (IndexOutOfBoundsException e) 
        {
              System.out.println(e.getMessage());
        }                   
    }

    public static void main(String[] args)  throws java.lang.IndexOutOfBoundsException
    {

        setAllValues(); // sets the initial a,b,c,d values and dimBase, initializes 2d arraylist
        // Get the Dimension Base number
        System.out.println("Enter Dimension Base Number.  Input an integer: ");
        int dimBaseInput = keyboard.nextInt();    // Receives integer

        dimBase = dimBaseInput;
                if( dimBase != 1  && dimBase != 2 && dimBase != 3)
                {// Error checking
                    System.out.println("invalid Dimension Base Number should be 1 or 2 ");
                    System.exit(1);
                }
     // Build the arraylist, print, clear, exit
                    buildDimArray(dimBase);
                    printArray(dimBase);
                    dimBaseArray.clear();
                    System.exit(1);


    }

}// End of class


What I have tried:

I have tried debugging the program. This wasn't very helpful. I asked an online tutor for assistance. They took my money, fixed a couple things, but didn't actually solve the problem. I would like to know if this code works for someone else.
Posted
Updated 14-Dec-19 3:23am

Code doesn't "work for you but not me", code either works or it doesn't. Hoping that someone else running it magically makes it work is like hoping that you will win the lottery with last weeks ticket - it's not going to happen.

And saying things like
Quote:
I have tried debugging the program. This wasn't very helpful.
Doesn't mean anything at all - except "I don't know how to debug my code".

If you wrote that code - and I assume you did - then you know what should happen when you run it. So use the debugger to run it line by line checking that exactly what you expected to happen does. Look at the data before each line executes, and work out what exactly should happen before you execute it. Does the data back up what your expected? If it doesn't, then you can look at why not. But just running code under the debugger doesn't highlight the problem automatically because the system doesn't know what you expected the code to do!

So fire up the debugger again, and start looking at what exactly does happen - you may learn a new and very useful skill!
 
Share this answer
 
Comments
Amaggeddon 13-Dec-19 20:09pm    
I understand that a computer only works logically. I am not suggesting that it is deciding not to work. I am trying to say that there are many factors that can affect the performance of code, including its environment. For example, java code used to work in old html some time ago, but in the later html versions, javascript is the only acceptable script. The Java language is almost constantly being updated, sometimes it's hard to keep track. Whether, I am adept at debugging, or a novice, I'm not sure how this helps me. I understand teaching a man to fish, but instead of taking the time to ridicule for not debugging thoroughly, it would have been nice to hear a potential solution or further instruction. However, thank you for trying anyway.
phil.o 14-Dec-19 4:33am    
Do you need links on how to conduct a debug session? Because, indeed, "debugging wasn't very helpful" suggests that you do not really know what debugging is and how to do it. The purpose here is not to make you feel stupid, but rather to bring your attention on the fact that debugging is a mandatory skill to learn if you are serious about being a developper.
OriginalGriff 14-Dec-19 4:39am    
Nicely put! :thumbsup:
If you enter the value 2, then it produces the following exception:
2
Index: 1, Size: 0
Index 1 out of bounds for length 0

So what index is it complaining about? A look at the code shows the following at the beginning of the buildDimArray method:
Java
//create first row ...

dimBaseArray.add(dimBase-1, new ArrayList<String>());

Note here that dimBase is equal to 2, so dimBase-1 equals 1. But dimBaseArray contains no entries yet so it fails, because you cannot add an entry at index 1, until you have added an entry at index 0.

Incidentally, if you had provided the output details in your original question it may have led us to the answer sooner.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900