First Last
First Last

Reputation: 1

Store user input String into array

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?

Upvotes: 0

Views: 879

Answers (3)

Bassinator
Bassinator

Reputation: 1724

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.

Upvotes: 1

camelsaucerer
camelsaucerer

Reputation: 301

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.

Upvotes: 1

Nikolay Hristov
Nikolay Hristov

Reputation: 1702

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

Upvotes: 2

Related Questions