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