Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Filling a 2D array of integers with user input separated by spaces.
Index -> Programming, Java -> Java Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Srlancelot39




PostPosted: Tue Sep 25, 2012 6:31 pm   Post subject: Filling a 2D array of integers with user input separated by spaces.

This was about to be a question...until I ran the code I had and realized it worked!

I'm not very good at tutorials and I don't have much time to go into detail, but for anyone in a hurry for an answer, here is my working code:

code:
Scanner scan = new Scanner(System.in);
                int[][] ints = new int[3][3]; //The 2D array
                System.out.println("Please enter 9 integers separated by spaces:");
                for(int cinput = 0; cinput < 3; cinput++) //column
                {
                        for(int rinput = 0; rinput < 3; rinput++) //row
                        {
                                ints[cinput][rinput] = scan.nextInt(); //This will read the line of integers and fill all 9 positions in the array.
                        }
                }
               
                for(int column = 0; column < 3; column++)
                {
                        for(int row = 0; row < 3; row++)
                        {
                                System.out.print(ints[column][row] + " "); //Outputs the array in a 3x3 grid.
                        }
                        System.out.println();
                }


Sorry that this is hardly a tutorial and practically just an answer to a specific question, but I figured that someone would find this useful. Razz

Post any questions you have (I expect you'll have a few lol)
Sponsor
Sponsor
Sponsor
sponsor
QuantumPhysics




PostPosted: Tue Sep 25, 2012 8:27 pm   Post subject: RE:Filling a 2D array of integers with user input separated by spaces.

Which part would you expect people to have question in?
Aange10




PostPosted: Wed Sep 26, 2012 8:03 am   Post subject: RE:Filling a 2D array of integers with user input separated by spaces.

Java:

ints[cinput][rinput] = scan.nextInt(); //This will read the line of integers and fill all 9 positions in the array.


The comment is a bit misleading. scan.nextInt(); gets the next input and returns it, it doesn't fill the array with inputs. That's the for loop making that happen.
Insectoid




PostPosted: Wed Sep 26, 2012 8:46 am   Post subject: RE:Filling a 2D array of integers with user input separated by spaces.

This is probably better suited in the Submissions section. It's a pretty specific, trivial problem. Posting this here will help exactly nobody. Anyone who has trouble with this exact problem will simply copy your code without understanding it. If they cannot solve this problem, they cannot understand your solution. You explain nothing.
DemonWasp




PostPosted: Wed Sep 26, 2012 9:11 am   Post subject: RE:Filling a 2D array of integers with user input separated by spaces.

Plus, this code confuses column and row: they should be reversed in both cases. You read and print row-by-row, not column-by-column. Admittedly, that choice is arbitrary, but it's the choice followed by the text editors you would use to edit such a file.
Srlancelot39




PostPosted: Wed Sep 26, 2012 10:31 am   Post subject: RE:Filling a 2D array of integers with user input separated by spaces.

First off, thanks for the constructive criticism! Looking back, Submissions would have been a better place to put this considering I wasn't able to write up a proper tutorial. To be honest, I was assuming someone would just copy it without understanding it, but that is not what tutorials are for - so yeah, it shouldn't be in here. Also, I'm not sure why I added the comment stating that the one line of code filled the array...wasn't thinking I guess lol

My logic behind the column/row ordering was different but I'm not going to explain it because your (DemonWasp's) ordering makes much more sense...not sure how I missed that logic originally.

Thanks again for your comments, they were very helpful!

EDIT: (Grammatical fixes)
Aange10




PostPosted: Wed Sep 26, 2012 4:33 pm   Post subject: RE:Filling a 2D array of integers with user input separated by spaces.

On the topic of Rows x Columns, I do remember either Tony or Insectoid (I'm not sure which. Maybe even Dan) stating that they had a very valid argument with a teacher about which spot in the array is a column or row. However I do recognize your argument as true considering you are following the style of the text editor. In any other case, I'd say it's up for debate.
Display posts from previous:   
   Index -> Programming, Java -> Java Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: