Up vote 0 down vote favorite share g+ share fb share tw.
I'm trying to input a number of strings (song names) into an array. The program will then ask the user to name one of songs and tell the user what position that song was placed in the array. Edit: Thanks for the help guys.
I've set both for loops to 0 and i'm still having trouble. I'm having various issues with the program. I'm getting the runtime errors of ArrayIndexOutOfBoundsException and NullPointerExeption.
What should I be doing to make it work?. Thanks in advance to everyone. Code: import javax.swing.
*; // import the swing library for I/O class favsongs { public static void main (String param) { music(); System. Exit(0); } // END main /* *************************************************** Set up an array containing songs then find one asked for by the user */ public static void music() { // Declare variables // String key =""; //the thing looked for int result = -1;// the position where the answer is found String songs = new String5; // Ask for songs for (p=0; p "); } // Ask user for a song key = JOptionPane. ShowInputDialog("Name a song and i'll tell you what position you placed it in.
"); for (int i=0; I Length; i++) { if (songsi. Equals(key)) { result = i; } else // Error message { JOptionPane. ShowMessageDialog(null,"Error!
"); break; } } // Tells user the name of the song and what position in the array it is in JOptionPane. ShowMessageDialog(null,"You placed " + key + " in position " + " " + result); } // END music } // END class favsongs java arrays string user-input link|improve this question edited Nov 22 '11 at 8:48 asked Nov 21 '11 at 19:42user105845263.
1 ArrayIndexOutOfBoundsException and NullPointerExeption are runtime-exceptions so you don't get those from the compiler. –? Nov 21 '11 at 19:50.
Look at this loop: for (p=1; p Length; i++) { if (songsp. Equals(key)) Not only are you trying to use p here when I think you mean i, but either way will fail. Using p will access songs5 which is out of bounds, and using I will call equals on songs0 which is null.
Hopefully that's enough of a hint to get you going. A few other points: Your "Sorry, What? " prompt is inside the loop.
Did you mean it to be? You should look at the Java naming conventions, both for capitalization and to give your methods more meaningful names You should try to declare variables in as tight a scope as possible. You wouldn't have been able to make the mistake of using p at the wrong place if you'd declared it as part of the for loop.
Ask for songs for (p=1; p When you try to use it later you'll get a NullPointerException.
First off, your loop to put songs into the is starting at 1, trying setting p=0, arrays go from zero to length -1. Try this and then see if it fixes the issue.
Your problem seems to be here: for (p=1; p Doing like so should fix your problem: for (p=0; p You might also want to give a look at the ArrayList Class, which will allow you to dynamically add items to your list as shown in this simple tutorial.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.