Monday 15 July 2013

bluej - Can't display search results from query in Java -



bluej - Can't display search results from query in Java -

i still new programming , needing help finishing program.

it simple plenty program, written in bluej, takes inputs user displayed or searched for.

i struggling both of these methods (display , search) maintain getting message displayed implemented display there no records display.

public class collection extends dvd { //private arraylist<dvd> dvds; //private int[] array; private dvd dvd1; private dvd dvd2; private dvd dvd3; /** * constructor objects of class collection */ public collection() { //array = new int[2]; //dvd1 = dvd1; //dvd2 = dvd2; //dvd3 = dvd3; dvd1 = new dvd(); dvd2 = new dvd(); dvd3 = new dvd(); } public static void main(string args[]) { //creates instance of collection class collection collection = new collection(); collection.menu(); } public void displaydvds() { float totalprice = 0; totalprice = dvd1.getdvdprice() + dvd2.getdvdprice() + dvd3.getdvdprice(); double totalruntime = 0; totalruntime = dvd1.getruntime() + dvd2.getruntime() + dvd3.getruntime(); if (dvd1.gettitle() == "" || dvd1.getdirector() == "" || dvd1.getlead() == "" || dvd1.getruntime() == 0 || dvd1.getdvdprice() == 0 || dvd2.gettitle() == "" || dvd2.getdirector() == "" || dvd2.getlead() == "" || dvd2.getruntime() == 0 || dvd2.getdvdprice() == 0 || dvd3.gettitle() == ""|| dvd3.getdirector() == "" ||dvd3.getlead() == "" ||dvd3.getruntime() == 0 ||dvd3.getdvdprice() == 0) { system.out.println("sorry, there no search results display."); } else { system.out.println(" \ndvd collection:\n dvd1:\n title: " + dvd1.gettitle() + " \ndirector: " + dvd1.getdirector() + " \nlead act: " + dvd1.getlead() + " \nrun time: " + dvd1.getruntime() + " \nprice: " + dvd1.getdvdprice()); system.out.println(" \ndvd collection:\n dvd1:\n title: " + dvd2.gettitle() + " \ndirector: " + dvd2.getdirector() + " \nlead act: " + dvd2.getlead() + " \nrun time: " + dvd2.getruntime() + " \nprice: " + dvd2.getdvdprice()); system.out.println(" \ndvd collection:\n dvd1:\n title: " + dvd3.gettitle() + " \ndirector: " + dvd3.getdirector() + " \nlead act: " + dvd3.getlead() + " \nrun time: " + dvd3.getruntime() + " \nprice: " + dvd3.getdvdprice()); system.out.println(" \ncombined cost of combined dvd's: " + totalprice); system.out.println(" \ncombined run time of combined dvd's: " + totalruntime); } } public void searchdvd() { string temp = ""; // temporary variable hold title // assign dvd 3's title temp system.out.println ("\nplease come in title search for: "); temp= genio.getstring(); if(temp.equals(dvd1.gettitle())) { system.out.println("\ndvd nowadays in collection @ location 1 (dvd 1 in collection): " + dvd1.gettitle()); } if(temp.equals(dvd2.gettitle())) { system.out.println("\ndvd nowadays in collection @ location 2 (dvd 2 in collection): " + dvd2.gettitle()); } if(temp.equals(dvd3.gettitle())) { system.out.println("\ndvd nowadays in collection @ location 3 (dvd 3 in collection): " + dvd3.gettitle()); } else system.out.println("\nsorry, there no search results display.\n "); } public void menu() { //declare alternative field int option; //start while loop menu { //display menu system.out.println("1: add together (up 3) dvd's collection"); system.out.println("2: display dvd collection"); system.out.println("3: search dvd collection collection"); system.out.println("4: quit program"); //prompt user come in selection system.out.println("please select alternative (1 - 4): "); //use genio user input option=genio.getinteger(); // alternative 1 allows user add together 3 dvd's if (option == 1) { dvd1.getinputs(); dvd2.getinputs(); dvd3.getinputs(); } // alternative 2 allows user display dvd collection if (option == 2) displaydvds(); //i alternative 3 allows user search dvd collection title if (option == 3) searchdvd(); } // alternative 4 print message tells programme may exited while (option != 4); system.out.println("you may close program"); }

entire dvd class:

public dvd( ) { dvdtitle = ""; dvddirector = ""; dvdlead = ""; dvdruntime = 0; dvdprice = 0; } public void getinputs() { system.out.println("please come in dvd title: "); dvdtitle=genio.getstring(); system.out.println("please come in dvd director: "); dvddirector=genio.getstring(); system.out.println("please come in dvd lead actor/actress: "); dvdlead=genio.getstring(); system.out.println("please come in dvd run time: "); dvdruntime=genio.getinteger(); system.out.println("please come in dvd cost: "); dvdruntime=genio.getinteger(); } public string gettitle(){ homecoming dvdtitle; } public string getdirector(){ homecoming dvddirector; } public string getlead(){ homecoming dvdlead; } public double getruntime() { homecoming dvdruntime; } public float getdvdprice() { homecoming dvdprice; }

}

