Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-10-2008, 09:46 PM
Member
 
Join Date: Jan 2008
Posts: 5
jazzinspace is on a distinguished road
Stuck and Frustrated.
I have run this method through BlueJ in excess of 100 times to no avail. Please could someone correct my code

question:

/**
*A password-checking method called testNext has a signature:
public static String testNext(ArrayList<String> pins, String pin)

The method checks the String pin against each of the items in the ArrayList pins,
and if it finds a match it returns the next password in the ArrayList as a String,
otherwise it returns an empty String "" as a "rogue" (i.e. warning) value.
It also returns an empty String if the password found is the last one in the ArrayList.
Create the method.
Enter your answer (all the code between the braces that enclose the method) below:

public static String testPrevious(ArrayList<String> pins, String pin) {

String m = pin;
for (int i = 0; i < pins.size(); i++){
if (pins.get(i).equals(m)) {
if ( i == pins.size()-1) {
return "";
} return pins.get(i+1);
}

//else {return "";}



}

return "";
}
}

p.s as to the uncertainty of whether it is testnext or testprevious that is down to the poor quality question build. however it doesnt really matter which it is as i can try your code with (i+1) and (i-1) can't I.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-10-2008, 11:27 PM
jelly's Avatar
Member
 
Join Date: Jan 2008
Location: Somerset, UK
Posts: 46
jelly is on a distinguished road
Is this the exact code for the method or have you included some code for the class? I ask because there is an extra } at the end of you method code. Is it failing to compile or to run properly?
__________________
-- Hope that helps
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-10-2008, 11:33 PM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
You have an extra }

and make sure you imported ArrayList.

It is important that you are able to understand the errors you receive. You should have gotten an error such as :

"Syntax error on token } , delete this token"

This obviously is saying that you have an extra }

And if you didn't import ArrayList you will get an error such as:

"ArrayList cannot be resolved to a type"

This means that it doesn't know what ArrayList is, so you didn't import it.

Error messages are extremely helpful and not too hard to understand. Just read them and you should have no trouble.
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-10-2008, 11:41 PM
Member
 
Join Date: Jan 2008
Posts: 5
jazzinspace is on a distinguished road
sorry guys I forgot to mention that I have already included the arraylist into the program and that semi colon is a typo. the thing does compile. It says however that the last password was checked correctly but it could not find the password in the middle. I have had some more attempts since and this is what I have come up with (still doesnt work tho).

for (String pw: pins) {
for (int i = 0; i < pw.length(); i++){
if (pins.get(i) == pin) {
return pins.get(i-1);}
}
}
return "";

Comments:
Test 1 succeeded (0 marks):

The compilation was successful


Test 2 failed (exit code = 1):

The output should have been:
Test number: 2
Password in list checked correctly
Last Password correctly processed

This is what was actually produced:
Test number: 2
More work needed - password in list not found
Last Password correctly processed

Mark: 0 out of 1

This code is for an online test that I'm doing and so doesnt need the rest of the program, just the method.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-11-2008, 05:47 PM
Member
 
Join Date: Jan 2008
Posts: 5
jazzinspace is on a distinguished road
apparently I had too many loops and needed to take one out. Have done that but it still needs tweaking some how. Current update:

for (int i = 0; i < pins.size(); i++){
if (pins.get(i).equals(pin)) {
return pins.get(i+1);
}
if (i == pins.size()) {
return "";}
}
return "";

Test 1 succeeded (0 marks):

The compilation was successful


Test 2 failed (exit code = 1):

The output should have been:
Test number: 2
Password in list checked correctly
Last Password correctly processed

This is what was actually produced:
Test number: 2
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 8, Size: 8
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Main.testNext(Main.java:128)
at Main.main(Main.java:71)

Mark: 0 out of 1
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-11-2008, 05:56 PM
jelly's Avatar
Member
 
Join Date: Jan 2008
Location: Somerset, UK
Posts: 46
jelly is on a distinguished road
You are always attempting to return the value at i+1 before testing whether you are at the end of the List or not

You should have, using your code
Code:
if (pins.get(i).equals(pin)) { if (i == pins.size()) { return "";} } return pins.get(i+1); }
__________________
-- Hope that helps

Last edited by jelly : 01-11-2008 at 05:59 PM.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-12-2008, 05:02 AM
Member
 
Join Date: Jan 2008
Posts: 5
jazzinspace is on a distinguished road
this is rediculous, the thing is unbeatable guys. current update (over 132 attempts).


for(int i = 0; i < pw.size(); i++) {
if (pins.get(i).equals(pin)) {
if (i == pins.size()) {
return "";}
return pw.get(i+1);
}
}
return "";

Comments:
Test 1 succeeded (0 marks):

The compilation was successful


Test 2 failed (exit code = 1):

The output should have been:
Test number: 2
Password in list checked correctly
Last Password correctly processed

This is what was actually produced:
Test number: 2
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 8, Size: 8
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Main.testNext(Main.java:130)
at Main.main(Main.java:71)



I took ur advice jelly and went with this setup. I don't actually know why I have to have a return ""; statement at the end but it says missing return statement if I don't put it there :S This is the hardest java question I have come across by far.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-12-2008, 04:38 PM
jelly's Avatar
Member
 
Join Date: Jan 2008
Location: Somerset, UK
Posts: 46
jelly is on a distinguished road
the reason you have to have a return statement at the end is because the method is declared as returning a String, theoretically you may not go through the for loop, the compiler can see a route ( no entries in the ArrayList) where the for loop would never be entered and hence needs to find a return statement on that path.

To be honest I would rewrite the code. If the question you posed at the beginning is the whole question that you need to answer then you never need to check the last entry in the ArrayList. If you get to the last entry and it matches you return "", if you get to the last entry and it doesn't match you return "" so no point in checking. Hence I would code it as

Code:
public static String testNext (ArrayList<String> pins, String pin) { // set up the default return value String returnValue = ""; // loop around the array list looking for a match up // to and including the last but one entry for (int i = 0; i < (pins.size()-1); i++){ // check the current entry for equality if (pins.get(i).equals(pin)) { // found a match so set the return // value and break out of the loop returnValue = pins.get(i+1); break; } } // when we get here either we found a matching entry // with a pin to return or we run through the whole // ArrayList and found no suitable match. Either way // we just return the returnValue variable. return returnValue; }
I only checked that code by hand so apologies for any typos
__________________
-- Hope that helps

Last edited by jelly : 01-12-2008 at 04:46 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stuck on Two Questions, Please Help sylo18 New To Java 5 03-11-2008 03:03 AM
musically stuck cry for help 2 geork New To Java 0 02-07-2008 04:09 PM
musically stuck geork New To Java 1 02-06-2008 11:44 PM
hi there i am stuck with a construct can anyone help??? sonal New To Java 3 12-05-2007 04:22 AM
I am completely stuck jpnym15 New To Java 2 11-14-2007 08:40 PM


All times are GMT +3. The time now is 01:58 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org