-1

I have a method that helps to enter information about movies, then write them in the file.

Writing a name (eg The Shawshank Redemption) in the file only writes "The".

What can I do in this case?

System.out.print("Name movie:");
String name=input.next();
input.nextLine();
System.out.print("Genre:");
String genre=input.next();
input.nextLine();
System.out.print("Country:");
String country=input.next();
Movie movie = new Movie(name, genre, country);
input.nextLine();
write(movie);

Method write()

FileWriter fw = new FileWriter("Movie.txt", true); 
fw.write(i.name+" "+i.genre+" "+i.country+"\n");
fw.close();

Word is read to space, and in the .txt file only the first word to space is written

  • Debug buddy. Is the name , genre and country has been read properly? that eliminates the scanner issue. Are you sure if the issue is while writing? – Arun Gowda May 26 '20 at 10:18
  • It seems like input.next() reads just the input till space, and that is the reason you miss the 'whole' move name. – kolboc May 26 '20 at 10:19
  • are you sure you are setting correctly the name,genre and country? looks like you have an issue with that and the spaces... update the question please – ΦXocę 웃 Пepeúpa ツ May 26 '20 at 10:20
  • See this answer: https://stackoverflow.com/questions/22458575/whats-the-difference-between-next-and-nextline-methods-from-scanner-class – kolboc May 26 '20 at 10:20
  • If you formatted your code along rational and accepted lines you would be better able to understand its workings for yourself, as would we. – user207421 May 26 '20 at 10:20
  • Also note: separate concerns. WRITING data to a file is INDEPENDENT to where that data is coming from. Meaning: first focus on writing the code that reads user input. Just print that input, to see if it matches your expectation. Then, when that part works, write a second program that takes *predefined, hardcoded* data, and writes that to a file. And only when that part works, too ... then bring your code together. – GhostCat May 26 '20 at 10:25

2 Answers2

0

Replace

String name=input.next();input.nextLine();

with

String name=input.nextLine();

The problem with your current approach is that input.next() is scanning only the first word and storing it into name. The change will scan the complete line (i.e. the full name of the movie) instead of stopping the scan after the first word.

Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72
0

You are scanning the inputs wrong. Here is an example:

Scanner input = new Scanner(System.in);
System.out.print("Name movie:"); 
String name=input.nextLine();
System.out.print("Genre:"); 
String genre=input.nextLine();
System.out.print("Country:"); 
String country=input.nextLine();
System.out.println(name + " " + genre + " " + country);

Output:

Name movie:new movie
Genre:this genre
Country:that country
new movie this genre that country

In this way, you scan the whole line at each input instead of scanning the first word and jumping to an empty line after that.

Majed Badawi
  • 16,831
  • 3
  • 12
  • 27