Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Filling a 2D array of integers with user input separated by spaces.
Author Message
Srlancelot39

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

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

QuantumPhysics

Posted: 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

Posted: 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

Posted: 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

Posted: 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

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

EDIT: (Grammatical fixes)
Aange10

Posted: 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: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 7 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: