E
Efthimios
Hello all
I am studying Java and am an absolute beginner in programming. I have
a programming problem which I have almost completed but I cannot get
ot work properly. It is an array question. the program is to hold data
entered by the user then the user can ask the program to print on
screen the data entered.With my program, it only displays the last
data entered, even though I have instantiated an Array object of the
class. I tried to find what is missing but I cannot find the solution.
Any hints will be apreciated. Cheers.
Following is the driver class and object class in full:
//This program collects up to 10 descrptions and passwords.User can
// enter data ,which can be recalled by the program.
// Driver for SecretClass class which follows.
import java.io.*;
class Hard //Driver for SecretClass
{
public static void main(String [] args) throws IOException
{
//declare variables
String [] inDesc = new String[10];
String [] inPassWord = new String [10];
String [] checkPassWord = new String [10];
String [] newPassWord = new String [10];
int option = 0;
//create SecretClass object
SecretClass [] secret = new SecretClass[10];
//prompt for options
System.out.println("Keychain of Secrets");
System.out.println("=================");
System.out.println(" 1. Add a secret \n 2. Check a secret \n 3.
Display summary \n 4. Change a secret\n 5. Quit");
//create keyboard reader
BufferedReader stdin = new BufferedReader(new
InputStreamReader(System.in));
//Loop the array
for(int i = 0; i < secret.length; i ++)
{
secret = new SecretClass();
do
{
option = Integer.parseInt(stdin.readLine());
//enter user info
if(option == 1)
{
System.out.print("Enter the description: ");
inDesc = stdin.readLine();
secret.setDescription(inDesc);
System.out.print("Enter the password: ");
inPassWord = stdin.readLine();
secret.setSecret(inPassWord);
}
//option 2 check password
if(option == 2)
{
System.out.print("Secrets\n=======\n");
System.out.println((i+1) + ". Description: "
+secret.getDescription());
System.out.print("What is the password: ");
checkPassWord = stdin.readLine();
if(checkPassWord.equals(secret.getSecret()))
System.out.print("Correct\n\n");
else
System.out.print("Incorrect");
}
// option 3 check description
if (option == 3)
{
System.out.println((i+1) +". Description: "
+secret.getDescription());
}
// option 4 change password
if (option == 4)
{
System.out.println("Descriptions\n");
System.out.println("============");
System.out.println((i+1) + ".Description: " +
secret.getDescription());
System.out.print("What is the password for " +
secret.getDescription() + ": ");
checkPassWord = stdin.readLine();
if(checkPassWord.equals(secret.getSecret()))
System.out.print("Correct\n");
System.out.print("Enter the new password: ");
newPassWord = stdin.readLine();
secret.setSecret(newPassWord);
}
// invalid entries
if(option < 1 || option > 5)
{
System.out.println("Invalid option. Try again");
}
secret.Menu(); // repeat menu
}while(option !=5);
}
}
}
// **************************END OF HARD CLASS
********************************
//SecretWord class: contains description string and secret string
class SecretClass
{
//class variables
private String description;
private String secret;
//default constructor
SecretClass()
{
description = "none";
secret = "none";
}
//full constructor
SecretClass(String description, String secret)
{
this.description = description;
this.secret = secret;
}
//mutators
public void setDescription(String description)
{
this.description = description;
}
public void setSecret(String secret)
{
this.secret = secret;
}
//accesors
public String getDescription()
{
return description;
}
public String getSecret()
{
return secret;
}
// method for displaying menu
public void Menu()
{
System.out.println("\n");
System.out.println("Keychain of Secrets");
System.out.println("=================");
System.out.println(" 1. Add a secret \n 2. Check a secret \n 3.
Display summary \n 4. Change a secret\n 5. Quit");
System.out.print("Choose an option: ");
}
}
// ********End of the SecretClass ***********
I am studying Java and am an absolute beginner in programming. I have
a programming problem which I have almost completed but I cannot get
ot work properly. It is an array question. the program is to hold data
entered by the user then the user can ask the program to print on
screen the data entered.With my program, it only displays the last
data entered, even though I have instantiated an Array object of the
class. I tried to find what is missing but I cannot find the solution.
Any hints will be apreciated. Cheers.
Following is the driver class and object class in full:
//This program collects up to 10 descrptions and passwords.User can
// enter data ,which can be recalled by the program.
// Driver for SecretClass class which follows.
import java.io.*;
class Hard //Driver for SecretClass
{
public static void main(String [] args) throws IOException
{
//declare variables
String [] inDesc = new String[10];
String [] inPassWord = new String [10];
String [] checkPassWord = new String [10];
String [] newPassWord = new String [10];
int option = 0;
//create SecretClass object
SecretClass [] secret = new SecretClass[10];
//prompt for options
System.out.println("Keychain of Secrets");
System.out.println("=================");
System.out.println(" 1. Add a secret \n 2. Check a secret \n 3.
Display summary \n 4. Change a secret\n 5. Quit");
//create keyboard reader
BufferedReader stdin = new BufferedReader(new
InputStreamReader(System.in));
//Loop the array
for(int i = 0; i < secret.length; i ++)
{
secret = new SecretClass();
do
{
option = Integer.parseInt(stdin.readLine());
//enter user info
if(option == 1)
{
System.out.print("Enter the description: ");
inDesc = stdin.readLine();
secret.setDescription(inDesc);
System.out.print("Enter the password: ");
inPassWord = stdin.readLine();
secret.setSecret(inPassWord);
}
//option 2 check password
if(option == 2)
{
System.out.print("Secrets\n=======\n");
System.out.println((i+1) + ". Description: "
+secret.getDescription());
System.out.print("What is the password: ");
checkPassWord = stdin.readLine();
if(checkPassWord.equals(secret.getSecret()))
System.out.print("Correct\n\n");
else
System.out.print("Incorrect");
}
// option 3 check description
if (option == 3)
{
System.out.println((i+1) +". Description: "
+secret.getDescription());
}
// option 4 change password
if (option == 4)
{
System.out.println("Descriptions\n");
System.out.println("============");
System.out.println((i+1) + ".Description: " +
secret.getDescription());
System.out.print("What is the password for " +
secret.getDescription() + ": ");
checkPassWord = stdin.readLine();
if(checkPassWord.equals(secret.getSecret()))
System.out.print("Correct\n");
System.out.print("Enter the new password: ");
newPassWord = stdin.readLine();
secret.setSecret(newPassWord);
}
// invalid entries
if(option < 1 || option > 5)
{
System.out.println("Invalid option. Try again");
}
secret.Menu(); // repeat menu
}while(option !=5);
}
}
}
// **************************END OF HARD CLASS
********************************
//SecretWord class: contains description string and secret string
class SecretClass
{
//class variables
private String description;
private String secret;
//default constructor
SecretClass()
{
description = "none";
secret = "none";
}
//full constructor
SecretClass(String description, String secret)
{
this.description = description;
this.secret = secret;
}
//mutators
public void setDescription(String description)
{
this.description = description;
}
public void setSecret(String secret)
{
this.secret = secret;
}
//accesors
public String getDescription()
{
return description;
}
public String getSecret()
{
return secret;
}
// method for displaying menu
public void Menu()
{
System.out.println("\n");
System.out.println("Keychain of Secrets");
System.out.println("=================");
System.out.println(" 1. Add a secret \n 2. Check a secret \n 3.
Display summary \n 4. Change a secret\n 5. Quit");
System.out.print("Choose an option: ");
}
}
// ********End of the SecretClass ***********