genio class:

public class genio { /** * constructor objects of class genio, nil needing constructed! */ public genio() { } /** * getstr() private method safely returns string utilize * public methods getstring() , getcharacter() in class. * * @return string farther processing withing class */ private static string getstr() { string inputline = ""; bufferedreader reader = new bufferedreader(new inputstreamreader(system.in)); seek { inputline = reader.readline(); } catch(exception exc) { system.out.println ("there error during reading: " + exc.getmessage()); } homecoming inputline; } /** * getinteger() returns integer value. exception handling used trap * invalid info - including floating point numbers, non-numeric characters * , no data. in event of exception, user prompted come in * right info in right format. * * @return validated int value */ public static int getinteger() { int temp=0; boolean ok = false; bufferedreader keyboard = new bufferedreader(new inputstreamreader(system.in)); { seek { temp = integer.parseint(keyboard.readline()); ok = true; } grab (exception eref) { if (eref instanceof numberformatexception) { system.out.print("integer value needed: "); } else { system.out.println("please study error: "+eref.tostring()); } } } while(ok == false); return(temp); } /** * getfloat() returns floating point value. exception handling used trap * invalid info - including non-numeric characters , no data. * in event of exception (normally no info or alpha), user prompted come in * info in right format * * @return validated float value */ public static float getfloat() { float temp=0; boolean ok = false; bufferedreader keyboard = new bufferedreader(new inputstreamreader(system.in)); { seek { temp = float.parsefloat(keyboard.readline()); ok = true; } grab (exception eref) { if (eref instanceof numberformatexception) { system.out.print("number needed: "); } else { system.out.println("please study error: "+eref.tostring()); } } } while(ok == false); return(temp); } /** * getdouble() returns double precision floating point value. * exception handling used trap invalid info - including non-numeric * characters , no data. * in event of exception, user prompted come in * info in right format * * @return validated double precision value */ public static double getdouble() { double temp=0; boolean ok = false; bufferedreader keyboard = new bufferedreader(new inputstreamreader(system.in)); { seek { temp = double.parsedouble(keyboard.readline()); ok = true; } grab (exception eref) { if (eref instanceof numberformatexception) { system.out.print("number needed: "); } else { system.out.println("please study error: "+eref.tostring()); } } } while(ok == false); return(temp); } /** * getcharacter() returns character keyboard. * reading string taking first character read. subsequent characters * discarded without raising exception. * method checks ensure character has been entered, , prompts * if has not. * * @return validated character value */ public static char getcharacter() { string tempstr=""; char temp=' '; boolean ok = false; { seek { tempstr = getstr(); temp = tempstr.charat(0); ok = true; } grab (exception eref) { if (eref instanceof stringindexoutofboundsexception) { // means nil entered prompt ... system.out.print("enter character: "); } else { system.out.println("please study error: "+eref.tostring()); } } } while(ok == false); return(temp); } /** * getstring() returns string entered @ keyboard. * @return string value */ public static string getstring() { string temp=""; seek { temp = getstr(); } grab (exception eref) { system.out.println("please study error: "+eref.tostring()); } return(temp); }

} apologise if code written terribly methods work when invoke them individually.

as mentioned, problem when select display or search (after adding records), message "sorry, there no search results display."

the problem dvdprice never getting set dvd's staying @ 0. set dvdruntime twice in getinputs() method.

public void getinputs() { system.out.println("please come in dvd title: "); dvdtitle=genio.getstring(); system.out.println("please come in dvd director: "); dvddirector=genio.getstring(); system.out.println("please come in dvd lead actor/actress: "); dvdlead=genio.getstring(); system.out.println("please come in dvd run time: "); ***dvdruntime=genio.getinteger();*** //should dvdruntime = genio.getdouble(); system.out.println("please come in dvd cost: "); ***dvdruntime=genio.getinteger();*** //should dvdprice = genio.getfloat(); }

also if statement should this:

if (dvd1.gettitle().equalsignorecase("")) {

when comparing strings utilize equals or equalsignorecase method not ==. changed dvd1.gettitle().equalsignorecase because come in 3 dvd's @ same time if title of first 1 hasn't been entered none of them have.

here total working code both classes although there still somethings should changed:

collection:

public class collection { // private arraylist<dvd> dvds; // private int[] array; private dvd dvd1 = null; private dvd dvd2 = null; private dvd dvd3 = null; /** * constructor objects of class collection */ public collection() { // array = new int[2]; // dvd1 = dvd1; // dvd2 = dvd2; // dvd3 = dvd3; dvd1 = new dvd(); dvd2 = new dvd(); dvd3 = new dvd(); } public static void main(string args[]) { // creates instance of collection class collection collection = new collection(); collection.menu(); } public void displaydvds() { float totalprice = 0; totalprice = dvd1.getdvdprice() + dvd2.getdvdprice() + dvd3.getdvdprice(); double totalruntime = 0; totalruntime = dvd1.getruntime() + dvd2.getruntime() + dvd3.getruntime(); if (dvd1.gettitle().equalsignorecase("")) { system.out .println("sorry, there no search results display."); } else { system.out.println(" \ndvd collection:\n dvd1:\n title: " + dvd1.gettitle() + " \ndirector: " + dvd1.getdirector() + " \nlead act: " + dvd1.getlead() + " \nrun time: " + dvd1.getruntime() + " \nprice: " + dvd1.getdvdprice()); system.out.println(" \ndvd collection:\n dvd1:\n title: " + dvd2.gettitle() + " \ndirector: " + dvd2.getdirector() + " \nlead act: " + dvd2.getlead() + " \nrun time: " + dvd2.getruntime() + " \nprice: " + dvd2.getdvdprice()); system.out.println(" \ndvd collection:\n dvd1:\n title: " + dvd3.gettitle() + " \ndirector: " + dvd3.getdirector() + " \nlead act: " + dvd3.getlead() + " \nrun time: " + dvd3.getruntime() + " \nprice: " + dvd3.getdvdprice()); system.out.println(" \ncombined cost of combined dvd's: " + totalprice); system.out.println(" \ncombined run time of combined dvd's: " + totalruntime); } } public void searchdvd() { string temp = ""; // temporary variable hold title // assign dvd 3's title temp system.out.println("\nplease come in title search for: "); temp = genio.getstring(); if (temp.equals(dvd1.gettitle())) { system.out .println("\ndvd nowadays in collection @ location 1 (dvd 1 in collection): " + dvd1.gettitle()); } if (temp.equals(dvd2.gettitle())) { system.out .println("\ndvd nowadays in collection @ location 2 (dvd 2 in collection): " + dvd2.gettitle()); } if (temp.equals(dvd3.gettitle())) { system.out .println("\ndvd nowadays in collection @ location 3 (dvd 3 in collection): " + dvd3.gettitle()); } else system.out .println("\nsorry, there no search results display.\n "); } public void menu() { // declare alternative field int option; // start while loop menu { // display menu system.out.println("1: add together (up 3) dvd's collection"); system.out.println("2: display dvd collection"); system.out.println("3: search dvd collection collection"); system.out.println("4: quit program"); // prompt user come in selection system.out.println("please select alternative (1 - 4): "); // utilize genio user input alternative = genio.getinteger(); // alternative 1 allows user add together 3 dvd's if (option == 1) { dvd1.getinputs(); dvd2.getinputs(); dvd3.getinputs(); } // alternative 2 allows user display dvd collection if (option == 2) displaydvds(); // alternative 3 allows user search dvd collection title if (option == 3) searchdvd(); } // alternative 4 print message tells programme may // exited while (option != 4); system.out.println("you may close program"); } }

dvd:

public class dvd { private string dvdtitle; private string dvddirector; private string dvdlead; private double dvdruntime; private float dvdprice; public dvd() { dvdtitle = ""; dvddirector = ""; dvdlead = ""; dvdruntime = 0; dvdprice = 0; } public void getinputs() { system.out.println("please come in dvd title: "); dvdtitle = genio.getstring(); system.out.println("please come in dvd director: "); dvddirector = genio.getstring(); system.out.println("please come in dvd lead actor/actress: "); dvdlead = genio.getstring(); system.out.println("please come in dvd run time: "); dvdruntime = genio.getdouble(); system.out.println("please come in dvd cost: "); dvdprice = genio.getfloat(); } public string gettitle() { homecoming dvdtitle; } public string getdirector() { homecoming dvddirector; } public string getlead() { homecoming dvdlead; } public double getruntime() { homecoming dvdruntime; } public float getdvdprice() { homecoming dvdprice; } }

java bluej

No comments:

Post a Comment