1

I'm trying to create a videoStore with the basic CRUD operation. For creating each movie I need to read the title, the year and the gender as below:

System.out.print("name: ");
name = in.nextLine();
System.out.print("year: ");
year = in.nextInt();
in.nextLine();
System.out.print("gender: ");
gender = in.next();

When I enter the addMovie option, I get this print on the console

(name: year:) 

Can someone explain to me why it happens as above?

Here is the rest of the method:

static ArrayList<Movie> movies = new ArrayList<Movie>();
static Scanner in = new Scanner(System.in);

public static void InserirFilme() {
    String name;
    int year;
    String gender;
    boolean existe = false;
    System.out.print("name: ");
    name = in.nextLine();
    System.out.print("year: ");
    year = in.nextInt();
    in.nextLine();
    System.out.print("gender: ");
    gender = in.next();

    Movie movie = new Movie(name, year, gender);
    for(Movie m: movies)
    {
        if(movie == m)
        {
            existe = true;
        }
    }

    if(!existe)
    {
        movies.add(movie);
    }
    else 
    {
        System.out.println("the movie already exists in the videoStore");
    }
}
Unni Kris
  • 2,963
  • 3
  • 27
  • 57

5 Answers5

1

Calling next does not remove the line break, which means the next time you call InserirFilme the call to read the name can complete immediately. Use nextLine.

System.out.print("gender: ");
gender = in.nextLine();

(You probably mean "genre" instead of "gender" though)

Also, as mentioned in the comments, this check will never succeed:

if(movie == f)
Joni
  • 101,441
  • 12
  • 123
  • 178
  • the prob is not on the gender, the prob is when i try to type the first input(name) before that the console just show me name: year: that is what is displayed on the print, the thing is it prints the name then it should read the name of the movie but instead it prints the year :S –  Sep 16 '16 at 08:48
  • The problem is with what comes *before* reading name. In the code you post, the way genre is read has an error - you may also have the same error in code you have not posted. – Joni Sep 16 '16 at 09:00
0

when i enter the addMovie option, i get this print on the console (name: year:) can someone explain me why it happens i already searched a lot and i cant understand why :S

The way i understood your question is that you are getting the output (name: year: ) in a line and want it in seperate lines? In that case you simply can use System.out.println(String); instead of System.out.print(String). On the other hand you can also use "\n" whenever you want a linebreak within a String. Hope i could help you :).

Edit: If this was not an answer to your question, feel free to tell me and clarify your question :)

Rüdiger
  • 701
  • 2
  • 15
  • 46
  • that is not the problem, before appear the year, the console should wait for the input to the name and then show me the year to input the year also –  Sep 16 '16 at 08:44
  • Okay now i know what you mean, how did you initialize the scanner? For me it kinda worked with Scanner in = new Scanner(System.in); – Rüdiger Sep 16 '16 at 09:13
0

For String name you are using in.nextLine(); i.e the data entered on the entire line will be added to name string.

After "name: " is displayed, enter some text and press enter key, so that the year and gender fields will get correct values.

The code written is correct but you are not giving appropriate input through the scanner.

I recommend to use

String name = in.next();//instead of String name = in.nextLine();
Rohit Gaikwad
  • 2,992
  • 3
  • 12
  • 32
  • what i learned was that if the string in this case name has more then 1 word i should use nextLine if not i can use next, in this case i can have a movie called butterfly effect with 2 words –  Sep 16 '16 at 08:46
0

You run this method in loop (right?)
The first call reads input correctly, but it leaves the linebreak in System.in after the last in.next().
On next call the name: is printed, then scanner reads an empty string from System.in because the linebreak already exists here.
And after thet the year: is printed on the same line because no new linebreaks are entered.

So you just have to insert another in.nextLine() after reading gender (or genre :) )
Or use nextLine() for read genre instead of next(), because genre might have more than one word.

But there are some disadvantages with using fake nextLine() to 'eat' linebreak - there might be another text which you doesn't process. It's a bad practice - to loose the data user entered.
It is better to read all the data from line, then validate/parse it, check isn't there some extra data, and if the data is invalid show notification and let him try to enter the right value.

Here are some examples how to deal with user input manually - https://stackoverflow.com/a/3059367/1916536. This is helpful to teach yourself.
Try to generalize user input operations:

name = validatedReader.readPhrase("name: ");
year = validatedReader.readNumber("year: ");
genre = validatedReader.readWord("genre: ");

where ValidatedReader is a custom wrapper for Scanner which could use your own validation rules, and could gently re-ask user after a wrong input. It could also validate dates, phone numbers, emails, url's or so

For production purposes, it is better to use validation frameworks with configurable validation rules. There are a lot of validation frameworks for different purposes - Web, UI, REST etc...

Community
  • 1
  • 1
Denis Kurochkin
  • 1,033
  • 1
  • 14
  • 27
  • great explanation, i understood everything, just a last question: what are the good practice in this cases? i dont want to be the programmer that cook spaghety code, i want to do good code can you give me a tip in this situation? –  Sep 16 '16 at 13:01
0

You may instantiate Scanner Class differently for String and Integer type input. It works for me :)

Example:

static Scanner in1 = new Scanner(System.in);
static Scanner in2 = new Scanner(System.in);

Please use nextLine() for 'name' and 'gender'. It may contain more than one word. Let me know if it works.

Example:

System.out.print("name: ");
name = in1.nextLine();
System.out.print("year: ");
year = in2.nextInt();
System.out.print("gender: ");
gender = in1.nextLine();

  • This question already has an accepted answer and your answer is neither really clear nor does it add additional information. – Guenther Sep 16 '16 at 17:00