0

User input into array.

E.g.

    private void addVideo()
{
    Scanner in = new Scanner( System.in );
    String message = "Video";
    System.out.print("Enter Video Name: ");
    return in.nextLine();
}

In in class:

    public static String nextLine()
{   
    return in.nextLine(); 
}

I have compile error 'incompatible types: unexpected return value.' What is the problem? explanation will be appreciated.

Thanks!

How do I assign the entered video name into an array? e.g. first video name into id array(id=1) second is id = 2 and so on?

2
  • why the return type of method addVideo() is void when its returning string Commented Apr 11, 2015 at 17:35
  • 1
    method signature is void but returning String Commented Apr 11, 2015 at 17:35

3 Answers 3

2
private String addVideo() {
        Scanner in = new Scanner(System.in);
        String message = "Video";
        System.out.print("Enter Video Name: ");
        return in.nextLine();
    }

in.nextLine() is returning a String value, so you have to use String not void

Sign up to request clarification or add additional context in comments.

Comments

1

If you wanted to store the video names into an array you could just use the method outlined by Nikolai Hristov and then add each instance to an array like this

String[] videos = new String[10];
videos[0] = addVideo();
videos[1] = addVideo(); 

Each call to addVideo() will prompt the user for a video name and then return a String to that position of the array.

Comments

1

You have defined a method with the return type void yet you used a return statement in the method.

As for how to do it, try something like this:

List<String> list = new ArrayList<String>();

public String addVideo() {
    Scanner in = new Scanner(System.in);
    String message = "Video";
    System.out.println("Enter Video Name: ");
    String input = in.nextLine();
    list.add(input);
    return input;
}

Each time you call addVideo you will add one video to the list.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.