Results 1 to 16 of 16
Thread: need help with 2D array
- 01-19-2011, 03:07 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 30
- Rep Power
- 0
need help with 2D array
program for asking the user to enter the size of the array(row).The size of the column is fixed (it should be 3).
Next the user has to enter the roll number which starts with IAD
example IAD123
The roll number 123 should be stored
and then ask user to enter the marks and also the name of the person and store them and then ask user to enter the rollnumber/person name to get the marks of that specified roll number/person.
Any suggestions on how to program .
- 01-19-2011, 03:12 PM #2
- 01-19-2011, 03:12 PM #3
- 01-19-2011, 03:19 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 30
- Rep Power
- 0
i am trying to do it.This is my first program with 2d arrays.I am not able to take input from the user for 2d array.
- 01-19-2011, 03:33 PM #5
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
If you are familiar with Scanner objects, you can use the next() method to take input from the user. As for the 2D array part, you need a way to traverse the array (i.e. access each element). For single array that is not 2D how would you do it? I would do it using a for loop. Likewise, for a 2D array how do you think it should happen? You can access each row-column combination using 2 for loops that are nested. Here is an outline I would use, you can improvise to suit your needs:
Hope this helps.Java Code:int[][] intArray = new int[10][10]; for(int i = 0; i < intArray.length; i++) { // this is used to access each row for(int j = 0; j < intArray[i].length; j++) { // used to access each column int[i][j] = assign something or get input from scanner; } }
Best,--user0--
- 01-19-2011, 03:44 PM #6
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
Also - here is how I learned to traverse the 2D array, it is a Java tutorial, for your reference:
Chapter 5 Continued: Strings and Arrays
Best,--user0--
- 01-19-2011, 04:29 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 30
- Rep Power
- 0
User has to give the input for the row size and then i have to use that row size.
This is the code which i have written so far
Java Code:int row, column,k=0; String rollNumber; int marks,s=0; String name; Scanner scan = new Scanner(System.in); System.out.print("Enter the size of the array s: "); int[][] arr = new int[s][3]; arr[row][column] = scan.nextInt(); for (row = 0; row < arr.length; row++) { for (column = 0; column <= 3; column++) { } }
Can any one help me please.
- 01-19-2011, 04:56 PM #8
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
- 01-19-2011, 05:05 PM #9
Member
- Join Date
- Jan 2011
- Posts
- 30
- Rep Power
- 0
I am fully confused with 2d arrays.I wish some one explains me how to do the program.
Please let me know .can any one atleast tell me where to t read from and understand what i have do .
- 01-19-2011, 05:08 PM #10
Member
- Join Date
- Jan 2011
- Posts
- 30
- Rep Power
- 0
here is the code written so far
Java Code:int row, column; int size = 0; String s=""; boolean found = false; String line =null; Scanner scan = new Scanner(System.in); System.out.print("Enter the size of the array s: "); System.out.println(); for (int i = 0; i < s.length(); i++) { try{ int k = Integer.parseInt(scan.nextLine()); } catch(NumberFormatException nfe){ System.out.print("Please enter the number again in correct format:"); System.out.println(); line = "nnf"; found = true; } if (scan.hasNextInt()) { size = scan.nextInt(); } } }
- 01-19-2011, 05:16 PM #11
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
OKyou are fully confused with 2D array...understood. But they are nothing to be confused about. You are familiar with arrays? An array of length N lets you store N elements of the same type in it.
A 2D array is simply an Array of Arrays. Say you were creating a 2D array of int. This is simply an array of int, where each element is another array of int. Make sense? So for your example, you said there will be 3 columns, and a number of rows given by the user. So if the user gives you 3 rows, for example, that simply means you are creating an array of length 3 (rows), where each element in turn is an array of int of length 3 (columns). Try to visualize it like a table.
here is a link I found that explains them in detail.
Java: Arrays -- 2-dimensional--user0--
- 01-19-2011, 05:19 PM #12
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
- 01-20-2011, 06:49 PM #13
Member
- Join Date
- Jan 2011
- Posts
- 30
- Rep Power
- 0
Here is the code which i have written so far
Can any one help me in writing the code forJava Code:int rowsize = 0; int columnindex = 3; String line = null; int i = 0; boolean nf = true; int rowindex = 0; String input = "IAD"; Scanner scan = new Scanner(System.in); System.out.print("Enter the row size of the array s: "); rowsize = scan.nextInt(); System.out.println(" row size is :" + rowsize); String[][] arr = new String[rowsize][3]; for (rowindex = 0, columnindex = 0; rowindex <= rowsize; rowindex++, columnindex++) { System.out.print("Enter the Roll Number:"); String rollnumber = scan.next(); System.out.println(rollnumber.split(input)[1]); System.out.print("Enter name for the student:"); String studentname = scan.next(); boolean letterFound = false; for (i = 0; i < studentname.length(); i++) { if (Character.isLetter(studentname.charAt(i))) { // System.out.println("Given input is in correct format"); } else { letterFound = true; } } System.out.println("" + studentname); System.out.print("Enter marks for the student:"); String studentmarks = scan.next(); System.out.println(" studentmarks is :" + studentmarks); } if (rowindex <= rowsize) { } else { System.out.print("Enter the rollnumber or name of the student:"); Pattern p = Pattern.compile(""); Matcher m = p.matcher(input); if (m.find(i)) { System.out.print("Roll number doesn't start with IAD"); }
if i enter the input which starts with IAD123 then i have to print the marks of that specified student if it is there or else say that the roll number doesn't exist
- 01-20-2011, 06:56 PM #14
Crossposted: need help with 2D array - Java Programming Forums
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-20-2011, 08:40 PM #15
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
-
Similar Threads
-
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
create a 2d char array from a 1D string array
By jschmall12 in forum New To JavaReplies: 1Last Post: 04-27-2010, 09:01 PM -
Convert Char Array to String Array
By Mayur in forum New To JavaReplies: 8Last Post: 10-12-2009, 11:41 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
.gif)

Bookmarks