Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Liutauras Vilda
Sheriffs:
• Rob Spoor
• Junilu Lacar
• paul wheaton
Saloon Keepers:
• Stephan van Hulst
• Tim Moores
• Tim Holloway
• Carey Brown
• Scott Selikoff
Bartenders:
• Piet Souris
• Jj Roberts
• fred rosenberger

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

Greenhorn
Posts: 22
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
To make my question clearer:-

Rancher
Posts: 43028
76
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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 !?

High Plains Drifter
Posts: 7289
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
Thanks for the response!

(instanceof Sidekick)
Posts: 8791
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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)
Posts: 8791
• Number of slices to send:
Optional 'thank-you' note:
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?

lowercase baba
Posts: 13053
67
• Number of slices to send:
Optional 'thank-you' note:
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...

Ranch Hand
Posts: 3061
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
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!!

 pie. tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton