Bücherrei Bücher verleih | 2 Fehler im Code

Also ich das Programm frägt am Anfang ob man sich die vorhandenen Bücher auflisten lassen will, eines suchen will oder die Bücherrei verlassen will. Falls man was komplett anderes eingibt, soll man wieder an den Anfang

Die falsche Eingabe funktioniert.

Die Auflistung funktioniert. Nach der Auflistung soll man nochmal zurück an den Anfang und das funktioniert auch.

Die Exit Funktion funktioniert auch.

Wenn man suche eingegeben hat, wird man gefragt ob man nach der id oder nach dem titel suchen möchte, diese Unterscheidung funktioniert auch. Oder ob man die Bücherei verlassen will, diese Funktion ist auch einwandfrei.

  1. Fall Titelsuche:
    Es soll gesucht werden ob das Buch da ist, wenn ja soll der Status abgefragt werden, wenn der status 0 ist, ist das Buch vorhanden und man wird gefragt ob man das Buch ausleihen will. Wenn ja, bedankt sich die Bücherrei, bei nein wird man zürck zur Suche geschickt.
    Funktionalität konnte ich hier nicht testen DA:
    -egal was man eingibt, das Buch wird nicht gefunden
    Ist das Buch nicht vorhanden soll gesagt werden das das Buch nicht im Inventar ist und wird wieder zurück zur suche geschickt, dies funktionert bedingt, den das ganze wird genau so oft ausgegeben wie die größe des Arrays.

  2. Fall Idsuche
    “–” Selbes Problem wie oben

Zusammenfassung:
-egal was man eingibt, das Buch wird nicht gefunden
-Meldung das das Buch nicht in der Bücherrei nicht vorhanden ist wird genau so oft ausgegeben wie die größe des Arrays ( 10 mal )

