-
March 6th, 2014, 10:13 AM
#31
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.
Norm
-
March 15th, 2014, 07:56 PM
#32
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.
-
March 15th, 2014, 08:04 PM
#33
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.
Norm
-
March 16th, 2014, 03:51 PM
#34
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;
}
}
-
March 16th, 2014, 03:57 PM
#35
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
Norm
-
March 16th, 2014, 04:19 PM
#36
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;
}
-
March 16th, 2014, 04:30 PM
#37
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
Norm
-
March 16th, 2014, 04:39 PM
#38
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;
}
}
}
-
March 16th, 2014, 04:41 PM
#39
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
Last edited by Norm; March 16th, 2014 at 04:44 PM.
Norm
-
March 16th, 2014, 04:50 PM
#40
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;
}
}
}
-
March 16th, 2014, 05:00 PM
#41
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
Norm
-
March 16th, 2014, 05:06 PM
#42
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?
-
March 16th, 2014, 05:13 PM
#43
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.
Norm
-
March 16th, 2014, 05:20 PM
#44
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
-
March 16th, 2014, 05:24 PM
#45
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)
Norm
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|