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

    Default Variable name determined by another variable's value

    I'm trying to write a program that creates matrices based on user inputs and then will later perform basic operations on those matrices (Transpose, add, subtract, multiply, etc.). The problem I'm having is I want users to be able to input multiple matrices and each matrix needs a unique name. I'm trying to use a char variable (L) that starts at 'A' and is incremented every time a matrix is created but I need to be able to use the value of L as the name of the new matrix (I.E. Matrix A, Matrix B, etc.). Here's the code I have so far, if anyone could help I would greatly appreciate it!

    Java Code:
    import java.util.*;
    
    public class Matrices
    {
    
      public static void main (String[] args)
      {
        double[][] val;
        int m, n, u1, u2;
        char l = 'A';
        Scanner scan = new Scanner(System.in);
        // M & N are used for height and width of the 2D array (val), 
        // u1 and u2 store user inputs for the menus
    
        System.out.println("********************************************");
        System.out.println(" Please note: You may store up to 6 matrices ");
        System.out.println(" to be used in calculations later.");
        System.out.println("********************************************");
        System.out.println();
        //Prints out a welcome screen
    
        while (u1 != 4)
        {
          System.out.println("Please select one of the following options: ");
          System.out.println("1. Create a matrix and enter your own values");
          System.out.println("2. Create an identity matrix of size M x N");
          System.out.println("3. Print a listing of matrices created so far");
          System.out.println("4. Begin performing operations on the current"
                                 + " matrices");
          u1 = scan.nextInt(); 
          switch (u1)
          {
            case 1: {System.out.println("Please enter the height of this Matrix: ");
                     m = scan.nextInt();
                     System.out.println("Please enter the width of this Matrix: ");
                     n = scan.nextInt();
                     val = new double[m][n];
                     for (int i = 0; i<m; i++)
                     {
                      System.out.println("Please enter the values of row " + (i+1) +
                                         " for this matrix one entry at a time:");
                      for (int j = 0; j<n; j++)
                       val[i][j] = scan.nextDouble();
                     }                 
                     Matrix (This name should be the value of l) = new Matrix(val, m, n);
                     l++;
                     break;
                    }
            case 2: {System.out.println("Please enter the height of this Matrix: ");
                     m = scan.nextInt();
                     System.out.println("Please enter the width of this Matrix: ");
                     n = scan.nextInt();
                     Matrix (This name should be the value of l) = new Matrix(m, n);
                     l++;
                     break;
                    }
            case 3: {
                     l = getMatrices();
                     for (char letter = 'A'; letter < l; letter++)
                     {
                      System.out.println("Matrix " + letter);
                      (this should be the value of letter).matrixPrint();
                     }
                    break;
                    }
         }            
        
        } 
        //Loops menu to allow the user to enter multiple matrices and view the results
    
    
    
    
        
        System.out.println("Matrix A: ");
        A.matrixPrint();
        //Used to test that method does indeed work
        
                              
        }
    }

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

    Default

    The variable names are almost meaningless once the code is compiled, so I'd give up on this idea. Instead why not create a HashMap to hold matrices and that uses a String as the key and the Matrix as the value?

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    what is A.matrixPrint()? neither A nor matrixPrint are defined. did you write all this code yourself? how experienced are you with java and classes?

  4. #4
    Lumpkabob is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by emceenugget View Post
    what is A.matrixPrint()? neither A nor matrixPrint are defined. did you write all this code yourself? how experienced are you with java and classes?
    sorry matrixPrint() is a method I wrote in a separate class, I had declared a Matrix A and used it to test the method, I removed the declaration after I had tested it. I am new to java, I'm in an introductory java programming class and we just started learning about classes. I did write all the code though.

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

    Default

    By the way, you're not coming to Java from PHP, are you?

  6. #6
    Lumpkabob is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    Thanks for the help, I checked into the hashmap and it seemed like it should work but it was a little over my head. I ended up just using an array of matrices, not sure why I didn't think of it before. If anyone's interested here's how that part looks now:

    Java Code:
    import java.util.*;
    
    public class Matrices
    {
    
      public static void main (String[] args)
      {
        int[][] val;
        int m, n, u1, u2;
        int l = 0;
        Matrix[] M = new Matrix[20];
        
        
        Scanner scan = new Scanner(System.in);
        // M & N are used for height and width of the 2D array (val), 
        // u1 and u2 store user inputs for the menus, l keeps track of
        // the Matrix array index value
    
        System.out.println("********************************************");
        System.out.println(" Please note: You may store multiple matrices ");
        System.out.println(" to be used in calculations later.");
        System.out.println("********************************************");
        System.out.println();
        //Prints out a welcome screen
        u1 = 3;
        while (u1 != 4)
        {
          System.out.println("Please select one of the following options: ");
          System.out.println("1. Create a matrix and enter your own values");
          System.out.println("2. Create an identity matrix of size M x N");
          System.out.println("3. Print a listing of matrices created so far");
          System.out.println("4. Begin performing operations on the current"
                                 + " matrices");
          u1 = scan.nextInt(); 
          switch (u1)
          {
            case 1: {
                      System.out.println("Please enter the height of this Matrix: ");
                      m = scan.nextInt();
                      System.out.println("Please enter the width of this Matrix: ");
                      n = scan.nextInt();
                      val = new int[m][n];
                      for (int i = 0; i<m; i++)
                      {
                       System.out.println("Please enter the values of row " + (i+1) +
                                          " for this matrix one entry at a time:");
                       for (int j = 0; j<n; j++)
                        val[i][j] = scan.nextInt();
                      }                 
                      M[l] = new Matrix(val, m, n);
                      l++;
                      System.out.println();
                      break;
                    }
            case 2: {
                      System.out.println("Please enter the height of this Matrix: ");
                      m = scan.nextInt();
                      System.out.println("Please enter the width of this Matrix: ");
                      n = scan.nextInt();
                      if (m != n)
                      {
                        System.out.println("For identity matrices M must equal N, " +
                                           "a zero matrix has been created instead.");
                        System.out.println();
                      }
                      M[l] = new Matrix(m, n);
                      l++;
                      System.out.println();
                      break;
                    }
            case 3: {
                      for (int p = 0; p < (Matrix.getLast()); p++)
                      {
                        Matrix A = M[p];
                        System.out.println("Matrix " + (p+1) + ":");
                        A.matrixPrint();
                      }
                    System.out.println();                                 
                    break;
                    }
            case 4: {
                      System.out.println("Continuing on to operations...");
                      System.out.println();
                      break;
                    }
            default: {
                       System.out.println("Invalid selection, please try again");
                       System.out.println();
                       break;
                     }
                     
         }            
        
        } 
        //Loops menu 1 to allow the user to enter multiple matrices and view the results

Similar Threads

  1. Java path variable's
    By Jordsk in forum New To Java
    Replies: 2
    Last Post: 04-09-2009, 02:20 AM
  2. getting the value of variable
    By Lehane_9 in forum New To Java
    Replies: 2
    Last Post: 03-05-2008, 01:42 AM
  3. Getting variable value from a variable name
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-16-2008, 09:26 PM
  4. Variable No. of Arguments
    By Gajesh Tripathi in forum New To Java
    Replies: 2
    Last Post: 10-31-2007, 02:50 PM
  5. Tomcat variable
    By baltimore in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 08-07-2007, 06:40 AM

Posting Permissions

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