Hier mal der Code, danke schomal im Vorraus
[SPOILER]```package inventar;

import java.util.Scanner;

public class BookBorrow {

public static void main(String[] args){
	
	//new array, 10 rows 2 columns
	String[][] books = new String[10][3];
	//input 
	String customer = "";
	Scanner sc = new Scanner(System.in);
	//condition if program has to repeat
	int error = 0;
	int error2 = 0;
	//search option
	String search = "";
	//search by title
	String title = "";
	//search by id
	String id = "";
	//borrow?
	String borrow ="";
	
	
	//books
	books[0][0] = "Der Hobbit";
	books[1][0] = "Harry Potter und der Stein der Weisen";
	books[2][0] = "Totenbraut";
	books[3][0] = "Koma";
	books[4][0] = "Der gestiefelte Kater";
	books[5][0] = "Die Tribute von Panem";
	books[6][0] = "Game of Thrones";
	books[7][0] = "analytische Mathematik I";
	books[8][0] = "Das Drachenblut";
	books[9][0] = "Schattenzeichen";
	
	//Article number
	books[0][1] = "01";
	books[1][1] = "02";
	books[2][1] = "03";
	books[3][1] = "04";
	books[4][1] = "05";
	books[5][1] = "06";
	books[6][1] = "07";
	books[7][1] = "08";
	books[8][1] = "09";
	books[9][1] = "10";
	
	//status 0 available, 1 not available
	books[0][2] = "0";
	books[1][2] = "0";
	books[2][2] = "0";
	books[3][2] = "0";
	books[4][2] = "0";
	books[5][2] = "0";
	books[6][2] = "0";
	books[7][2] = "0";
	books[8][2] = "0";
	books[9][2] = "0";
	
	//if condition error = 0 while will end
	do{
		//Preparations
		System.out.println();
		System.out.println("Welcome to our online library.");
		System.out.println();
		System.out.println("If you want to see our offers, write list ");
		System.out.println("If you want to search a book, write search");
		System.out.println("If you want to close the online library write exit");
		System.out.println("-------------------------------------------------");
		System.out.println();
		customer = sc.nextLine();
		
			//list all the entries in books
			if(customer.equalsIgnoreCase("list")){
				
				System.out.println("Following books are available in our library");
				System.out.println();
				
				for(int i = 0; i <= 9;i++){
					System.out.println(books**[0]);
					error = 1;
				}
			// starts search process	
			}else if(customer.equalsIgnoreCase("search")){
				
				do{
					System.out.println("Do you want to search the book by its title or the id?");
					System.out.println("You also can exit the library by typing exit");
					search = sc.nextLine();
					
						if(search.equalsIgnoreCase("title")){
							System.out.println("Please insert now the title");
							title = sc.nextLine();
								for(int i = 0; i <= 9;i++){
									if(books**[0] == title){
										for(int i2 = 0; i2 <= 9;i2++){
											if(books**[2] == "0"){
												System.out.println("This book is available");
												System.out.println("Do you want to borrow it?");
												borrow = sc.nextLine();
													if(borrow.equalsIgnoreCase("yes")){
														System.out.println("You borrowed the book " + title );
														System.out.println("Thanks for your visit and bring back our treasure soon");
														System.out.println();
														System.out.println("You left the library");
														System.exit(0);
													}else{
														System.out.println("Oh we are sorry for that, you could try an outher one");
														error2 = 1;
													}
											}else{
												System.out.println();
												System.out.println("We are sorry, but this book is not available at the moment");
												System.out.println("You could look for an outher book");
												error2 = 1;
											}
										}
									}else{
										System.out.println();
										System.out.println("We are sorry but " + title + " is not in our inventory" );
										System.out.println("You could look for an outher book");
										error2 = 1;
									}
									

								}
							
						}else if(search.equalsIgnoreCase("id")){
							System.out.println("Please insert now the book id");
							id = sc.nextLine();
								for(int i = 0; i <= 9;i++){
									if(books**[1] == id){
										for(int i2 = 0; i2 <= 9;i2++){
											if(books**[2] == "0"){
												System.out.println("This book is available");
												System.out.println("Do you want to borrow it?");
												borrow = sc.nextLine();
													if(borrow.equalsIgnoreCase("yes")){
														System.out.println("You borrowed the book with the id " + id );
														System.out.println("Thanks for your visit and bring back our treasure soon");
														System.out.println();
														System.out.println("You left the library");
														System.exit(0);
													}else{
														System.out.println("Oh we are sorry for that, you could try an outher one");
														error2 = 1;
													}
											}else{
												System.out.println();
												System.out.println("We are sorry, but this book is not available at the moment");
												System.out.println("You could look for an outher book");
												error2 = 1;
											}
										}
									}else{
										System.out.println();
										System.out.println("We are sorry but the book with the " + id + " is not in our inventory" );
										System.out.println("You could look for an outher book");
										error2 = 1;
										}
								}
							
						}else if(search.equalsIgnoreCase("exit")){
							System.out.println("Library closed...");
							System.out.println("Thanks for your visit");
							System.exit(0);
						}else{
							System.out.println();
							System.out.println("incorrect input, please write 'title' or 'id' or 'exit' ");
							error2 = 1;
						}
					
				}while(error2 == 1);
				
			
			// close the library
			}else if(customer.equalsIgnoreCase("exit")){
				System.out.println("Library closed...");
				System.out.println("Thanks for your visit");
				System.exit(0);
			
			//return to the loop head if input is unknown
			}else{
				System.out.println("incorrect input");
				error = 1;
			}
		
		
	}while(error == 1);
	
}

}```[/SPOILER]

Ganz schlimmer Code! Unbedingt in Methoden mit sprechenden Namen und passenden Rückgabetypen und Parametern aufteilen!

Zum Problem, habe String-Vergleiche mit ‘==’ entdeckt. Ändere diese zu equals. Das wird vermutlich den Fehler beheben.

Strings vergleicht man per equals. Das hattest du in den anderen Fällen ja auch schon korrekt gemacht.

Ich bin eher noch Anfänger was das angeht, aber mit Methoden kriege ich das irgendwie nie hin, weil ich da voll durcheinander komme mit dem
Konstruktor ?! und wo was steht und pi pa po. Aber so sollte es auch gehen. Eigentlich…

also auch dieses equals ignore case? oder welches?

wenn du groß/klein-Unterschiede ignorieren willst, dann bitte, speziell bei String gibt es das

allgemein hat JEDE Klasse die equals-Methode, die für zwei unterschiedliche Objekte mit gleichen Inhalt true liefert
(falls entsprechend implementiert, String und andere Basisklassen können das, schlimmstenfalls == Vergleich als Standard-Vorgabe für alle Klassen,
Vorsicht bei null -> häufig NullPointerException)

== gibt dagegen nur true wenn es dieselben bzw. beide dasselbe eine Objekt ist

jede Eingabe ist immer ein neuer String, vielleicht mit gleichen Inhalt wie ein alter, vielleicht auch nicht, gewiss nie ==

Rheinwerk Computing :: Java ist auch eine Insel - 3 Klassen und Objekte
3.7.6 Gleichheit und die Methode equals()

So jetzt geht fast alles, das einzige was jetzt nicht funktioniert ist die Titelsuche. Das Komische ist, das erste Buch findet das Programm, aber den Rest nicht…

Code :

