aspose file tools*
The moose likes Java in General and the fly likes How to get name of 2D matrix as user input? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to get name of 2D matrix as user input?" Watch "How to get name of 2D matrix as user input?" New topic
Author

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

shank ram
Greenhorn

Joined: Sep 05, 2005
Posts: 22
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

Joined: Sep 05, 2005
Posts: 22
To make my question clearer:-


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41861
    
  63
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.


Ping & DNS - my free Android networking tools app
shank ram
Greenhorn

Joined: Sep 05, 2005
Posts: 22
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

Joined: Oct 25, 2000
Posts: 7292

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.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
shank ram
Greenhorn

Joined: Sep 05, 2005
Posts: 22
Thanks for the response!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
shank ram
Greenhorn

Joined: Sep 05, 2005
Posts: 22
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

Joined: Jan 29, 2003
Posts: 8791
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

Joined: Oct 02, 2003
Posts: 11314
    
  16

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...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
shank ram
Greenhorn

Joined: Sep 05, 2005
Posts: 22
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!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get name of 2D matrix as user input?