Results 1 to 16 of 16
  1. #1
    radhi16 is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

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

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    Hi.
    Have you tried to do it yourself?
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by radhi16 View Post
    Any suggestions on how to program .
    Have you tried anything on your own for this?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    radhi16 is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    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.

  5. #5
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by radhi16 View Post
    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.
    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:

    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;
       }
    }
    Hope this helps.

    Best,
    --user0--

  6. #6
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

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

  7. #7
    radhi16 is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    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.

  8. #8
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by radhi16 View Post
    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.
    Did you see the traversal code example I gave you? What part are you still confused about? Is it reading input from Scanner? Please explain.
    --user0--

  9. #9
    radhi16 is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    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 .

  10. #10
    radhi16 is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    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();
    
    
    }
    }
    }

  11. #11
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

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

  12. #12
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by radhi16 View Post
    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();
    
    
    }
    }
    }
    You are trying to get the number of rows correct? So why do you need a for loop? Simply do after the System.out.print("Enter the size of the array s: ");:
    Java Code:
    size = scan.nextInt();
    --user0--

  13. #13
    radhi16 is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    Here is the code which i have written so far
    Java 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");
                }
    Can any one help me in writing the code for
    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

  14. #14
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. #15
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    I'm not surprised - the OP created 3 threads yesterday for the same assignment. BTW how do you figure out something is cross posted? Do you simply google it?
    --user0--

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by user0 View Post
    I'm not surprised - the OP created 3 threads yesterday for the same assignment. BTW how do you figure out something is cross posted? Do you simply google it?
    He keeps this up and no one will want to help him. Google usually will find cross-posts, but I'm betting that Kevin is a member of several forums.

Similar Threads

  1. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  2. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  3. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 09:01 PM
  4. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 11:41 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •