| |||||||||||
Inputting two dimensional array from command promp Posted by anonymous (anonymous), 24 November 2005 Hello,I`m a java newbie.... I`m writing a program, where in `m suppossed to ask the user to enter the values...and store them in arr[0][0], arr[0][1] , arr[1][0], arr[1][1]... I`m not sure how to do it....... Any help would be appreciated. Posted by admin (Graham Ellis), 24 November 2005 Are you asking about how to set up the 2D array, how to write loops to go though each of the elements, or how to read from the user? If the latter, are you using a stand alone application of an applet, or are you writing a server application based on Servlets of JSPs?Sorry about all the questions - but there's a lot of possible answers. Our Java Training Notes site may provide you with some background too - we've published our complete 5 day Java course there in .pdf files. Posted by admin (Graham Ellis), 24 November 2005 Ah - just noticed that you title said "from command prompt". OK - have a look at the wellreader class in the notes that I've given you a link to. And the source code is in our resource centre if you want to see it and grab a copy;.Posted by anonymous (anonymous), 24 November 2005 I`m asking the user to enter the 4 co-ordinates of rectangle for an array..............Also how do i get the float values from user.....Here is the code............ BufferedReader standard = new BufferedReader( new InputStreamReader(System.in)); try { System.out.println("Enter the co-ordinates for people"); String inline = standard.readLine(); while((standard.readLine()) != null) { int var = Integer.parseInt(inline); StringTokenizer strtok = new StringTokenizer (inline," "); while (inline.hasMoreTokens()) { arr[0][0]=var; arr[0][1]=var; arr[1][0]=var; arr[1][1]=var; } b = Integer.parseInt(inline); c = Integer.parseInt(inline); d = Integer.parseInt(inline); } } catch (Exception e) { ; } Posted by admin (Graham Ellis), 25 November 2005 I'll cut in some comments ...BufferedReader standard = new BufferedReader( new InputStreamReader(System.in)); try { // The prompt isn't very explanatory and your users may not // realise they have to enter four numbers, space separated. System.out.println("Enter the co-ordinates for people"); String inline = standard.readLine(); while((standard.readLine()) != null) { // Next line needs to be inside the loops to get tokens int var = Integer.parseInt(inline); StringTokenizer strtok = new StringTokenizer (inline," "); // hasMoreTokens runs on the tokenizer object not the string // If you always have four tokens, 4 assignments may do better // If it might expand later, use two nested loops. // See String Tokenizer example while (inline.hasMoreTokens()) { arr[0][0]=var; arr[0][1]=var; arr[1][0]=var; arr[1][1]=var; } // Float.parseFloat for a floating point number. We've used parseFloat in an example elsewhere. b = Integer.parseInt(inline); c = Integer.parseInt(inline); d = Integer.parseInt(inline); } } // You may do better to try and catch in smaller blocks - // that would make your error recovery much easier. // This advise is more completely given in our exception training notes catch (Exception e) { ; } I'm deleting your duplicate posting of this question - having two threads running doesn't get any extra positive attention and it tends to dilute responses This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |