Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get name of 2D matrix as user input?

 
shank ram
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We declare two dimensional matrices this way:
int[][] nameOfMatrix = new int[5][5];

How do we get the nameOfMatrix from command-line?

We first get a String (which can then be parsed to an integer) and then how do we assign this name to the matrix?
 
shank ram
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make my question clearer:-


 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming that your input line consists of 25 integers you can use a StringTokenizer to separate the numbers, and use Integer.parseInt() to turn the Strings into integers.
 
shank ram
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am afraid I havent made myself clear yet:

Assuming the user enters the name - mat1
The objective is to create a 2d integer array called "mat1".

that is we can hard code it as:

int[][] mat1 = new int[5][5];

but how do we get the value "mat1" from the user and then declare and initialize the 2d array?

Hope, I have made myself clear !?
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no way to accept a user-inputted string to be used as a variable or reference name in your code. Variable and references names must be determined at compile time.
 
shank ram
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look into maps, though. See if you could put a matrix into a map using a string name as key, then pull it out again later.
 
shank ram
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Stan,
Thanks for the idea of using Map, but I didnt quite get the funda..

To recapitulate my objective (which as of now is not allowed,directly that is ):
1. I want to get the user input of a string, say 'mat1'
2. Then declare a 2D integer array called mat1
(i.e.) int[][] mat1 = new int [5][5];
3. Then populate mat1 with integers (possibly again with user input.)

From, your thought provoking idea - all I could infer was to use the 'put' and 'get' methods of the Map interface so as to associate matrix elements' values and the string. But I couldnt proceed further.. and I dont know even if am on the right track of reasoning .. could you possibly throw some light on this?

Thanks again!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's look first at how you would use dynamically named variables if they existed. Say this was valid syntax:

How would a later line of code ever reference this again? It would have to know the name. Maybe it could do

But I don't see how this is any better than using a real variable name for the array all along.

A map would let you store a bunch of arrays with user entered keys. We could do this in a loop for as many names as the user wants to enter:

Now you can reference these named arrays

If you want to see what names have been defined you can get the keySet() from the map and iterate through them.

Does that help?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12087
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want the end-user to be able to name your variables? the variable names are used by you (and the JVM) to keep track of stuff. the end user doesn't need control over what the JVM uses to refer to stuff...

i'm just asking to try to understand your problem better. it's like saying you want your bank to let you pick your bank account number... you don't really need control of that, it'll mess up the bank because there will be all kinds of different schemas, and what happens when two people enter the same number?

having said all that, Stan's suggestion seems pretty good. you create a Map that uses the user supplied input string as the key to find the array you want. Don't forget you'll still have to handle a user entering the same key-value more than once...
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shank ram:
Hi! Stan,
Thanks for the idea of using Map, but I didnt quite get the funda..
(snip)


Perhaps you should take a look at the Collections trail from Sun's Java Tutorial. This explains how to use classes like ArrayList, HashSet, and HashMap which are all essential to a Java programmer. In this case, HashMap is a class that implements the Map interface, so it will probably be the most helpful to you.

Layne
 
shank ram
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stan! ... for that nifty idea and explaining the actual mechanism to be used.

And Fred, I am not going to make use of it anywhere in actual coding but as you said Stan has explained the method in a lucid manner and it was Stan who tickled my curiosity about this hitherto unkown method.

Layne, I was'nt referring to "funda" of using maps but the "funda" of Stan's idea ... which I have understood now.

Thanks all!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic