Zmienna nie czeka na dane z klawiatury

0

Zmienna 'choise' po wyjściu z metody 'addMovieToTheDatabase' nie pozwala na wpisanie danych i wykonuje raz pętlę wywołując w instrukcji switch wybór 'default: '.

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; // Signals that an I/O exception of some sort has occurred
import java.io.ObjectOutputStream;
import java.nio.file.Files; // This class consists exclusively of static methods that operate on files, directories, or other types of files
import java.nio.file.Paths; // An object that may be used to locate a file in a file system. It will typically represent a system dependent file path
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner; // A class for reading data from the input

/** The main class of the program */
public class Database {
	
	static Scanner input = new Scanner(System.in);
	static String choice;
	
	/** Main method */
	static public void main(String[] args) {
		
		createTheRequiredFiles();
		
		while(true) {
			
			System.out.println("1. Browse data" );
			System.out.println("2. Add movie / series to the database\r" );
			System.out.print("\n Your choice: " );
			
			choice = input.nextLine();
			
			switch(choice) {
				case "1": {
					while(true) {
						System.out.println("\n1. Movie database" );
						System.out.println("2. Serials database\r" );
						System.out.print("\n Your choice: " );
						
						choice = input.nextLine();
						
						switch(choice) {
							case "1": {
								//////////////////
								break;
							}
							case "2": {
								//////////////////
								break;
							}
							default: {
								System.err.println("There is no such option in the menu!");
							}
						}
					}
				}
				case "2": {
					while(true) {
						System.out.println("\n1. Add movie..." );
						System.out.println("2. Add series...\r" );
						System.out.print("\n Your choice: " );
						
						choice = input.nextLine();
						
						switch(choice) {
							case "1": {
								addMovieToTheDatabase();
								
								break;
							}
							case "2": {
								//////////////////
								break;
							}
							default: {
								System.err.println("There is no such option in the menu!");
							}
						}
					}
				}
				default: {
					System.err.println("There is no such option in the menu!");
				}
			}
		}
	}
	
	/** This method produces the required files */
	static final void createTheRequiredFiles() {
		try {
			Files.createFile(Paths.get("movies.ser"));
			Files.createFile(Paths.get("series.ser"));
		} catch(IOException e) {}
	}
	
	/** This method adds the movie to the database */
	static void addMovieToTheDatabase() {
		
		String title, description, director, releaseDate; 
		int budget; int indexGenre, indexCountry; int rate;
		
		Map<Integer, String> genres = new HashMap<Integer, String>(7);
		genres.put(1, "Comedy");genres.put(2, "Adventure");genres.put(3, "Fantasy");
		genres.put(4, "Animation");genres.put(5, "Horror");genres.put(6, "Thriller");
		genres.put(7, "Action");
		
		Map<Integer, String> country = new HashMap<Integer, String>(3);
		country.put(1, "USA");country.put(2, "UK");country.put(3, "Poland");
		
		System.out.print("\nTitle: ");
		title = input.nextLine();
		System.out.print("Description: ");
		description = input.nextLine();
		System.out.print("Director: ");
		director = input.nextLine();
		System.out.print("Release date DD/MM/YYYY: ");
		releaseDate = input.nextLine();
		System.out.print("Genre: ");
		for(Integer i : genres.keySet()) {
			System.out.print(i + ". " + genres.get(i) + " ");
		}
		System.out.print("\nChoice: ");
		indexGenre = input.nextInt();
		System.out.print("Country: ");
		for(Integer i : country.keySet()) {
			System.out.print(i + ". " + country.get(i) + " ");
		}
		System.out.print("\nChoice: ");
		indexCountry = input.nextInt();
		System.out.println("Budget: ");
		budget = input.nextInt();
		System.out.println("Rate 1-5: ");
		rate = input.nextInt();
		
		Movie movie = new Movie(title, description, director, releaseDate, genres.get(indexGenre),
				country.get(indexCountry), budget, rate);
		
		if(serialization(movie)) {
			System.out.println("The film has been saved successfully!");
		} else {
			System.out.println("The film was not saved!");
		}
	}
	
	/** This method serializes the object movie. */
	static boolean serialization(Movie movie)
	{
		try {
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("movies.ser", true));
			oos.writeObject(movie);
			oos.close();
			
			return true;
		} catch (FileNotFoundException e) {
			System.out.println("Can not find the file. Please try again later...");
			return false;
		} catch (IOException e) {
			System.out.println("Unable to save file. Please try again later...");
			return false;
		}
		
	}
}
import java.io.Serializable;

/**  Object class film */
public class Movie implements Serializable {
	private static final long serialVersionUID = 1L;
	
	/**  Title of the movie */
	private String title;
	/**  Movie description */
	private String description;
	/**  The film's director */
	private String director; 
	/**  Movie Release Date */
	private String releaseDate;
	/**  Film genre */
	private String genre;
	/**  Country of production */
	private String country;
	/**  The film's budget */
	private int budget;
	/**  Rating movie */
	private int rate;
	
	public Movie(String title, String description, String director, String releaseDate, String genre, String country,
			int budget, int rate) {
		
		this.title = title;
		this.description = description;
		this.director = director;
		this.releaseDate = releaseDate;
		this.genre = genre;
		this.country = country;
		this.budget = budget;
		this.rate = rate;
	}
}

Wygląda to tak

 1. Browse data
2. Add movie / series to the database


 Your choice: 2

1. Add movie...
2. Add series...


 Your choice: 1

Title: sdf
Description: sdf
Director: sdf
Release date DD/MM/YYYY: sdf
Genre: 1. Comedy 2. Adventure 3. Fantasy 4. Animation 5. Horror 6. Thriller 7. Action 
Choice: 1
Country: 1. USA 2. UK 3. Poland 
Choice: 1
Budget: 
1
Rate 1-5: 
1
The film has been saved successfully!

1. Add movie...
2. Add series...


 Your choice: 
1. Add movie...
2. Add series...


 Your choice: There is no such option in the menu!

Raz wykonuje się pętla bez czekania na wpisanie. W tym momencie jak nacisnę '1' to mogę dodawać kolejny film.

0

Wie ktoś jak wyczyścić ten bufor?

0

tak na skróty to możesz jeszcze raz wczytać

choice

na końcu metody addMovieToTheDatabase()

 lub w wewnątrz switcha po wywołaniu tej metody,
jednak przydałoby się popracować trochę nad tym jeszcze bo te wewnętrzne switche kręcą w kółko u Ciebie a chyba nie o to chodziło do końca....
0

nextLine czyści bufor, np. tak:

        System.out.println("Rate 1-5: ");
        rate = input.nextInt();
        input.nextLine();

1 użytkowników online, w tym zalogowanych: 0, gości: 1