[SPOILER]```package inventar;

import java.util.Scanner;

public class BookBorrow {

public static void main(String[] args) {

	// new array, 10 rows 2 columns
	String[][] books = new String[10][3];
	// input
	String customer = "";
	Scanner sc = new Scanner(System.in);
	// condition if program has to repeat
	int error = 0;
	int error2 = 0;
	// search option
	String search = "";
	// search by title
	String title = "";
	// search by id
	String id = "";
	// borrow?
	String borrow = "";

	// books
	books[0][0] = "Der Hobbit";
	books[1][0] = "Harry Potter und der Stein der Weisen";
	books[2][0] = "Totenbraut";
	books[3][0] = "Koma";
	books[4][0] = "Der gestiefelte Kater";
	books[5][0] = "Die Tribute von Panem";
	books[6][0] = "Game of Thrones";
	books[7][0] = "analytische Mathematik I";
	books[8][0] = "Das Drachenblut";
	books[9][0] = "Schattenzeichen";

	// Article number
	books[0][1] = "01";
	books[1][1] = "02";
	books[2][1] = "03";
	books[3][1] = "04";
	books[4][1] = "05";
	books[5][1] = "06";
	books[6][1] = "07";
	books[7][1] = "08";
	books[8][1] = "09";
	books[9][1] = "10";

	// status 0 available, 1 not available
	books[0][2] = "0";
	books[1][2] = "0";
	books[2][2] = "0";
	books[3][2] = "0";
	books[4][2] = "0";
	books[5][2] = "0";
	books[6][2] = "0";
	books[7][2] = "0";
	books[8][2] = "0";
	books[9][2] = "0";

	// if condition error = 0 while will end
	do {
		// Preparations
		System.out.println();
		System.out.println("Welcome to our online library.");
		System.out.println();
		System.out.println("If you want to see our offers, write list ");
		System.out.println("If you want to search a book, write search");
		System.out
				.println("If you want to close the online library write exit");
		System.out
				.println("-------------------------------------------------");
		System.out.println();
		customer = sc.nextLine();

		// list all the entries in books
		if (customer.equalsIgnoreCase("list")) {

			System.out
					.println("Following books are available in our library");
			System.out.println();

			for (int i = 0; i <= 9; i++) {
				System.out.println(books**[0]);
				error = 1;
			}
			// starts search process
		} else if (customer.equalsIgnoreCase("search")) {

			do {
				System.out
						.println("Do you want to search the book by its title or the id?");
				System.out
						.println("You also can exit the library by typing exit");
				search = sc.nextLine();

				if (search.equalsIgnoreCase("title")) {
					System.out.println("Please insert now the title");
					title = sc.nextLine();
					int iBook = -1;
					for (int i = 0; i <= 9; i++) {
						if (books**[0].equalsIgnoreCase(title)) {
							iBook = i;
							break;
						}
					}
					if (iBook <= 0) {
						if (books[iBook][2].equalsIgnoreCase("0")) {
							System.out.println("This book is available");
							System.out.println("Do you want to borrow it?");
							borrow = sc.nextLine();
							if (borrow.equalsIgnoreCase("yes")) {
								System.out.println("You borrowed the book "
										+ title);
								System.out
										.println("Thanks for your visit and bring back our treasure soon");
								System.out.println();
								System.out.println("You left the library");
								System.exit(0);
							} else {
								System.out
										.println("Oh we are sorry for that, you could try an outher one");
								error2 = 1;
							}
						} else {
							System.out.println();
							System.out
									.println("We are sorry, but this book is not available at the moment");
							System.out
									.println("You could look for an outher book");
							error2 = 1;
						}

					} else {
						System.out.println();
						System.out.println("We are sorry but " + title
								+ " is not in our inventory");
						System.out
								.println("You could look for an outher book");
						error2 = 1;
					}

				} else if (search.equalsIgnoreCase("id")) {
					System.out.println("Please insert now the book id");
					id = sc.nextLine();
					int iBook = -1;
					for (int i = 0; i <= 9; i++) {
						if (books**[1].equalsIgnoreCase(id)) {
							iBook = i;
							break;
						}
					}
					if (iBook >= 0) {
						if ((books[iBook][2]).equalsIgnoreCase("0")) {

							System.out.println("This book is available");
							System.out.println("Do you want to borrow it?");
							borrow = sc.nextLine();
							if (borrow.equalsIgnoreCase("yes")) {
								System.out
										.println("You borrowed the book with the id "
												+ id);
								System.out
										.println("Thanks for your visit and bring back our treasure soon");
								System.out.println();
								System.out.println("You left the library");
								System.exit(0);
							} else {
								System.out
										.println("Oh we are sorry for that, you could try another one");
								error2 = 1;
							}
						} else {
							System.out.println();
							System.out
									.println("We are sorry, but this book is not available at the moment");
							System.out
									.println("You could look for another book");
							error2 = 1;

						}
					} else {
						System.out.println();
						System.out
								.println("We are sorry but the book with the "
										+ id + " is not in our inventory");
						System.out
								.println("You could look for another book");
						error2 = 1;
					}

				} else if (search.equalsIgnoreCase("exit")) {
					System.out.println("Library closed...");
					System.out.println("Thanks for your visit");
					System.exit(0);
				} else {
					System.out.println();
					System.out
							.println("incorrect input, please write 'title' or 'id' or 'exit' ");
					error2 = 1;
				}

			} while (error2 == 1);

			// close the library
		} else if (customer.equalsIgnoreCase("exit")) {
			System.out.println("Library closed...");
			System.out.println("Thanks for your visit");
			System.exit(0);

			// return to the loop head if input is unknown
		} else {
			System.out.println("incorrect input");
			error = 1;
		}

	} while (error == 1);

}

}

Das Problem liegt in dieser Abfrage:
if (iBook <= 0) {
Das sollte iBook >= 0 heißen.

Ja habs auch gesehen danke euch !

  • Programmlogik in der main
  • keine Methoden verwendet
  • “tiefe” Verschachtelung
  • eigenes Objekt für book/Book

Ich hab das mal enfädelt:

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

/**
 * @author CB
 */
public class BookBorrow {
    
    private static final BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
    private String[][] books = null;
    
    public BookBorrow(String[] names) {
        books = new String[names.length][3];
        for (int i = 0; i < names.length; i++) {
            // name
            books**[0] = names**;
            // id
            books**[1] = String.format("%03d", i + 1);
            // nicht ausgeliehen
            books**[2] = "0";
        }
    }
    
    public void startMenue() {
        a:
        for (;;) {
            System.out.print("list (1), search (2), rent (3), exit (4) : ");
            try {
                String lin = BR.readLine();
                int opt = -1;
                try {
                    if (lin != null && !(lin = lin.trim()).isEmpty() && (opt = Integer.parseInt(lin)) >= 1 && opt <= 4) {
                        switch (opt) {
                            case 1:
                                showList();
                                break;
                            case 2:
                                showSearch();
                                break;
                            case 3:
                                showRent();
                                break;
                            case 4:
                                showExit();
                                break a; // exit
                            default:
                                break a; // exit
                        }
                    }
                } catch (NumberFormatException nfe) {
                    // ignore
                }
            } catch (IOException ioe) {
                break a; // exit
            }
        }
    }
    
    private void showList() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    private void showSearch() throws IOException {
        System.out.print("id (1), name (2) : ");
        String line = BR.readLine();
        if (line != null) {
            line = line.trim();
            try {
                int opt = Integer.parseInt(line);
                if (opt == 1) {
                    throw new UnsupportedOperationException("Not supported yet.");
                } else if (opt == 2) {
                    System.out.println("name:");
                    line = BR.readLine();
                    if (line != null) {
                        line = line.trim().toLowerCase();
                        for (String[] book : books) {
                            if (book[0].equalsIgnoreCase(line) || book[0].toLowerCase().startsWith(line)) {
                                // gefunden
                                System.out.println(Arrays.toString(book));
                            }
                        }
                    }
                }
            } catch (NumberFormatException nfe) {
                // ignore
            }
        }
    }
    
    private void showRent() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    private void showExit() {
        System.out.println("program will exit");
    }
    
    public static void main(String[] args) {
        new BookBorrow(new String[]{
            "Der Hobbit",
            "Harry Potter und der Stein der Weisen",
            "Totenbraut",
            "Koma",
            "Der gestiefelte Kater",
            "Die Tribute von Panem",
            "Game of Thrones",
            "analytische Mathematik I",
            "Das Drachenblut",
            "Schattenzeichen"
        }).startMenue(); // or args
    }
}```


