• Post Reply Bookmark Topic Watch Topic
  • New Topic

stuck at the if-elseif-else structure  RSS feed

 
Kyle Lykens
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I'm currently working on a project, using arrays to store data about a phonebook. I currently have the following code, but I'm a little stuck at the if-elseif-else structure right now. The code runs fine, but I don't know how to implement the different things I'm trying to do. For if (x == 1), I want the code to display the whole name and phone number of everyone matching the last name 'Smith', and for (x == 2), I want to display the whole name and phone number of the person with the matching phone number.

The code compiles fine right now, I just don't know if:
A) I declared my Arrays right (firstname, lastname, number).
B) How to implement what I described above

Any help is much appreciated. Thank you!

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the first thing you should do is bring out a pencil and paper (or white board) and come up with a plan. Pick one of the things you need to do - say for example looking for the people with last name Smith.

You know you will have to:

- Iterate over the list of last names.
- Compare the name in the array to "Smith"
- If the name matches, get the first name and phone number for the same person
- display the person
- Keep searching until you know there are no more Smiths to be found.

Now that we have our steps, pick one, and turn it into smaller steps. And then try to translate it to the Java Code. Do that for each step, and you should have a pretty good idea of what you need to write to get the functionality you want.

p.s. There are many other things that you could do to make things easier - like making a Class which encapsulates all the data for a single person. This would make searching and finding all the related information easier, but I assume you are just learning and it is easier to do one thing at a time, and for now, that is planning out how to do complicated steps.
 
Kyle Lykens
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:Well the first thing you should do is bring out a pencil and paper (or white board) and come up with a plan. Pick one of the things you need to do - say for example looking for the people with last name Smith.

You know you will have to:

- Iterate over the list of last names.
- Compare the name in the array to "Smith"
- If the name matches, get the first name and phone number for the same person
- display the person
- Keep searching until you know there are no more Smiths to be found.

Now that we have our steps, pick one, and turn it into smaller steps. And then try to translate it to the Java Code. Do that for each step, and you should have a pretty good idea of what you need to write to get the functionality you want.

p.s. There are many other things that you could do to make things easier - like making a Class which encapsulates all the data for a single person. This would make searching and finding all the related information easier, but I assume you are just learning and it is easier to do one thing at a time, and for now, that is planning out how to do complicated steps.


Thank you, I'll get cracking on the steps suggestion as fast as I can.
As for the class, I attempted to make a class called Resident and defined it with firstname, lastname, and numbers, but I was getting shrouded in errors that I didn't understand, so I took it out and kept going.
 
Kyle Lykens
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just an update. I found a way to do this, but when I go to enter an integer into the scanner, I get an error. Here is my updated code:



And here is the error (The error takes place after I type 1 and press enter in the scanner):
java.lang.ArrayIndexOutOfBoundsException: 1
at FinalProject.main(FinalProject.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

Edit: No error if I type 2, 3, or 4. Just if I type 1.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kyle Lykens wrote:No error if I type 2, 3, or 4. Just if I type 1.


Well, that part is easy. It's only when you type 1, and the variable x gets assigned 1, that "x == 1" is true. So this is the only time that any significant code is executed.

And the stack trace tells you which line of code is throwing the exception. It's line 32 -- you can see that from "at FinalProject.main(FinalProject.java:32)". So start by looking at line 32. The error message is saying you're trying to access element 1 of an array (that would be the second element, since the index starts at zero) but there aren't that many elements in the array you're working with.
 
Kyle Lykens
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So up in line 14, where I declared that Array, do I need to set an amount?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kyle Lykens wrote:So up in line 14, where I declared that Array, do I need to set an amount?

What is line 32 in your code? My guess is the array which is giving you the problem isn't one you created in line 14, but one you created inside the if (x==1) block. Why do you split the last name? what are the results of that split?
 
Kyle Lykens
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:
What is line 32 in your code? My guess is the array which is giving you the problem isn't one you created in line 14, but one you created inside the if (x==1) block. Why do you split the last name? what are the results of that split?


I don't know why I split the last name from the first name. Would it make a difference if I grouped them back together?
Line 32 is listed above in the code, but it's exeone.add(n);
 
Kyle Lykens
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a final update that I need help with. Here is my code:



I can compile and run it with no errors. Options 2 and 3 work correctly. Option 1 doesn't give me an error, it just gives me nothing. The console recognizes that the input was 1, but then nothing happens. I've tried using continue or break statements to get the program to jump to the lastnameSmith method, but it doesn't seem to want to. Any help? This is the last thing I need for my program! D:
Edit: I can confirm that the for loop is working. It adds the two 'Smith' entries to the exeone array. I just need a way for the code to skip to the last method. Anyone?
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kyle Lykens wrote:I just need a way for the code to skip to the last method.


"Skip to" a method isn't a thing you do in Java. Probably you meant you wanted to call that lastnameSmith method. Which your code actually does, at line 15. However I don't think you want to call the method before you've done any of the processing, do you? I think you want to call it after all of that. Probably inside the block controlled by "if (x == 1)"?
 
Kyle Lykens
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Kyle Lykens wrote:I just need a way for the code to skip to the last method.


"Skip to" a method isn't a thing you do in Java. Probably you meant you wanted to call that lastnameSmith method. Which your code actually does, at line 15. However I don't think you want to call the method before you've done any of the processing, do you? I think you want to call it after all of that. Probably inside the block controlled by "if (x == 1)"?


You, sir, are a life saver. I was going to submit it without fixing it, because this project is due at midnight, but with your helpful tip, I was able to get it working. I thought that line 15 was defining it, not calling it, so you also helped me learn that. You have my thanks x1000!
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, line 47 and onwards is where you declare the method. Funny how even a small misunderstanding can really mess you up in programming, isn't it?
 
Kyle Lykens
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know, even the slightest thing. But again, thank you for the assistance. I appreciate it very much.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.

Not even experienced programmers are immune to screwing up small details. Several years ago I named two columns in a database table "txnumber" and "txnnumber", which anybody can see is an accident waiting to happen. (Don't ask me why I did that. I was an experienced programmer then as well.) And then yesterday I wrote code which put code into the "txnumber" column instead of the "txnnumber" column. Made quite a mess of the database before I found the problem.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kyle Lykens wrote:
Steve Luke wrote:
What is line 32 in your code? My guess is the array which is giving you the problem isn't one you created in line 14, but one you created inside the if (x==1) block. Why do you split the last name? what are the results of that split?


I don't know why I split the last name from the first name. Would it make a difference if I grouped them back together?

I see in your new code you put the names back together so the split would work, but that just added more work for you. What you could have done was simply not call lastname[n].split() since the last name was already split from the first name (they were in different arrays). Obviously no reason to go back and change now, but just wanted to comment on it.

Kyle Lykens wrote:Line 32 is listed above in the code, but it's exeone.add(n);
The line 32 above is line 32 as displayed in our forum. The error message at FinalProject.main(FinalProject.java:32) was talking about line 32 in your .java file. Since your compiler doesn't know about our forum or how it would display lines, you should look for line 32 of your code in your file, not line 32 displayed on the forum. I can tell you that the problem was not with exeone.add(n), because that line can't cause ArrayIndexOutOfBoundsException. It had to be from accessing a value in an array (for example an array index like name[1].)

I know you already sorted things and got it working (gratz by the way, always feels great to finally get it working but I just wanted to bring it up to help with any future troubleshooting.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!