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.
Re: need help with 2D array
Here is the code which i have so far
Code :
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.
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)
{
}
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.
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?
Re: need help with 2D array
Quote:
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)));
Re: need help with 2D array
Re: need help with 2D array
Quote:
Originally Posted by
javapenguin
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.
Re: need help with 2D array
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
Re: need help with 2D array
Quote:
Originally Posted by
javapenguin
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:
Code :
class Main {
public static void main(String[] ar){
System.out.println("1 2 3 4 5".split(" ")[1]);
}
}
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.
Re: need help with 2D array
Also, I don't know what:
Code java:
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.
Re: need help with 2D array
Quote:
Originally Posted by
javapenguin
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:
Code :
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.
Re: need help with 2D array
Quote:
Originally Posted by
javapenguin
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.
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).
Re: need help with 2D array
Quote:
Originally Posted by
radhi16
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?
Re: need help with 2D array
Quote:
Originally Posted by
copeg
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.
Re: need help with 2D array
Quote:
Originally Posted by
javapenguin
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
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.
Re: need help with 2D array
Quote:
Originally Posted by
javapenguin
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
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
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
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.