The Artima Developer Community
Sponsored Link

Java Answers Forum
Human database terminal program

1 reply on 1 page. Most recent reply: Dec 10, 2003 11:54 PM by Fay

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Absoluteloser

Posts: 2
Nickname: dirtyhuman
Registered: Nov, 2003

Human database terminal program Posted: Nov 30, 2003 4:25 PM
Reply to this message Reply
Advertisement
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") + ".");
}
}


Fay

Posts: 16
Nickname: flyfay
Registered: Sep, 2003

Re: Human database terminal program Posted: Dec 10, 2003 11:54 PM
Reply to this message Reply
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") + ".");
}
}

Flat View: This topic has 1 reply on 1 page
Topic: help me out friends Previous Topic   Next Topic Topic:

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use