run:
list (1), search (2), rent (3), exit (4) : 2
id (1), name (2) : 2
name:
der hobbit
[Der Hobbit, 001, 0]
list (1), search (2), rent (3), exit (4) : 4
program will exit
BUILD SUCCESSFUL (total time: 35 seconds)



Man kann das xbeliebig machen. Übersichtlich ist es schon mal. Aus dem Hauptmenü (man bleibt nicht in Untermenüs) kommt man nur mit "4" oder mit ungültiger Methode.

- null
- trim()
- toLowerCase()
- equals()

wichtige Methoden.

Lange, sprechende Zeilen sind nicht schlimm, Methoden hingegen sehr.

- Konstruktor
- public non-static
- private non-static
- public static void main

Bis dann. :mad: :grr: :)

Edit:

- deutsch
- englisch
- switch
- String...

Ok, ich hab noch was vergessen.

Du brauchst eine Methode, der 1 bis n Optionen (String) als Parameter übergeben wird, und die 1 bis … (int) zurückgibt, wenn die Eingabe gültig war, oder 0, wenn ungültig, oder -1, wenn BufReader geschlossen wurde. übersichtlich

Dann zur Suche fallen mir (spontan) 6 Möglichkeiten/Optionen ein:

genaue Suche,
genaue case-insensitive,
Anfang,
Anfang case-insensitive,
beinhaltet,
beinhaltet case-insensitive.

Nun hast du aber nicht nur 10 Bücher, sondern 1 Millionen oder 15 Millionen Bücher und > 10 Zugriffe im Minutentakt, deshalb fallen RegEx whrs. schon mal weg.

Deshalb sollte man sich neben ‘Name genau’ auch ‘Name genau lowerCase’ speichern.

Aber ich denke mal, hier kommt es nicht auf Millisekunden an. Sondern hier ist wichtig, welche String-Methoden gibt es usw. (basics)

