-
Still can not call my Subclass method returns. J2EE
I am having problems returning and using my subclass method returns... I am trying to print out the results but am getting just the space allocation only. Any help will greatly appreciated.
Code:
package contacts;
import java.util.Scanner;
import java.util.ArrayList;
public class Contacts {
public static void main(String[] args)
{
System.out.println("By enetering B for Business or P for personal contact: ");
System.out.println("Are you a Business Contact or Personal Contact?: ");
//Storing the users input
Scanner in = new Scanner(System.in);
String cont = in.next();
if (cont.equals("P"))
{
System.out.println("Please enter: ");
}
else if (cont.equals("B"))
{
Business fno = new Business();
Business lno = new Business();
Business ado = new Business();
Business pno = new Business();
Business emo = new Business();
Business jto = new Business();
Business orgo = new Business();
fno.firstName();
lno.lastName();
ado.address();
pno.phoneNumber();
emo.eMail();
jto.jobTitle();
orgo.organization();
}
else
{
System.out.println("Please choose either B for Business Contact or "
+ "P as a Personal Contact as you picked an invalid entry.");
// return;
}
}
}
Code:
package contacts;
abstract public class ContactInfo
{
public abstract String firstName();
public abstract String lastName();
public abstract String address();
public abstract String phoneNumber();
public abstract String eMail();
}
Code:
package contacts;
import java.util.Scanner;
public class Business extends ContactInfo
{
Scanner in = new Scanner(System.in);
public String firstName()
{
System.out.println("Please enter your First Name: ");
String fName2 = in.next();
return fName2;
}
public String lastName()
{
System.out.println("Please enter your Last Name: ");
String lName2 = in.next();
return lName2;
}
public String address()
{
System.out.println("Please enter your Address: ");
String addre2 = in.next();
return addre2;
}
public String phoneNumber()
{
System.out.println("Please enter Phone Number: ");
String phoneNum2 = in.next();
return phoneNum2;
}
public String eMail()
{
System.out.println("Please enter your Email Address: ");
String eMailAdd2 = in.next();
return eMailAdd2;
}
public String jobTitle()
{
System.out.println("Please enter your job title: ");
String jobTit = in.next();
return jobTit;
}
public String organization()
{
System.out.println("Please enter your organization: ");
String organ = in.next();
return organ;
}
}
-
Re: Still can not call my Subclass method returns. J2EE
Please fix the code tags in your post. They use the square brackets: [] not <>
-
Re: Still can not call my Subclass method returns. J2EE
Sorry about that...... I guess I have done too much HTML...LOL
I have corrected and it looks MUCH better now.
-
Re: Still can not call my Subclass method returns. J2EE
What are the "results" that you want printed?
What does the program currently print out? Can you post the output and also show what you want printed?
-
Re: Still can not call my Subclass method returns. J2EE
What I as trying to do is test my Business by printing the results from creating the objects in the main class........ What I need to do, but am confused on how to..is create an arraylist that I can store the users inputed values... Say for example, teacher contact and a student contact... Not to sure how to accomplish using inheritance and or polymorhism..
-
Re: Still can not call my Subclass method returns. J2EE
create an arraylist that I can store the users inputed values
What would be stored in the ArrayList? If there are several pieces of data that need to be kept together, then you should define a class to hold those pieces of data. Then in the program after the data that goes together is collected, create an instance of the new class passing its constructor the data it is to save and then save that instance of the class in the ArrayList.
After all the data is collected, a loop could be used to go through the data saved in the ArrayList and to create the reports.
-
Re: Still can not call my Subclass method returns. J2EE
Can you give a real fast example to help point me down the right direction? I am a little confused.
-
Re: Still can not call my Subclass method returns. J2EE
I listed several steps, which step are you having problems with? Work on them one at a time.
-
Re: Still can not call my Subclass method returns. J2EE
Code:
//ArrayList<Employee> staff = new ArrayList<Employee>();
//staff.add(new Employee("Harry Hacker", . . .));
package contacts;
import java.util.ArrayList;
/**
*
* @author Drew1
*/
public class BusinessList
{
/**
private Business[] busList = new Business[100];
private int i = 0;
public void add(Business d)
{
if(i < busList.length)
{
busList[i] = d;
System.out.println("Business Contact added at index " + i);
i++;
}
* */
ArrayList<Business> busContact = new ArrayList<Business>();
//contacts.Business.busContact.add(new Business (Business.fName2, Business.lName2, Business.adddre2, Business.phoneNum2, Business.eMailAdd2, Business.jobTitle,Business.jobTit, Business.organ ));
//private int next1 = 0;
//Business bus1 = new Business();
// busContact.add(bus1);
/** public void add(Business d)
{
if (next1 < busContact.size())
{
busContact[next1] = d;
next1++;
}
}**/
ArrayList<Personal> perContact = new ArrayList<Personal>();
{
perContact.add("Drew");
}
}
I have commented out some stuff cause the errors, but am I getting on the track.
-
Re: Still can not call my Subclass method returns. J2EE
What is that code supposed to do?
When you get errors, don't ignore them, fix them. If you need help, copy the full text and paste it here.
-
Re: Still can not call my Subclass method returns. J2EE
As soon as I can, I will rerun the code in the complier and give you the error..
The code is supposed ask which kind of contact is, then prompt the the user to enter the information. from there the user should be able to display all contact information based on a selected contact.
-
Re: Still can not call my Subclass method returns. J2EE
I have changed my code but I do not think that I am adding to the array.
Code:
if (cont.equals("P"))
{
System.out.println("Please enter your First Name");
String fName = in.next();
System.out.println("Please enter your Last Name");
String lName = in.next();
in.nextLine();
System.out.println("Please enter your Address");
String addre = in.nextLine();
System.out.println("Please enter Phone Number");
String phoneNum = in.next();
System.out.println("Please enter your Email Address");
String eMailAdd = in.next();
System.out.println("Please enter your Date of Birth (xx/xx/xxxx");
String dob = in.next();
System.out.println("Your contact info is: " + fName + ", " + lName + ", " + addre
+ ", " + phoneNum + ", " + eMailAdd + ", " + dob);
Code:
public abstract class ContactInfo
{
public abstract void firstName();
public abstract void lastName();
public abstract void address();
public abstract void phoneNumber();
public abstract void eMail();
//public abstract String jobTitle();
//public abstract String organization();
//public abstract String dateOfBirth();
}
Code:
package contacts;
public class Personal extends ContactInfo
{
public void firstName()
{
BusinessList.perContact.add(fName);// Error here that a non static variable can not reference a static variable
}
public void lastName()
{
//return lName;
}
public void address()
{
//return addre;
}
public void phoneNumber()
{
//return phoneNum;
}
public void eMail()
{
//return eMailAdd;
}
public void dateOfBirth()
{
//return dob;
}
}
Code:
package contacts;
import java.util.ArrayList;
public class BusinessList
{
ArrayList<Business> busContact = new ArrayList<Business>();
ArrayList<Personal> perContact = new ArrayList<Personal>();
{
perContact.add("Drew");// error here saying not a suitable method
}
}
-
Re: Still can not call my Subclass method returns. J2EE
perContact is takes Personal objects not Strings.
-
Re: Still can not call my Subclass method returns. J2EE
I have went to my Business Class (original idea) and tried to make the ArrayList work, but I still seems to be getting errors... I must be missing something????
Code:
package contacts;
import java.util.Scanner;
import java.util.ArrayList;
public class Contacts
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
//ArrayList<Business> busContact = new ArrayList<Business>();
// Asks the user if they are a business contact or personal contact
System.out.println("By enetering B for Business or P for personal contact: ");
System.out.println("Are you a Business Contact or Personal Contact?: ");
//Storing the users input
Scanner in = new Scanner(System.in);
String cont = in.next();
//System.out.print("You chose: "+ cont + " as your contact.");
//String fName;
if (cont.equals("P"))
{
System.out.println("Please enter your First Name");
String fName = in.next();
System.out.println("Please enter your Last Name");
String lName = in.next();
in.nextLine();
System.out.println("Please enter your Address");
String addre = in.nextLine();
System.out.println("Please enter Phone Number");
String phoneNum = in.next();
System.out.println("Please enter your Email Address");
String eMailAdd = in.next();
System.out.println("Please enter your Date of Birth (xx/xx/xxxx");
String dob = in.next();
System.out.println("Your contact info is: " + fName + ", " + lName + ", " + addre
+ ", " + phoneNum + ", " + eMailAdd + ", " + dob);
}
else if (cont.equals("B"))
{
ArrayList<Business> busContact = new ArrayList<Business>();
Business fno = new Business();
Business lno = new Business();
Business ado = new Business();
Business pno = new Business();
Business emo = new Business();
Business jto = new Business();
Business orgo = new Business();
fno.firstName();
lno.lastName();
ado.address();
pno.phoneNumber();
emo.eMail();
jto.jobTitle();
orgo.organization();
busContact.add(fno);
busContact.add(lno);
busContact.add(ado);
busContact.add(pno);
busContact.add(emo);
busContact.add(jto);
busContact.add(orgo);
disArrayList(busContact);
}
public static void disArrayList (ArrayList<Business> toBeDisp)
{
for (int i =0; i < toBeDisp.size(); i++)
{
System.out.println(toBeDisp.get(i));
}
}
Thanks.
-
Re: Still can not call my Subclass method returns. J2EE
Quote:
I still seems to be getting errors
Please copy the full text of the error messages and paste it here.
-
Re: Still can not call my Subclass method returns. J2EE
Hello Norm,
I have decided to start fresh as I was getting to jumbled on this..... I will include what I have so far as I don't know where to go from here. On my Business class that extends the InfoClass, there should be a return of fName but I cant get anything to return anything.... I will need to add it to an ArrayList of some kind until the user is all done... I will also have to recall all data either (either kind of contact).. Any direction will greatly be appreciated.
Code:
package contacts2;
//import java.util.ArrayList;
import java.util.Scanner;
import java.util.ArrayList;
/**
*
* @author Drew
*/
public class Contacts2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
//Instantiates the Scanner Class
Scanner in = new Scanner(System.in);
//Asks user which kinda of contact 1)Business or 2) Personal
System.out.println("Please enter B for a business contact or P for a personal contact: ");
String cont = in.next();
if(cont.equals("B"))
{
Business fno = new Business();
Business lno = new Business();
Business ado = new Business();
Business pno = new Business();
Business emo = new Business();
Business poso = new Business();
Business orgo = new Business();
fno.firstName();
}
// else
//{
// System.out.print("You chose else: ");
//}
}
}
Code:
package contacts2;
import java.util.Scanner;
import java.util.ArrayList;
/**
*
* @author Drew
*/
public class Business extends ContactInfo
{
Scanner in = new Scanner(System.in);
ArrayList<Business> busContact = new ArrayList<Business>();
public String firstName()
{
System.out.println("What is your Fisrt Name: ");
String fName = in.next();
//return fName;
//System.out.println("This is some txt!!" + fName);
return fName;
}
}
-
Re: Still can not call my Subclass method returns. J2EE
I cant get anything to return anything
Can you be more specific? What method is being called? What is it supposed to return? When the method is called, what does the caller do with what the method returns?
-
Re: Still can not call my Subclass method returns. J2EE
When the program runs...(for this example) should ask for first name and return the inputed data to the busContact arraylist..... It does not so now I am lost on how to get this in the arratlist.
-
Re: Still can not call my Subclass method returns. J2EE
how to get this in the arratlist.
Use the ArrayList class's add() method to put objects in the list.
-
Re: Still can not call my Subclass method returns. J2EE
I had it in my Business class but it could not locate it...
busContact.add(fName);
-
Re: Still can not call my Subclass method returns. J2EE
busContact is an ArrayList that holds Business objects, not Strings. You need to create a new Business object and add that to the arraylist.
-
Re: Still can not call my Subclass method returns. J2EE
Quote:
run:
Please enter B for a business contact or P for a personal contact:
B
What is your Fisrt Name:
Drew
BUILD SUCCESSFUL (total time: 8 seconds)
I do not get an error when I run the code,.... I just can't get the name Drew to add to the ArrayList.....I am not too sure where to put
Quote:
busContact.add(fno);
If I put in main class, I am told that it could not find "busContact"......when put in the Business Class, I am told that the complier can not find the fno object.
-
Re: Still can not call my Subclass method returns. J2EE
Where is there a place in the code where both variables are in scope?
Are either of the variables defined in the wrong place?
Could a method be added that could be called with the value to be added to the arraylist?
-
Re: Still can not call my Subclass method returns. J2EE
I am lost on your first sentence......
I am sure that I have an issue somewhere, but I have no idea where... That is what I have trying to do...... In the Business class, it receives an input for the First Name..... From there, I can not find an acceptable solution to put the name...
What I am trying to accomplish is is make a contact that stores the First Name, Last Name, Address, Email, etc in an arrayList. I can get it to the point where it asks and it receives the data, but from there... I can not get anything to work... Once the user enters the in the from the last question the program ends.
-
Re: Still can not call my Subclass method returns. J2EE
The arraylist of Business objects should be somewhere that it is accessible to whatever classes and methods need to have access.
Somewhere the code should gather the data needed to build a Business object, then create the Business object and add it to the arraylist.
Later some methods will need access to the arraylist to build reports etc using the data in the Business objects stored in the arraylist.
-
Re: Still can not call my Subclass method returns. J2EE
Would it make sense for me to make a class that is designed to make the arraylist from the objects that the Business class creates?
Kinda like the main called the business class to ask questions and the List Class will store the input into an array?
-
Re: Still can not call my Subclass method returns. J2EE
The Main class could contain the arraylist of Business objects.
Somewhere there could be a method that gets the data to go in the Business class object, create the Business class object and store that object in the arraylist.
How is the contents of the arraylist supposed to be used after it has been filled with Business class objects?
-
Re: Still can not call my Subclass method returns. J2EE
I will need to call, for example, all the business contacts.... It should print out all First Names, Last Names, Addresses, etc..
Quote:
{Bob, Sims, 123 Test Dr, etc}
{Karen, Jones, 115 South St, etc}
-
Re: Still can not call my Subclass method returns. J2EE
How does the program get the data that goes into a Business class object?
When does the program create a Business class object using that data?
When does the program put that newly created Business object into the arraylist?
-
Re: Still can not call my Subclass method returns. J2EE
The main class looks builds the objects of business class, from there it passes to the business class to ask the questions..... From there is where I wanted to build the ArrayList.....
-
Re: Still can not call my Subclass method returns. J2EE
The arraylist that holds Business class objects should not be inside of the Business class. The arraylist needs to be in a class where the methods that create Business class objects can get to it so that the Business class objects can be saved in the arraylist.
-
Re: Still can not call my Subclass method returns. J2EE
Hello Norm... I have most of the code know work, thanks for all the guidance...
I do have 2 small issues still:
1) When I select the personal contact to enter, I can't go back and enter another business
2) If I select to see just 1 contact, I will see both..
Can you look and point me to where the issue might lay.
Code:
package contacts2;
//import java.util.ArrayList;
import java.util.Scanner;
//import java.util.ArrayList;
import java.io.File;
//import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.*;
//import java.io.*;
//import java.util.List;
import java.io.BufferedReader;
import java.io.FileReader;
/**
*
* @author Drew
*/
public class Contacts2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
//Instantiates the Scanner Class
Scanner in = new Scanner(System.in);
// readFile r = new readFile();
System.out.println("Please enter:\n 1 to add a business contact\n 2 to add a personal conatact"
+ "\n 3 to display the contacts \n 4 to Quit " );
int enter = in.nextInt();
final Formatter x;
final Formatter y;
try
{
x = new Formatter ("business.txt");
y = new Formatter("personal.txt");
//System.out.println("You created a file"); Used as a test
}
catch(Exception e)
{
System.out.println("you have an error");
}
// ArrayList<String> busContact = new ArrayList<String>();
// String[] busContact = new String[7];
// ArrayList<String> perContact = new ArrayList<String>();
// File businessCon = new File("C:\\java\\business.txt");
while (enter == 1)
{
//makes a new Business object
ContactInfo bCont = new Business();
//creates a txt file to store data of the business object
String filename = "business.txt";
//writes to the new business.txt file
try {
PrintWriter outputStream = new PrintWriter(new FileWriter(filename, true));
//identifies as hte "Business Contact"
outputStream.print("Business: ");
outputStream.print(bCont.firstName()+" ");
System.out.println();
outputStream.print(bCont.lastName() + " ");
System.out.println();
outputStream.print(bCont.address()+ " ");
System.out.println();
outputStream.print(bCont.email() + " ");
System.out.println();
outputStream.print(bCont.phoneNumber() +" ");
System.out.println();
outputStream.print(bCont.jobTitle() + " ");
System.out.println();
outputStream.print(bCont.org() + " ");
outputStream.println();
//Close the file stream
outputStream.close();
//decision to re-enter the loop
}
catch (IOException ex) {
Logger.getLogger(Contacts2.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Please enter:\n 1 to add a business contact\n 2 to add a personal conatact"
+ "\n 3 to display the contacts \n 4 to Quit " );
int newEnter = in.nextInt();
enter = newEnter;
}
//Personal Contact
while (enter == 2)
{
//creates a new Personal object
ContactInfo pCont = new Personal();
//freates a new txt file called "personal"
String filename = "personal.txt";
//wtites to the new personal.txt file
try {
PrintWriter outputStream = new PrintWriter(new FileWriter(filename, true));
outputStream.print("Personal: ");
outputStream.print(pCont.firstName() + ", ");
System.out.println();
outputStream.print(pCont.lastName() + ", ");
System.out.println();
outputStream.print(pCont.address() + ", ");
System.out.println();
outputStream.print(pCont.phoneNumber() + ", ");
System.out.println();
outputStream.print(pCont.email() + ", ");
System.out.println();
outputStream.print(pCont.dob());
outputStream.println();
outputStream.close();
}
catch (IOException ex) {
Logger.getLogger(Contacts2.class.getName()).log(Level.SEVERE, null, ex);
}
// outputStream.println();
System.out.println("Please enter:\n 1 to add a business contact\n 2 to add a personal conatact"
+ "\n 3 to display the contacts \n 4 to Quit " );
int newEnter = in.nextInt();
enter = newEnter;
}
while(enter == 3)
{
System.out.println("Press 1 to show the Business Cotact\n"
+ "Press 2 to show the Personal Conatct\n"
+ "Press 3 to show both contacts.");
int showCont = in.nextInt();
final int newShow =4;
if(showCont == 1){
try {
File file = new File("business.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");}
enter = showCont;
}
else if(showCont == 2 )
{
try {
File file = new File("personal.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");
}enter = newShow;
}
else
{
try {
File file = new File("business.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");}
//enter = newShow;
}
try {
File file = new File("personal.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");
}
enter = newShow;
} // while(enter == 3);
if(enter == 4)
{
System.out.println("You have chose to exit the program. ");
}
}
}
I can show the out put if needed.
-
Re: Still can not call my Subclass method returns. J2EE
There should be only one loop enclosing the asking of user choice and the handling of his choice, not a loop for handling each choice.
-
Re: Still can not call my Subclass method returns. J2EE
Thanks for the advice on the adding of contact.... I am still having a problem showing my contacts.... When I select to show just the business contacts... the business and personal show up, when I select to show just the personal I have a double showing of personal....The selection to show both contacts works as described.
Code:
package contacts2;
//import java.util.ArrayList;
import java.util.Scanner;
//import java.util.ArrayList;
import java.io.File;
//import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.*;
//import java.io.*;
//import java.util.List;
import java.io.BufferedReader;
import java.io.FileReader;
/**
*
* @author Drew
*/
public class Contacts2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
//Instantiates the Scanner Class
Scanner in = new Scanner(System.in);
// readFile r = new readFile();
System.out.println("Please enter:\n 1 to add a business contact\n 2 to add a personal conatact"
+ "\n 3 to display the contacts \n 4 to Quit " );
int enter = in.nextInt();
final Formatter x;
final Formatter y;
try
{
x = new Formatter ("business.txt");
y = new Formatter("personal.txt");
//System.out.println("You created a file"); Used as a test
}
catch(Exception e)
{
System.out.println("you have an error");
}
// ArrayList<String> busContact = new ArrayList<String>();
// String[] busContact = new String[7];
// ArrayList<String> perContact = new ArrayList<String>();
// File businessCon = new File("C:\\java\\business.txt");
final int returnEnter;
while (enter == 1 || enter == 2 )//|| enter == 3 || enter == 4)
{
if (enter == 1)
{
//makes a new Business object
ContactInfo bCont = new Business();
//creates a txt file to store data of the business object
String filename = "business.txt";
//writes to the new business.txt file
try {
PrintWriter outputStream = new PrintWriter(new FileWriter(filename, true));
//identifies as hte "Business Contact"
outputStream.print("Business: ");
outputStream.print(bCont.firstName()+" ");
System.out.println();
outputStream.print(bCont.lastName() + " ");
System.out.println();
outputStream.print(bCont.address()+ " ");
System.out.println();
outputStream.print(bCont.email() + " ");
System.out.println();
outputStream.print(bCont.phoneNumber() +" ");
System.out.println();
outputStream.print(bCont.jobTitle() + " ");
System.out.println();
outputStream.print(bCont.org() + " ");
outputStream.println();
//Close the file stream
outputStream.close();
//decision to re-enter the loop
}
catch (IOException ex) {
Logger.getLogger(Contacts2.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Please enter:\n 1 to add a business contact\n 2 to add a personal conatact"
+ "\n 3 to display the contacts \n 4 to Quit " );
int newEnter = in.nextInt();
enter = newEnter;
}
//Personal Contact
else //if(enter == 2)
{
//creates a new Personal object
ContactInfo pCont = new Personal();
//freates a new txt file called "personal"
String filename = "personal.txt";
//wtites to the new personal.txt file
try {
PrintWriter outputStream = new PrintWriter(new FileWriter(filename, true));
outputStream.print("Personal: ");
outputStream.print(pCont.firstName() + ", ");
System.out.println();
outputStream.print(pCont.lastName() + ", ");
System.out.println();
outputStream.print(pCont.address() + ", ");
System.out.println();
outputStream.print(pCont.phoneNumber() + ", ");
System.out.println();
outputStream.print(pCont.email() + ", ");
System.out.println();
outputStream.print(pCont.dob());
outputStream.println();
outputStream.close();
}
catch (IOException ex) {
Logger.getLogger(Contacts2.class.getName()).log(Level.SEVERE, null, ex);
}
// outputStream.println();
System.out.println("Please enter:\n 1 to add a business contact\n 2 to add a personal conatact"
+ "\n 3 to display the contacts \n 4 to Quit " );
int newEnter = in.nextInt();
enter = newEnter;
}
//else if(enter == 3)
if(enter == 3)
//while(enter == 3)
{
System.out.println("Press 1 to show the Business Cotact\n"
+ "Press 2 to show the Personal Conatct\n"
+ "Press 3 to show both contacts.");
int showCont = in.nextInt();
//final int newShow = 4;
//while(showCont == 1){
//while(showCont ==1){
if(showCont == 1 && showCont != 2 && showCont != 3)
{
try {
File file = new File("business.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");}
//showCont = newShow;
enter = 4;
}
//else if(showCont == 2 )
//while(showCont == 2)
else if(showCont == 2)
{
try {
File file = new File("personal.txt");
FileReader fileReader2 = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader2);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader2.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");
}//showCont = newShow;
enter = 4;
}
//while (showCont == 3)
//if(showCont == 3)
else
{
try {
File file = new File("business.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");}
//showCont = newShow;
}
try {
File file = new File("personal.txt");
FileReader fileReader2 = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader2);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader2.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");
}
//showCont = newShow;
enter = 4;
} //while(enter == 3);
//}
//else //(enter ==4)//else
}
if(enter == 4)
{
System.out.println("The program has exited... Have a nice day. ");
}
while (enter !=1 && enter != 2 && enter != 3 && enter != 4)
{
System.out.println("You have inputed an invaled entry, please re-enter.");
System.out.println("Please enter:\n 1 to add a business contact\n 2 to add a personal conatact"
+ "\n 3 to display the contacts \n 4 to Quit " );
int newEnter = in.nextInt();
enter = newEnter;
}
//enter = newEnter;
}
}
-
Re: Still can not call my Subclass method returns. J2EE
Something like this:
begin loop
ask question
get response
do what was requested << this would be handled by switch or chain of if/else if statements
end loop
-
Re: Still can not call my Subclass method returns. J2EE
I started off with a "while(enter = 3)" and carried to if/else statements, but I still get the same result... Am I missing something vital from your last response.
Code:
while(enter == 3)
{
System.out.println("Press 1 to show the Business Cotact\n"
+ "Press 2 to show the Personal Conatct\n"
+ "Press 3 to show both contacts.");
int showCont = in.nextInt();
//final int newShow = 4;
//while(showCont == 1){
//while(showCont ==1){
if(showCont == 1 )
{
try {
File file = new File("business.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");}
//showCont = newShow;
enter = 4;
}
//else if(showCont == 2 )
//while(showCont == 2)
else if(showCont == 2)
{
try {
File file = new File("personal.txt");
FileReader fileReader2 = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader2);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader2.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");
}//showCont = newShow;
enter = 4;
}
//while (showCont == 3)
//if(showCont == 3)
else
{
try {
File file = new File("business.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");}
//showCont = newShow;
}
try {
File file = new File("personal.txt");
FileReader fileReader2 = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader2);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader2.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");
}
//showCont = newShow;
enter = 4;
}
-
Re: Still can not call my Subclass method returns. J2EE
My idea was to have ONE while loop. That was what I meant when I said:
begin loop
that was THE ONE while loop
-
Re: Still can not call my Subclass method returns. J2EE
Now I get a continues loop that wont exit after I select which contact that I want to see goes straight to (enter == 4).... I now don't get my contacts, any of them.
Code:
package contacts2;
//import java.util.ArrayList;
import java.util.Scanner;
//import java.util.ArrayList;
import java.io.File;
//import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.*;
//import java.io.*;
//import java.util.List;
import java.io.BufferedReader;
import java.io.FileReader;
/**
*
* @author Drew
*/
public class Contacts2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
//Instantiates the Scanner Class
Scanner in = new Scanner(System.in);
// readFile r = new readFile();
System.out.println("Please enter:\n 1 to add a business contact\n 2 to add a personal conatact"
+ "\n 3 to display the contacts \n 4 to Quit " );
int enter = in.nextInt();
final Formatter x;
final Formatter y;
try
{
x = new Formatter ("business.txt");
y = new Formatter("personal.txt");
//System.out.println("You created a file"); Used as a test
}
catch(Exception e)
{
System.out.println("you have an error");
}
// ArrayList<String> busContact = new ArrayList<String>();
// String[] busContact = new String[7];
// ArrayList<String> perContact = new ArrayList<String>();
// File businessCon = new File("C:\\java\\business.txt");
final int returnEnter;
while (enter == 1 || enter == 2 || enter == 3 || enter == 4)
{
if (enter == 1)
{
//makes a new Business object
ContactInfo bCont = new Business();
//creates a txt file to store data of the business object
String filename = "business.txt";
//writes to the new business.txt file
try {
PrintWriter outputStream = new PrintWriter(new FileWriter(filename, true));
//identifies as hte "Business Contact"
outputStream.print("Business: ");
outputStream.print(bCont.firstName()+" ");
System.out.println();
outputStream.print(bCont.lastName() + " ");
System.out.println();
outputStream.print(bCont.address()+ " ");
System.out.println();
outputStream.print(bCont.email() + " ");
System.out.println();
outputStream.print(bCont.phoneNumber() +" ");
System.out.println();
outputStream.print(bCont.jobTitle() + " ");
System.out.println();
outputStream.print(bCont.org() + " ");
outputStream.println();
//Close the file stream
outputStream.close();
//decision to re-enter the loop
}
catch (IOException ex) {
Logger.getLogger(Contacts2.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Please enter:\n 1 to add a business contact\n 2 to add a personal conatact"
+ "\n 3 to display the contacts \n 4 to Quit " );
int newEnter = in.nextInt();
enter = newEnter;
}
//Personal Contact
else if(enter == 2)
{
//creates a new Personal object
ContactInfo pCont = new Personal();
//freates a new txt file called "personal"
String filename = "personal.txt";
//wtites to the new personal.txt file
try {
PrintWriter outputStream = new PrintWriter(new FileWriter(filename, true));
outputStream.print("Personal: ");
outputStream.print(pCont.firstName() + ", ");
System.out.println();
outputStream.print(pCont.lastName() + ", ");
System.out.println();
outputStream.print(pCont.address() + ", ");
System.out.println();
outputStream.print(pCont.phoneNumber() + ", ");
System.out.println();
outputStream.print(pCont.email() + ", ");
System.out.println();
outputStream.print(pCont.dob());
outputStream.println();
outputStream.close();
}
catch (IOException ex) {
Logger.getLogger(Contacts2.class.getName()).log(Level.SEVERE, null, ex);
}
// outputStream.println();
System.out.println("Please enter:\n 1 to add a business contact\n 2 to add a personal conatact"
+ "\n 3 to display the contacts \n 4 to Quit " );
int newEnter = in.nextInt();
enter = newEnter;
}
else if(enter == 3)
//if(enter == 3)
//while(enter == 3)
{
System.out.println("Press 1 to show the Business Cotact\n"
+ "Press 2 to show the Personal Conatct\n"
+ "Press 3 to show both contacts.");
int showCont = in.nextInt();
//final int newShow = 4;
//while(showCont == 1){
//while(showCont ==1){
if(showCont == 1 )
{
try {
File file = new File("business.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");}
//showCont = newShow;
enter = 4;
}
//else if(showCont == 2 )
//while(showCont == 2)
else if(showCont == 2)
{
try {
File file = new File("personal.txt");
FileReader fileReader2 = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader2);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader2.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");
}//showCont = newShow;
enter = 4;
}
//while (showCont == 3)
//if(showCont == 3)
else
{
try {
File file = new File("business.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");}
//showCont = newShow;
}
try {
File file = new File("personal.txt");
FileReader fileReader2 = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader2);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader2.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");
}
//showCont = newShow;
enter = 4;
} //while(enter == 3);
//}
//else //(enter ==4)//else
//}
else if (enter == 4)
{
System.out.println("The program has exited... Have a nice day. ");
}
//while (enter !=1 && enter != 2 && enter != 3 && enter != 4)
else
{
System.out.println("You have inputed an invaled entry, please re-enter.");
System.out.println("Please enter:\n 1 to add a business contact\n 2 to add a personal conatact"
+ "\n 3 to display the contacts \n 4 to Quit " );
int newEnter = in.nextInt();
enter = newEnter;
}
//enter = newEnter;
}
}
}
-
Re: Still can not call my Subclass method returns. J2EE
First thing I see is that the asking of the question and the reading of the response are before the while loop not INSIDE the loop.
The next thing is that the question is asked in many places NOT ONCE at the beginning of the loop.
See my post#35
-
Re: Still can not call my Subclass method returns. J2EE
Some how I have made it worse....... :(
Code:
package contacts2;
//import java.util.ArrayList;
import java.util.Scanner;
//import java.util.ArrayList;
import java.io.File;
//import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.*;
//import java.io.*;
//import java.util.List;
import java.io.BufferedReader;
import java.io.FileReader;
/**
*
* @author Drew
*/
public class Contacts2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
//Instantiates the Scanner Class
Scanner in = new Scanner(System.in);
// readFile r = new readFile();
System.out.println("Please enter:\n 1 to add a business contact\n 2 to add a personal conatact"
+ "\n 3 to display the contacts \n 4 to Quit " );
int enter = in.nextInt();
final Formatter x;
final Formatter y;
try
{
x = new Formatter ("business.txt");
y = new Formatter("personal.txt");
//System.out.println("You created a file"); Used as a test
}
catch(Exception e)
{
System.out.println("you have an error");
}
// ArrayList<String> busContact = new ArrayList<String>();
// String[] busContact = new String[7];
// ArrayList<String> perContact = new ArrayList<String>();
// File businessCon = new File("C:\\java\\business.txt");
final int returnEnter;
while (enter == 1 || enter == 2 || enter == 3 || enter == 4)
{
if (enter == 1)
{
//makes a new Business object
ContactInfo bCont = new Business();
//creates a txt file to store data of the business object
String filename = "business.txt";
//writes to the new business.txt file
try {
PrintWriter outputStream = new PrintWriter(new FileWriter(filename, true));
//identifies as hte "Business Contact"
outputStream.print("Business: ");
outputStream.print(bCont.firstName()+" ");
System.out.println();
outputStream.print(bCont.lastName() + " ");
System.out.println();
outputStream.print(bCont.address()+ " ");
System.out.println();
outputStream.print(bCont.email() + " ");
System.out.println();
outputStream.print(bCont.phoneNumber() +" ");
System.out.println();
outputStream.print(bCont.jobTitle() + " ");
System.out.println();
outputStream.print(bCont.org() + " ");
outputStream.println();
//Close the file stream
outputStream.close();
//decision to re-enter the loop
}
catch (IOException ex) {
Logger.getLogger(Contacts2.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Please enter:\n 1 to add a business contact\n 2 to add a personal conatact"
+ "\n 3 to display the contacts \n 4 to Quit " );
int newEnter = in.nextInt();
enter = newEnter;
}
//Personal Contact
else if(enter == 2)
{
//creates a new Personal object
ContactInfo pCont = new Personal();
//freates a new txt file called "personal"
String filename = "personal.txt";
//wtites to the new personal.txt file
try {
PrintWriter outputStream = new PrintWriter(new FileWriter(filename, true));
outputStream.print("Personal: ");
outputStream.print(pCont.firstName() + ", ");
System.out.println();
outputStream.print(pCont.lastName() + ", ");
System.out.println();
outputStream.print(pCont.address() + ", ");
System.out.println();
outputStream.print(pCont.phoneNumber() + ", ");
System.out.println();
outputStream.print(pCont.email() + ", ");
System.out.println();
outputStream.print(pCont.dob());
outputStream.println();
outputStream.close();
}
catch (IOException ex) {
Logger.getLogger(Contacts2.class.getName()).log(Level.SEVERE, null, ex);
}
// outputStream.println();
System.out.println("Please enter:\n 1 to add a business contact\n 2 to add a personal conatact"
+ "\n 3 to display the contacts \n 4 to Quit " );
int newEnter = in.nextInt();
enter = newEnter;
}
System.out.println("Press 1 to show the Business Cotact\n"
+ "Press 2 to show the Personal Conatct\n"
+ "Press 3 to show both contacts.");
int showCont = in.nextInt();
if(enter == 3)
//if(enter == 3)
//while(enter == 3)
{
//final int newShow = 4;
//while(showCont == 1){
//while(showCont ==1){
if(showCont == 1 )
{
try {
File file = new File("business.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");}
//showCont = newShow;
enter = 4;
}
enter =4;
//else if(showCont == 2 )
//while(showCont == 2)
if(showCont == 2)
{
try {
File file = new File("personal.txt");
FileReader fileReader2 = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader2);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader2.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");
}//showCont = newShow;
enter = 4;
}
enter = 4;
//while (showCont == 3)
//if(showCont == 3)
if(showCont ==3)
{
try {
File file = new File("business.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");}
//showCont = newShow;
}
try {
File file = new File("personal.txt");
FileReader fileReader2 = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader2);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader2.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");
}
//showCont = newShow;
enter = 4;
} //while(enter == 3);
//}
//else //(enter ==4)//else
//}
else if (enter == 4)
{
System.out.println("The program has exited... Have a nice day. ");
}
//while (enter !=1 && enter != 2 && enter != 3 && enter != 4)
else
{
System.out.println("You have inputed an invaled entry, please re-enter.");
System.out.println("Please enter:\n 1 to add a business contact\n 2 to add a personal conatact"
+ "\n 3 to display the contacts \n 4 to Quit " );
int newEnter = in.nextInt();
enter = newEnter;
}
//enter = newEnter;
}
}
}
-
Re: Still can not call my Subclass method returns. J2EE
Did you miss my last post? I still see the question and answer OUTSIDE the loop.
And the question/answer groups are in more than one place instead of in ONE PLACE at the top of the loop. See post#35
-
Re: Still can not call my Subclass method returns. J2EE
I might be new, but would I want to ask which contact to show at the same time I ask which contact they would like to enter? If I am understanding you correctly, I should put this code:
Code:
System.out.println("Press 1 to show the Business Cotact\n"
+ "Press 2 to show the Personal Conatct\n"
+ "Press 3 to show both contacts.");
int showCont = in.nextInt();
above the while loop found at the top..... will this not actually ask for this information before I enter in any information into my txt files?
-
Re: Still can not call my Subclass method returns. J2EE
Are you asking about:
Where the question should be asked
vs what question to ask?
What question to ask and what responses to allow depend on the state of the data. Boolean variables can be used to keep track of the state of the data.
I'd rather ask the question and get the response in one place instead of many places.
-
Re: Still can not call my Subclass method returns. J2EE
I just don't want to ask which contacts you want to be displayed before you enter in any contacts...
Order of Operation:
1) ask user to enter in contacts, which can be in any order and switch back and forth between personal and business
2) ask user which (if any) contact they want displayed (personal, business, or both)
3) display contacts in required
-
Re: Still can not call my Subclass method returns. J2EE
Ok what is the problem coding that?
Put 1) inside a loop that continues until ALL contracts are entered
Then do 2)
-
Re: Still can not call my Subclass method returns. J2EE
I guess the correct question is: How do I put the #2 above my while loop and only ask for it when ready?.... I thought I was already doing that.
-
Re: Still can not call my Subclass method returns. J2EE
Post#45 suggested there be two loops:
1) to get the data
2) to display the data
-
Re: Still can not call my Subclass method returns. J2EE
I have implemented 2 while loops and using my if conditionals inside the loops.... I still am having the problems with showing my contacts.... I get both when I am supposed to show business, I get duplicate when I am supposed to show personal, and both works fine.
Code:
package contacts2;
//import java.util.ArrayList;
import java.util.Scanner;
//import java.util.ArrayList;
import java.io.File;
//import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.*;
//import java.io.*;
//import java.util.List;
import java.io.BufferedReader;
import java.io.FileReader;
/**
*
* @author Drew
*/
public class Contacts2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
//Instantiates the Scanner Class
Scanner in = new Scanner(System.in);
// readFile r = new readFile();
System.out.println("Please enter:\n 1 to add a business contact\n 2 to add a personal conatact"
+ "\n 3 to display the contacts \n 4 to Quit " );
int enter = in.nextInt();
final Formatter x;
final Formatter y;
try
{
x = new Formatter ("business.txt");
y = new Formatter("personal.txt");
//System.out.println("You created a file"); Used as a test
}
catch(Exception e)
{
System.out.println("you have an error");
}
final int returnEnter;
while (enter == 1 || enter == 2 )//| enter == 3 || enter == 4)
{
if (enter == 1)
{
//makes a new Business object
ContactInfo bCont = new Business();
//creates a txt file to store data of the business object
String filename = "business.txt";
//writes to the new business.txt file
try {
PrintWriter outputStream = new PrintWriter(new FileWriter(filename, true));
//identifies as hte "Business Contact"
outputStream.print("Business: ");
outputStream.print(bCont.firstName()+" ");
System.out.println();
outputStream.print(bCont.lastName() + " ");
System.out.println();
outputStream.print(bCont.address()+ " ");
System.out.println();
outputStream.print(bCont.email() + " ");
System.out.println();
outputStream.print(bCont.phoneNumber() +" ");
System.out.println();
outputStream.print(bCont.jobTitle() + " ");
System.out.println();
outputStream.print(bCont.org() + " ");
outputStream.println();
//Close the file stream
outputStream.close();
//decision to re-enter the loop
}
catch (IOException ex) {
Logger.getLogger(Contacts2.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Please enter:\n 1 to add a business contact\n 2 to add a personal conatact"
+ "\n 3 to display the contacts \n 4 to Quit " );
int newEnter = in.nextInt();
enter = newEnter;
}
//Personal Contact
else if(enter == 2)
{
//creates a new Personal object
ContactInfo pCont = new Personal();
//freates a new txt file called "personal"
String filename = "personal.txt";
//wtites to the new personal.txt file
try {
PrintWriter outputStream = new PrintWriter(new FileWriter(filename, true));
outputStream.print("Personal: ");
outputStream.print(pCont.firstName() + ", ");
System.out.println();
outputStream.print(pCont.lastName() + ", ");
System.out.println();
outputStream.print(pCont.address() + ", ");
System.out.println();
outputStream.print(pCont.phoneNumber() + ", ");
System.out.println();
outputStream.print(pCont.email() + ", ");
System.out.println();
outputStream.print(pCont.dob());
outputStream.println();
outputStream.close();
}
catch (IOException ex) {
Logger.getLogger(Contacts2.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Please enter:\n 1 to add a business contact\n 2 to add a personal conatact"
+ "\n 3 to display the contacts \n 4 to Quit " );
int newEnter = in.nextInt();
enter = newEnter;
}
}
if(enter == 3){
System.out.println("Press 1 to show the Business Cotact\n"
+ "Press 2 to show the Personal Conatct\n"
+ "Press 3 to show both contacts.");
int showCont = in.nextInt();
while(enter == 3)
{
//final int newShow = 4;
//while(showCont == 1){
//while(showCont ==1)//{
if(showCont == 1 )
{
try {
File file = new File("business.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");}
//showCont = newShow;
enter = 4;
}
//enter =4;
//else if(showCont == 2 )
else if(showCont == 2)
// else if(showCont == 2)
{
try {
File file = new File("personal.txt");
FileReader fileReader2 = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader2);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader2.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");
}//showCont = newShow;
enter = 4;
}
//enter = 4;
//while (showCont == 3)
//if(showCont == 3)
else //if(showCont ==3)
{
try {
File file = new File("business.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");}
//showCont = newShow;
}
try {
File file = new File("personal.txt");
FileReader fileReader2 = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader2);
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader2.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
System.out.println("You have an error that is not priting!!!!");
}
//showCont = newShow;
enter = 4;
} //while(enter == 3);
}
//else //(enter ==4)//else
//}
//else
if (enter == 4)
{
System.out.println("The program has exited... Have a nice day. ");
}
//while (enter !=1 && enter != 2 && enter != 3 && enter != 4)
else
{
System.out.println("You have inputed an invaled entry, please re-enter.");
System.out.println("Please enter:\n 1 to add a business contact\n 2 to add a personal conatact"
+ "\n 3 to display the contacts \n 4 to Quit " );
int newEnter = in.nextInt();
enter = newEnter;
}
//enter = newEnter;
}
}
//}
-
Re: Still can not call my Subclass method returns. J2EE
What did you change?
The question is still OUTSIDE of the loop?
There are more questions inside of the loop.
I thought there were to be two loops:
// First loop to process input and save it
begin loop1
question about input
process input
end loop1
// Second loop to display input from above
begin loop2
question about display
do requested display
end loop2
-
Re: Still can not call my Subclass method returns. J2EE
Did I mention that my initial display has to give options
1) add business contacts
2) add personal contacts
3) show contacts --then lead to which contact
4) exit
So it should look like:
1) ask initial questioning (see above)
2) create loop through 1-2 till complete
3) upon choosing 3 (ask user which contact) ,loop through till desire selection is complete
4) display the correct contact
5) terminate program