Results 1 to 8 of 8
  1. #1
    jazzinspace is offline Member
    Join Date
    Jan 2008
    Posts
    5
    Rep Power
    0

    Default 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.

  2. #2
    jelly's Avatar
    jelly is offline Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    46
    Rep Power
    0

    Default

    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

  3. #3
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    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.

  4. #4
    jazzinspace is offline Member
    Join Date
    Jan 2008
    Posts
    5
    Rep Power
    0

    Default

    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.

  5. #5
    jazzinspace is offline Member
    Join Date
    Jan 2008
    Posts
    5
    Rep Power
    0

    Default

    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

  6. #6
    jelly's Avatar
    jelly is offline Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    46
    Rep Power
    0

    Default

    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
    Java Code:
    if (pins.get(i).equals(pin)) {
        if (i == pins.size()) {
            return "";}
        }
        return pins.get(i+1);
    }
    Last edited by jelly; 01-11-2008 at 03:59 PM.
    -- Hope that helps

  7. #7
    jazzinspace is offline Member
    Join Date
    Jan 2008
    Posts
    5
    Rep Power
    0

    Default

    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.

  8. #8
    jelly's Avatar
    jelly is offline Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    46
    Rep Power
    0

    Default

    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

    Java 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
    Last edited by jelly; 01-12-2008 at 02:46 PM.
    -- Hope that helps

Similar Threads

  1. Stuck on Two Questions, Please Help
    By sylo18 in forum New To Java
    Replies: 5
    Last Post: 03-11-2008, 01:03 AM
  2. musically stuck cry for help 2
    By geork in forum New To Java
    Replies: 0
    Last Post: 02-07-2008, 02:09 PM
  3. musically stuck
    By geork in forum New To Java
    Replies: 1
    Last Post: 02-06-2008, 09:44 PM
  4. Replies: 3
    Last Post: 12-05-2007, 02:22 AM
  5. I am completely stuck
    By jpnym15 in forum New To Java
    Replies: 2
    Last Post: 11-14-2007, 06:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •