Array problem for beginner

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 ***********
 
A

Andrew Thompson

I am studying Java and am an absolute beginner in programming.

People new to Java are best helped on a different group..
Following is the driver class and object class in full:

I encourage people to post compileable examples, but you could
do with some tips on how to prepare an example for others to see.
<http://www.physci.org/codes/sscce.jsp>

[ Note particularly the comments on line length and 'tabs'. ]

I've trimmed your code and put my version of same, with 'tweaks'..

It probably is still not right, but it should give you some
ideas on what you need to do to proceed. Read the comments
carefully.

<sscce>
//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));


/** This loop serves no purpose, your class already has logic
built in to perform the looping until the user enters '5'. */

//Loop the array
// for(int i = 0; i < secret.length; i ++)
// {

// simply declare and initialise 'i'
int i = 0;


do
{

option = Integer.parseInt(stdin.readLine());


//enter user info
if(option == 1)
{
// this is the only place we need to create a SecretClass
secret = new SecretClass();

System.out.print("Enter the description: ");
inDesc = stdin.readLine();
secret.setDescription(inDesc);


System.out.print("Enter the password: ");
inPassWord = stdin.readLine();
secret.setSecret(inPassWord);

// increment i!
i++;
}
//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)
{

for (int ii=0;ii<secret.length; ii++) {
if ( secret[ii]!=null ) {
System.out.println((ii+1) +". Description: "
+secret[ii].getDescription());
} else {
break;
}
}

}
// 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");
}



// method names should start with lowerCase()..
SecretClass.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
// and this can be a static method, so it does not
// require an Object of type Secret before it is invoked
public static 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 ***********
</sscce>

HTH
 
E

Efthimios

Andrew Thompson said:
I am studying Java and am an absolute beginner in programming.

People new to Java are best helped on a different group..
Following is the driver class and object class in full:

I encourage people to post compileable examples, but you could
do with some tips on how to prepare an example for others to see.
<http://www.physci.org/codes/sscce.jsp>

[ Note particularly the comments on line length and 'tabs'. ]

I've trimmed your code and put my version of same, with 'tweaks'..

It probably is still not right, but it should give you some
ideas on what you need to do to proceed. Read the comments
carefully.

<sscce>
//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));


/** This loop serves no purpose, your class already has logic
built in to perform the looping until the user enters '5'. */

//Loop the array
// for(int i = 0; i < secret.length; i ++)
// {

// simply declare and initialise 'i'
int i = 0;


do
{

option = Integer.parseInt(stdin.readLine());


//enter user info
if(option == 1)
{
// this is the only place we need to create a SecretClass
secret = new SecretClass();

System.out.print("Enter the description: ");
inDesc = stdin.readLine();
secret.setDescription(inDesc);


System.out.print("Enter the password: ");
inPassWord = stdin.readLine();
secret.setSecret(inPassWord);

// increment i!
i++;
}
//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)
{

for (int ii=0;ii<secret.length; ii++) {
if ( secret[ii]!=null ) {
System.out.println((ii+1) +". Description: "
+secret[ii].getDescription());
} else {
break;
}
}

}
// 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");
}



// method names should start with lowerCase()..
SecretClass.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
// and this can be a static method, so it does not
// require an Object of type Secret before it is invoked
public static 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 ***********
</sscce>

HTH

Thanks for that Andrew. You've given me some ideas to work on. The
program mostly works now, except for option 2 where it give a
NullPointer Exception. That's ok, I will work on it.
Cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top