Grüße…

Bitte nicht BufferedReader für Benutzereingaben nutzen. Dafür gibt es die Scanner-Klasse.

Labels sind komplett unnötig und man sollte auch drauf verzichten. Anstatt break a; kann man auch einfach ein return; verwenden. Wobei man sich das auch sparen kann, wenn man einfach eine do-while-Schleife nimmt:


do
-- Hier die Eingabe auswerten.
while(Benutzer möchte das Programm nicht beenden)

Die einen sehen das so, die anderen so. BufferedReader ist für Anfänger, wegen weniger Abstraktion, besser geeignet.

@ TS : Nachbedingung einer Schleife ist: Gültige Eingabe, ungültige Eingebe, Programm beenden, Stream wurde geschlossen, man kommt um „Sprünge“ leider nicht drumrum. return ist auch ein „Sprung“.

Aber mehr Schreibarbeit.

[QUOTE=Unregistered]Die einen sehen das so, die anderen so. BufferedReader ist für Anfänger, wegen weniger Abstraktion, besser geeignet.

@ TS : Nachbedingung einer Schleife ist: Gültige Eingabe, ungültige Eingebe, Programm beenden, Stream wurde geschlossen, man kommt um “Sprünge” leider nicht drumrum. return ist auch ein “Sprung”.[/QUOTE]

Als ob ein Anfänger zwischen “BufferedReader readLine” und “Scanner nextXXX” unterscheiden würde, welches “abstrakter” ist. Einen Beginner interessiert wie man es komfortabel, shcnell und vor allem richtig macht. Daher sind die obigen Codebeispiele wieder zu mHaare raufen. Aber das ist ja nix Neues.

so ungern ich auch die Position eines gewissen Users verteidige,
hat Scanner eine recht ernste Problematik beim Einsatz der kleinen ‘next’-Methoden:
im Fehlerfall muss man im catch der richtigen Exception (einer RuntimeException, auf die man erstmal kommen muss) mit nextLine() das bisher Gelesene entfernen,

für relativ geringen Einsatz von Scanner kommt es vergleichsweise häufig zu Threads wie
Java Scanner exception handling - Stack Overflow
ein für Anfänger fast unmöglich zu lösendes Problem, eigentlich ein No-Go,

aber da man sich solchen Code eh zusammensammelt kann man noch damit leben,
wenn es nur häufiger genannt werden würde, in Tutorials oft ignoriert, die hasNextInt()-Methoden genauso,

da ist das Vorgehen, ganze Zeilen zu lesen und diese Schritt für Schritt zu parsen einfacher zu durchblicken,
passt exakt zur Eingabe ganzer Zeilen mit Enter getrennt (wie auch zu Text-Dateien, Standardfall),
ist mit Scanner und nextLine() auch möglich, wie Posting #1 zeigt


noch ein arges Problem:

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Programmauswahl: ");
        int a = sc.nextInt();
        System.out.print("Buchtitel: ");
        String t = sc.nextLine();
        System.out.println("Auswahl: " + a + ", Titel: " + t);
    }

Programm bricht nach Eingabe der Zahl ab bzw. läuft zum Ende durch,
das nextLine() liest die leere Restzeile der ersten Eingabe als zweite Eingabe…

Du hast meinen Beitrag glaub nicht richtig gelesen. Mir geht es um die Vermeidung von Labels und dass diese unnötig sind (mal abgesehen, dass diese alles für einen Anfänger komplexer machen). Mal so am Rande: Ich hab in all den Jahren in denen ich Java programmiert habe, nicht einmal ein Label benötigt. Und auch noch nie einen Fall gesehen, wo es die elegantere Lösung wäre. Desweiteren brauchts auch kein return-Statement, wenn man eine do-while nimmt.

In meinem Beispiel oben, habe ich das Programm laufen lassen, bei einer ungültigen Eingabe (was bei uns in einer solchen Übungsaufgabe immer Standard war). Es ist aber auch kein Problem, dass noch in die Abbruchsbedingung mit einzubauen.

