Re: Still can not call my Subclass method returns. J2EE
How can the initial display show option 3 if there are not any contacts entered yet?
Is the design shown in post#49 wrong?
Can there be an intermixing of adding contacts and showing contacts?
Or are they separated as shown? Do all of one BEFORE doing the next.
Re: Still can not call my Subclass method returns. J2EE
I still must be able to give the option to show the unpopulated file and produce it if so desired...... Stupid I know, but none the less it is what is required... It would have been a lot easier the other way around.....
Ask for contact input, when done ask which for which contact wanted to be shown.
IN the beginning of the main method, I create both txt files that are going to be written on..... That way I can call an empty file if needed.
Re: Still can not call my Subclass method returns. J2EE
Then back to one loop with the questions inside and at the top of the loop.
Re: Still can not call my Subclass method returns. J2EE
That is what I have saying .... I still get issues with run-time.
Re: Still can not call my Subclass method returns. J2EE
You haven't changed the code as I suggested.
Re: Still can not call my Subclass method returns. J2EE
As I run a single While loop at the beginning, I get a continues loop when I select which contact to view.....
Is this the basic layout?
***Ask initial question
While loop (1-4)
1)If add Business{some action}
2)else if add Personal{some action}
3)else if view contact{(Ask which contact to view)
While loop(1-3)
****1a)if view Business txt file
**** 2a)else if view Personal txt file
**** 3a)else view both txt files
else{quit}
While NOT 1-4{inform user not valid, and rerun from beginning}
Re: Still can not call my Subclass method returns. J2EE
Why are there 2 while loops?
How does the code exit the 2nd loop if the control variable's value isn't changed?
Re: Still can not call my Subclass method returns. J2EE
At the end of the code, I change the user's input to the exit #4
Example: "enter" is the variable I am using for 1-4 (with 4 being the exit) ....At the end of the code for 1a-3a, I put "enter = 4"; this exits and terminates the program.
Code:
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;
}
Re: Still can not call my Subclass method returns. J2EE
Why have a while loop if it will only execute once? Why not use an if statement?
Re: Still can not call my Subclass method returns. J2EE
I have removed all WHILE loops except at the beginning, now I have an infinite loop when I select to view my contact.
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){
else if (enter == 3 )//|| enter == 4)
// else 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();
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 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;
//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
else if (enter == 4)
{
System.out.println("The program has exited... Have a nice day. ");
}
// else
// }
else //(enter !=1 && enter == 2 | enter != 3 || enter != 4)
{
//do{
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;
}//while(enter !=1 || enter != 2 || enter != 3 || enter != 4);
}
//enter = newEnter;
}
}
//2}
//}
Re: Still can not call my Subclass method returns. J2EE
Just tells me the program has exited.
Re: Still can not call my Subclass method returns. J2EE
These lines are still outside of the loop:
Code:
// 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();
I'm done for tonight. Back tomorrow.
Re: Still can not call my Subclass method returns. J2EE
I hae the loop exited correctlt now by removing 4 form the loop..... Still same issue with showing the contacts.... :(
Re: Still can not call my Subclass method returns. J2EE
Could it be possible that I have an issue with my try blocks that read and import my txt files?
Re: Still can not call my Subclass method returns. J2EE
I don' think the try blocks would cause the problem that you describe.
Can you post a short sample session with a user? Something like this:
Ask the questions
user enters 1
do work for 1
Ask the questions
user enters 1
do work for 1
Ask the questions
user enters 1
do work for 1
Ask the questions
user enters 2
do work for 2
Ask the questions
user enters 3
do work for 3
Ask the questions
user enters 4
exit loop
Re: Still can not call my Subclass method returns. J2EE
Everything else works fine....... I can even open the txt file in notepad and everything is there... but the import and displaying it has the problem.
Re: Still can not call my Subclass method returns. J2EE
Is the problem with reading and displaying the contents of a file?
Re: Still can not call my Subclass method returns. J2EE
just displaying the txt file... I write to them correctly..... I can open files with notepad and see everything in there correctly.... But always displays 2 files instead of just one...
Look at previous replies........
Re: Still can not call my Subclass method returns. J2EE
Are you saying that there is no problems with reading and displaying the contents of a file?
The problem is with the program's logic: when a read and display request is made, it is done two times.
I asked for a sample user session in post#65. Can you make a list of the events in the order that they should happen when the program is executed? Not what is now happening, but what you want to happen.
Also post the current version of the code and a script for executing it for testing that will show the problem that you are describing.