Greetings all. I have created a human database program. However, to be brief, it has errors, but I can't seem to find out what the problem is. After I have stared at this for about a hour, I decided to turn to a forum for help. If someone could point out this Java newbie in the right direction, it would be greatly apreciated. Here is the source:
import java.io.*;
public class broken { public static void main(String[] args) { int i,folknum; char daInit,tchr; boolean isMale; double daAge; String da1stName,da2ndName; // some_folks[]; DataInput kbd = new DataInputStream(System.in); System.out.print("How many people do you want to enter into the database? "); try { folknum = Integer.parseInt(kbd.readLine()); } catch (IOException e) { System.out.println("Fatal IO Error..."); return; } Human some_folks[] = new Human(folknum); for (i = 0;i <= folknum;i++) { try { System.out.print("Enter first name for person #" + (i + 1) + ": "); da1stName = kbd.readLine(); System.out.print("Enter last name for person #" + (i + 1) + ": "); da2ndName = kbd.readLine(); System.out.print("Enter middle initial for person #" + (i + 1) + ": "); daInit = (kbd.readLine()).charAt(0); System.out.print("Enter age for person #" + (i + 1) + ": "); daAge = Double.parseDouble(kbd.readLine()); System.out.print("Is person #" + (i + 1) + " a male? (y/n) "); tchr = (kbd.readLine()).charAt(0); isMale = (tchr == 'y' || tchr == 'Y'); some_folks = new Human(da1stName,da2ndName,daInit,isMale); } catch (IOException e) { some_folks = new Human(); System.out.println("Argh, IO error!"); } } System.out.println("\nHere are the descriptions of the people you specified:\n"); for (i = 1;i < folknum;++i) some_folks.printStuffAboutMe(); } }
class Human { private String firstName; private String lastName; private char midInit; private double age; private boolean isMale; public Human(int folknum) { firstName = "John"; lastName = "Doe"; midInit = 'E'; age = 0; isMale = true; } public Human(String fname,String lname,char minit,double daAge,boolean maleq) { firstName = fname; lastName = lname; midInit = minit; age = daAge; isMale = maleq; } public void printStuffAboutMe() { System.out.println("Hi, my name is " + firstName + " " + midInit + ". " + lastName + "."); System.out.println("I am a " + age + " year-old " + ((isMale) ? "male" : "female") + "."); } }
hi, here is my answer for your question, hope it can help you.... Fay
import java.io.*;
public class broken { public static void main(String[] args) { int i,folknum; char daInit,tchr; boolean isMale; double daAge; String da1stName,da2ndName; // some_folks[]; DataInput kbd = new DataInputStream(System.in); System.out.print("How many people do you want to enter into the database? "); try { folknum = Integer.parseInt(kbd.readLine()); } catch (IOException e) { System.out.println("Fatal IO Error..."); return; } Human some_folks[] = new Human[folknum]; for (i = 0;i <= folknum;i++) { try { System.out.print("Enter first name for person #" + (i + 1) + ": "); da1stName = kbd.readLine(); System.out.print("Enter last name for person #" + (i + 1) + ": "); da2ndName = kbd.readLine(); System.out.print("Enter middle initial for person #" + (i + 1) + ": "); daInit = (kbd.readLine()).charAt(0); System.out.print("Enter age for person #" + (i + 1) + ": "); daAge = Double.parseDouble(kbd.readLine()); System.out.print("Is person #" + (i + 1) + " a male? (y/n) "); tchr = (kbd.readLine()).charAt(0); isMale = (tchr == 'y' || tchr == 'Y'); some_folks = new Human(da1stName,da2ndName,daInit,daAge,isMale); } catch (IOException e) { // some_folks = new Human(); System.out.println("Argh, IO error!"); } } System.out.println("\nHere are the descriptions of the people you specified:\n"); for (i = 1;i < folknum;++i) some_folks.printStuffAboutMe(); } }
class Human { private String firstName; private String lastName; private char midInit; private double age; private boolean isMale; public Human(int folknum) { firstName = "John"; lastName = "Doe"; midInit = 'E'; age = 0; isMale = true; } public Human(String fname,String lname,char minit,double daAge,boolean maleq) { firstName = fname; lastName = lname; midInit = minit; age = daAge; isMale = maleq; } public void printStuffAboutMe() { System.out.println("Hi, my name is " + firstName + " " + midInit + ". " + lastName + "."); System.out.println("I am a " + age + " year-old " + ((isMale) ? "male" : "female") + "."); } }