Results 1 to 6 of 6
 04102009, 08:39 PM #1Member
 Join Date
 Apr 2009
 Posts
 3
 Rep Power
 0
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 } }

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?
 04102009, 09:04 PM #3Senior Member
 Join Date
 Sep 2008
 Posts
 564
 Rep Power
 6
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?
 04102009, 09:08 PM #4Member
 Join Date
 Apr 2009
 Posts
 3
 Rep Power
 0
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.

By the way, you're not coming to Java from PHP, are you?
 04142009, 08:00 AM #6Member
 Join Date
 Apr 2009
 Posts
 3
 Rep Power
 0
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

Java path variable's
By Jordsk in forum New To JavaReplies: 2Last Post: 04092009, 02:20 AM 
getting the value of variable
By Lehane_9 in forum New To JavaReplies: 2Last Post: 03052008, 01:42 AM 
Getting variable value from a variable name
By Java Tip in forum Java TipReplies: 0Last Post: 02162008, 09:26 PM 
Variable No. of Arguments
By Gajesh Tripathi in forum New To JavaReplies: 2Last Post: 10312007, 02:50 PM 
Tomcat variable
By baltimore in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 08072007, 06:40 AM
Bookmarks