Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: need help with 2D array

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: need help with 2D array

    Do you have a specific part of the problem that you're stuck on? Have you written any code for this already and if so, can you paste it here please along with the problem you're having with it.

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with 2D array

    Here is the code which i have so far
     int size = 0;
     Scanner scan = new Scanner(System.in);
            System.out.print("Enter the size of the array s: ");
            size = scan.nextInt();
     System.out.println("size is :" + size);
            String[][] arr = new String[size][3];
            System.out.print("Enter the Roll Number:");
            String rollnumber = scan.next();
            System.out.println(rollnumber.split("IAD")[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.isDigit(studentname.charAt(i))) {
                    System.out.println("Given input is in correct format");
                } else {
                    letterFound = true;
     
                }
            }
            System.out.println("" + studentname);
     
        }
    }


    I have loop till rowindex<= arraysize.

    How do i do that.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need help with 2D array

    System.out.println(rollnumber.split("IAD")[1]);

    You're printing an array?

    Also, I'm positive that's invalid syntax.

    It's

    public String[] split(String regularExpression)
    {

    }

    or

    public String[] split(String regularExpression, int limit)
    {

    }

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need help with 2D array

    Also, your for loop will change letter found the first time it finds the letter and it'll still be true for the next iteration of your for loop.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: need help with 2D array

    Quote Originally Posted by javapenguin
    Also, I'm positive that's invalid syntax.
    Are you sure you are positive? Did you try it?

  7. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need help with 2D array

    System.out.println(rollnumber.split("IAD")[1])
    Doesn't seem right to me, with the [] around the 1.

    If anything, it'd be split("IAD", 1);

    Actually, it could be

    System.out.println(Arrays.toString(rollnumber.spli t("IAD", 1)));
    Last edited by javapenguin; January 20th, 2011 at 02:19 PM.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: need help with 2D array

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: need help with 2D array

    Quote Originally Posted by javapenguin View Post
    Doesn't seem right to me, with the [] around the 1.

    If anything, it'd be split("IAD", 1);

    Actually, it could be

    System.out.println(Arrays.toString(rollnumber.spli t("IAD", 1)));
    But what happened when you tried it?

    Because you're wrong. The syntax is perfectly legal. Yet again, you're offering advice that is absolutely not true. I'm a little more forward about calling you out on it than the other members, but it's really misleading to the other novice programmers that we're trying to help.

    Please, please, please, at least start trying these things yourself before you post about them. Throwing together a sample program to test this syntax took me less than 30 seconds.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need help with 2D array

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: need help with 2D array

    Quote Originally Posted by javapenguin View Post
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    Okay, but that's not a syntax error, as you erroneously indicated. That's not a compile-time error, it's a runtime error. It's not a problem with the syntax itself ("with the [] around the 1", as you said), it's a problem with the logic. Do you see how that can be confusing?

    If you think there's a problem with the syntax, throw together a simple program to prove it, without worrying about any other logic errors. Here's one that proves that the syntax is fine:
    class Main  {
        public static void main(String[] ar){
        	System.out.println("1 2 3 4 5".split(" ")[1]);
        }
    }
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need help with 2D array

    It should be

    System.out.println(rollnumber.split("IAD")[0]);

    I see now that the 0 would be index 0 of the array the split method is creating.
    Last edited by javapenguin; January 20th, 2011 at 02:41 PM.

  13. #13
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need help with 2D array

    Also, I don't know what:
      System.out.print("Enter the size of the array s: ");
    		        size = scan.nextInt();
    		 System.out.println("size is :" + size);
    		        String[][] arr = new String[size][3];

    is doing. Appears to be doing nothing and is never used.

  14. #14
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: need help with 2D array

    Quote Originally Posted by javapenguin View Post
    It should be

    System.out.println(rollnumber.split("IAD")[0]);

    I see now that the 0 would be index 0 of the array the split method is creating.
    Sigh. No. Again, wrong. Again, misleading advice.

    What did you get when you ran that line? Or a similar line? Here, I'll even give you this example program again, since you don't seem to know how to make one yourself:

    class Main  {
        public static void main(String[] ar){
        	System.out.println("IAD123".split("IAD")[0]);
        }
    }

    In my opinion, it seems like overkill for the OP to use anything except substring here. And no, that doesn't mean you should attempt to throw together a full solution for him.

    And that's just the first issue. The OP's main problem of storing things into a 2D array should be treated as completely separate.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. #15
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: need help with 2D array

    Quote Originally Posted by javapenguin View Post
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    What does that have to do with the syntax?

    EDIT: Didn't notice there was a second page. KevinWorkman is on it.

  16. #16
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: need help with 2D array

    Javapenguin, am I going to have to send another warning your way (this one won't be as lenient)? Please stop handing out incorrect advice...if you don't know the answer or can't address the original posters problem then move on. Its doing more harm than good throwing out information that is completely incorrect (especially when others must continually sweep up the mess left behind, and especially for something so simple that can be tested in the matter of seconds).

  17. #17
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need help with 2D array

    Quote Originally Posted by radhi16 View Post
    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 .
    String rollNumber = console.nextLine();

    String number = rollNumber.substring(3);

    Where do you want them stored at in the array?

  18. #18
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need help with 2D array

    Quote Originally Posted by copeg View Post
    Javapenguin, am I going to have to send another warning your way (this one won't be as lenient)? Please stop handing out incorrect advice...if you don't know the answer or can't address the original posters problem then move on. Its doing more harm than good throwing out information that is completely incorrect (especially when others must continually sweep up the mess left behind, and especially for something so simple that can be tested in the matter of seconds).
    The problem is that sometimes I, like others, don't know we're doing it wrong.

    I didn't realize I could get kicked out for giving advice, especially advice that seems to make sense to me at the time. I compiled the original code the OP gave, found that it gave an error, which I posted, pointed out that if the value were changed to 0, it wouldn't show that error, and got read the riot act. Also, I didn't notice the OPs question. I kinda saw the code and thought that's what I should focus on. My bad.

  19. #19
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: need help with 2D array

    Quote Originally Posted by javapenguin View Post
    The problem is that sometimes I, like others, don't know we're doing it wrong.
    IMO completely beside the point. Part of giving advice is not just throwing out what you think is best, its what you know - and programming is so clearly defined that you can easily test what you know: the example above is a perfect example - double check yourself (by spending 30 second to check that your information is correct before handing over advice - even if you think you know) and you can possibly learn something in the process as well as not mislead a poster as well as not get read the riot act.

    Quote Originally Posted by javapenguin View Post
    I didn't realize I could get kicked out for giving advice, especially advice that seems to make sense to me at the time. I compiled the original code the OP gave, found that it gave an error, which I posted, pointed out that if the value were changed to 0, it wouldn't show that error, and got read the riot act. Also, I didn't notice the OPs question. I kinda saw the code and thought that's what I should focus on. My bad.
    You got read the riot act because you consistently try to give advice you think you may know, and could have just as easily tested your knowledge with a simple test script, but instead insist on feeding out information that you have not checked. You have done this a lot in the past (and been warned about it to boot)...Its not just posting incorrect info once or twice, its doing it consistently - at times so much so that sometimes I have to spend my valuable time cleaning up the mess left behind.

  20. #20
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: need help with 2D array

    Quote Originally Posted by javapenguin View Post
    The problem is that sometimes I, like others, don't know we're doing it wrong.
    Which is why you should be extra careful when posting advice. I'm no expert, but I've been programming Java for 8 years or so, and I still throw together sample programs all the time to make sure my assumptions are true. If you're going to post advice, you have to test your assumptions, especially since it's apparent that your level of understanding is oftentimes lower than that of the original posters.

    When I originally saw your post about the syntax being incorrect, I thought you were wrong, but before I posted anything, I tested that assumption by writing that basic program. It took me less than 30 seconds. Why can't you do the same thing before you post your assumptions, especially given your track record of posting incorrect information?

    Quote Originally Posted by javapenguin View Post
    I didn't realize I could get kicked out for giving advice, especially advice that seems to make sense to me at the time.
    I'm not a mod, so I don't have any say in what forms of punishment you can/should receive, but I have personally showed you the problems with posting incorrect information, and I've suggested you try code yourself before offering invalid assumptions about it, yet you continue to ignore that advice and continue to mislead other users. That's bad for the users who follow your bad advice, it's bad for the forums as they become known for this kind of response, it's bad for you because you don't learn by testing your assumptions, and it's bad for anybody else who stumbles upon your faulty advice later. Not to mention the distraction caused by the repeated need to tell you about this, again and again.

    You also have a strange propensity towards offering full solutions, despite the fact that they are often wrong and are never helpful (I've shown you many times why spoonfeeding is not helping). Posting code examples can be a great way to help, but you take away the process of using examples to work out a solution, which is one of the most important skills an amateur must learn. I told the OP about the substring function (to be fair, I only mentioned it to point out how the previous discussions were overkill, as I don't really love helping crossposters), and I even told you that it wasn't an invitation to take that and try to come up with the code yourself because I know how you are, but still you post the exact code in your next reply. Why? Who are you trying to impress? If you want to learn by trying to solve the problems, that's fine, but why take that process away from the other users? You aren't helping.

    For these reasons, and because of your refusal to learn from your mistakes, if it were up to me, you'd be banned already. I'm not telling you to stop helping- in fact, I'm trying to help you be more helpful. But you continue to refuse to listen to any of this advice, and you continue to give out incorrect information, which would have been very easily corrected with a simple test program.

    Quote Originally Posted by javapenguin View Post
    I compiled the original code the OP gave, found that it gave an error, which I posted, pointed out that if the value were changed to 0, it wouldn't show that error, and got read the riot act.
    That's because none of that is helpful. Sure changing it to 0 gets rid of that runtime error, but what does that have to do with syntax? And further, changing it to 0 does NOT fix the OP's actual problem. Do you understand why? Did you even bother running the test program I wrote for you?

    Quote Originally Posted by javapenguin View Post
    Also, I didn't notice the OPs question. I kinda saw the code and thought that's what I should focus on. My bad.
    So let me get this straight- you don't even read the posts before trying to offer your shoddy advice? That explains a lot.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  21. The Following 2 Users Say Thank You to KevinWorkman For This Useful Post:

    copeg (January 21st, 2011), DavidFongs (January 21st, 2011)

Similar Threads

  1. Char array to a String array with hex
    By fortune2k in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2014, 01:01 PM
  2. prininting out a 4x4 array according to an order by a 1d array
    By fortune2k in forum Collections and Generics
    Replies: 7
    Last Post: November 25th, 2010, 12:54 PM
  3. 2d (4x4) array insdie a 1d array. (Block cipher)
    By fortune2k in forum Collections and Generics
    Replies: 13
    Last Post: November 23rd, 2010, 05:29 PM
  4. [SOLVED] Create new Array from old Array
    By satory in forum Collections and Generics
    Replies: 1
    Last Post: February 24th, 2010, 12:44 PM
  5. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM