• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

Czyszczenie bufora klasy Scanner

Object Storage Arubacloud
0 głosów
843 wizyt
pytanie zadane 3 września 2016 w Java przez Jonki Dyskutant (8,180 p.)

Zmienna 'choice' nie pozwala na podanie jej wartości z klawiatury gdy wychodzę z "2. Add movie / series to the database\r".

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 {
	
	private 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.next();
						
						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..." );
						System.out.println("9. Back\r" );
						System.out.print("\n Your choice: " );
						
						choice = input.nextLine();
						
						if(choice.equals("9")) {
							break;
						}
						
						switch(choice) {
							case "1": {
								addMovieToTheDatabase();
								
								break;
							}
							case "2": {
								addSeriesToTheDatabase();

								break;
							}
							default: {
								System.out.println("There is no such option in the menu2!");
							}
						}
					}
				}
				default: {
					System.err.println("There is no such option in the menu1!");
				}
			}
		}
	}
	
	/** 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.next();
		System.out.print("Release date DD/MM/YYYY: ");
		releaseDate = input.next();
		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;
		}
		
	}
	
	/** This method adds the series to the database */
	static void addSeriesToTheDatabase() {
		
		String title, description, director, releaseDate; 
		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.next();
		System.out.print("Description: ");
		description = input.nextLine();
		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("Rate 1-5: ");
		rate = input.nextInt();
		
		Series series = new Series(title, description, director, releaseDate, genres.get(indexGenre),
				country.get(indexCountry), rate);
		
		if(serialization(series)) {
			System.out.println("The film has been saved successfully!");
		} else {
			System.out.println("The film was not saved!");
		}
	}
	
	/** This method serializes the object series */
	static boolean serialization(Series series)
	{
		try {
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("series.ser", true));
			oos.writeObject(series);
			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;
		}
		
	}
}

Tak wynik

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


 Your choice: 2

1. Add movie...
2. Add series...
9. Back


 Your choice: 9
1. Browse data
2. Add movie / series to the database


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

Gdy wracam do głównego menu to wykonuje się automatycznie opcja 'default: '.

 

1 odpowiedź

0 głosów
odpowiedź 4 września 2016 przez Tomasz90 Nałogowiec (25,140 p.)
case, który się zaczyna w 54 linii nie ma breaka dlatego default się wywołuje. Tak odbiegając od tematu to w equals stała po lewej (jak jest zmienna to może NullPointerException polecieć). Można też porównywać przy pomocy Objects.equals(), a jeszcze lepiej było to na intach zrobić.

Podobne pytania

0 głosów
2 odpowiedzi 1,657 wizyt
pytanie zadane 2 stycznia 2017 w Java przez rubesom Obywatel (1,690 p.)
0 głosów
1 odpowiedź 183 wizyt
pytanie zadane 23 października 2020 w C i C++ przez adamos321 Nowicjusz (140 p.)
0 głosów
1 odpowiedź 449 wizyt
pytanie zadane 26 kwietnia 2020 w C i C++ przez ResCrove Obywatel (1,700 p.)

92,583 zapytań

141,434 odpowiedzi

319,669 komentarzy

61,966 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj.

Akademia Sekuraka

Kolejna edycja największej imprezy hakerskiej w Polsce, czyli Mega Sekurak Hacking Party odbędzie się już 20 maja 2024r. Z tej okazji mamy dla Was kod: pasjamshp - jeżeli wpiszecie go w koszyku, to wówczas otrzymacie 40% zniżki na bilet w wersji standard!

Więcej informacji na temat imprezy znajdziecie tutaj. Dziękujemy ekipie Sekuraka za taką fajną zniżkę dla wszystkich Pasjonatów!

Akademia Sekuraka

Niedawno wystartował dodruk tej świetnej, rozchwytywanej książki (około 940 stron). Mamy dla Was kod: pasja (wpiszcie go w koszyku), dzięki któremu otrzymujemy 10% zniżki - dziękujemy zaprzyjaźnionej ekipie Sekuraka za taki bonus dla Pasjonatów! Książka to pierwszy tom z serii o ITsec, który łagodnie wprowadzi w świat bezpieczeństwa IT każdą osobę - warto, polecamy!

...