Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By gimbal2

Thread: error when trying to retun array via a method

  1. #1
    nischalinn is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Default error when trying to retun array via a method

    I am getting these errors with this code, I can not figure why this error is occuring.
    Can anyone tell me what is the issue with this code?

    Java Code:
    package ABC;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    
    public class GetInputFromUser
    {
    	private ArrayList<String> name = new ArrayList<String>();  
    	char quit = 'Y';		
    	
    	public ArrayList<String > GetArrayInput(){ //This method must return a result of type ArrayList<String>
    			
    		while (quit == 'Y') {
    			System.out.println("Please enter your name: ");
    			name.add(in.next());        
    			System.out.print(" Enter Another Record? (Y/N)");
    			String word = in.next();
    			word = word.toUpperCase();
    			quit= word.charAt(0);
    			
    			return name;
        }
    	}	
    }
    And when I do this I get the following error;

    Java Code:
    public class GetInputFromUser
    {	
    
    	char quit = 'Y';	
    	
    	public ArrayList<String > GetArrayInput(){ 
    		private ArrayList<String> name  = new ArrayList<String>();	
    		// get error in name "Illegal modifier for parameter name; only final is permitted"
    		while (quit == 'Y') {
    			System.out.println("Please enter your name: ");
    			name.add(in.next());        
    			System.out.print(" Enter Another Record? (Y/N)");
    			String word = in.next();
    			word = word.toUpperCase();
    			quit= word.charAt(0);
    			
    			return name;
        }
    	}	
    }
    Thanks!

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,095
    Rep Power
    4

    Default Re: error when trying to retun array via a method

    You cannot make a variable private in a method. The illegal modifier is "private".
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: error when trying to retun array via a method

    For the first one, your return is INSIDE the while loop, is that REALLY what you wanted?

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: error when trying to retun array via a method

    Are there screenshots in this thread I'm not seeing due to the corporate firewall? Otherwise I'm seriously confused :)

    Note to OP: if you have attachments, upload them to the forum thread and not some untrustworthy external site such as pastebin. But in the end it is far easier to just copy/paste the error itself as plain text.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,095
    Rep Power
    4

    Default Re: error when trying to retun array via a method

    No attachments, just code. You must have skipped your morning coffee :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: error when trying to retun array via a method

    lol okay :) My confusion remains then that you say "this error happens" and then not post the error. But I'm weird that way that I expect that you provide what you're talking about.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: error when trying to retun array via a method

    Quote Originally Posted by gimbal2 View Post
    lol okay :) My confusion remains then that you say "this error happens" and then not post the error. But I'm weird that way that I expect that you provide what you're talking about.
    The "errors" are summarized in a comment in each code block.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: error when trying to retun array via a method

    Okay never mind, I'm blind. Apologies to the OP that I'm making assumptions based on the wrongful doings of other people.

    If you need me, I'll be ----------------------------------------> over here for a while thinking about my sins.
    masijade likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 0
    Last Post: 10-04-2013, 06:27 AM
  2. Replies: 7
    Last Post: 04-11-2013, 06:31 AM
  3. Replies: 4
    Last Post: 03-28-2013, 09:40 PM
  4. Error when adding an array into main method
    By abeidson in forum New To Java
    Replies: 7
    Last Post: 03-12-2013, 05:49 PM
  5. ERROR: Calling array from a method
    By agente47 in forum New To Java
    Replies: 2
    Last Post: 03-25-2011, 04:18 PM

Tags for this Thread

Posting Permissions

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