@ TS : Du weißt wahrscheinlich nicht, was eine Nachbedingung einer Schleife ist.

@ Slater : Aber wird jetzt eine 100 % richtige Lösung, eine Komplettlösung erwartet? Darf diese allgemein hier geschrieben werden? (best practice) Für jede Menü"ebene" sollte es auch eine Methode geben…

Sry, Unregistered in diesem Thread/Thema war ich.

Edit: Philosophisch: Alle richtigen Rückgabewerte einer Methode können nur im Konsens entschieden werden.

wer erwartet was von wem?
was der Threadersteller will und wieviel jeder dazu schreiben will ist jedem selbst überlassen,

was die heutigen Beiträge von dir wollten ist:
wenn Code/ Ideen, in welcher Ausführlichkeit auch immer, dann mit den jeweiligen Dingen (Scanner, return) nicht den anderen, auch nur Meinungsäußerungen

aus meinem Posting sollte gar keine Erwartung an irgendwen und irgendwas abzulesen sein,
nur weitere 42 Cent zur Frage Scanner vs. BufferedReader

natürlich könnte ‚wenn es nur häufiger genannt werden würde, in Tutorials oft ignoriert‘ in Richtung Erwartung interpretiert werden, aber auch nur allgemein dahingesprochen,
blah blah, ich kann immer ganze Seiten schreiben zu unklaren Fragen…

Also wirklich:)

BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Prompft: ");
String zeile = null;
try {
    zeile = console.readLine();
} catch (IOException e) {
    e.printStackTrace();
}
System.out.println("Input: " + zeile);

Dazu braucht man:** InputStreamReader, BufferedReader, System.in, IOException, try-catch** - nicht direkt „wenig Abstraktion“ als

 Scanner sc = new Scanner(System.in);

was wenigstens nur eine magische Zeile ist, die einem Anfänger schneller zu einem Erfolgserlebnis verhilft.

*** Edit ***

OT: ich weiß es ist überflüssig, aber gerade Anfänger sollte man nie mit bedenklichem Code alleine lassen:

[quote=CyborgBeta;110669]

private void showSearch() throws IOException {
    System.out.print("id (1), name (2) : "); // den user ausführlicher informieren als das hier...
    String line = BR.readLine(); // sollte eigentlich schon in BR.readLine() getrimmt werden
        if (line != null) { // besser wäre. BR.readLine anstatt null einfach "" zurückgeben zu lassen, dann spart man sich das !=null Zeugs
        line = line.trim();
        try {
            int opt = Integer.parseInt(line); // zu kompliziert: viel besser ein switch mit Werten "1","2","3" -> keine überflüssige Numberformatexception möglich, 
            if (opt == 1) {
                throw new UnsupportedOperationException("Not supported yet."); // unsinnig: sollte im default des switch sein
            } else if (opt == 2) {
                System.out.println("name:"); // Verwirrend: sage dem User immer was zu tun ist "Suche nach:"
                line = BR.readLine();
                if (line != null) {
                    line = line.trim().toLowerCase(); // kürzer: line = (null==line) ? "" : line.trim(); am besten in BR.readLine vor dem return
                    for (String[] book : books) {
                       if (book[0].equalsIgnoreCase(line) || book[0].toLowerCase().startsWith(line)) { // komische Bedingung
                          // warum nicht gleich: book[0].toLowerCase().startsWith(line.toLowerCase())
                          // gefunden
                          System.out.println(Arrays.toString(book));
                       }
                   }
               }
           } // hier geht der VIERTE Block zu, deshalb lieber einen Kommentar her, fast unlesbar, wozu gehört das?
       } catch (NumberFormatException nfe) {
            // ignore // völliger Humbug, nie einem Anfänger so was zeigen: Exceptions DARF man nicht ignorieren -> Benutzer informieren, Abfangen, ...
       }
}
```[/quote]