-
March 16th, 2014, 05:45 PM
#46
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.
-
March 16th, 2014, 05:50 PM
#47
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
Norm
-
March 16th, 2014, 06:13 PM
#48
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;
}
}
//}
-
March 16th, 2014, 06:44 PM
#49
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
Last edited by Norm; March 16th, 2014 at 06:48 PM.
Norm
-
March 16th, 2014, 07:08 PM
#50
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
-
March 16th, 2014, 07:22 PM
#51
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.
Norm
-
March 16th, 2014, 07:35 PM
#52
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.
-
March 16th, 2014, 07:45 PM
#53
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.
Norm
-
March 16th, 2014, 08:04 PM
#54
Re: Still can not call my Subclass method returns. J2EE
That is what I have saying .... I still get issues with run-time.
-
March 16th, 2014, 08:08 PM
#55
Re: Still can not call my Subclass method returns. J2EE
You haven't changed the code as I suggested.
Norm
-
March 16th, 2014, 08:23 PM
#56
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}
-
March 16th, 2014, 08:29 PM
#57
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?
Norm
-
March 16th, 2014, 08:51 PM
#58
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;
}
-
March 16th, 2014, 08:56 PM
#59
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?
Norm
-
March 16th, 2014, 09:10 PM
#60
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}
//}
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
|