Java 169 Error: One of my variable haven't been initialized + 3 other questions
Hi there,
I'm doing a Password Storage Application for my project and this is what I have to do:
Password Storage Application
- The id/password pairs along with a description are to be stored in an encrypted file.
- Access to the file is controlled with a master password which is set on first use and can later be changed.
- The user can add, remove (and modify) all the stored id/password pairs and descriptions.
This is my code.
Code:
import javax.swing.JOptionPane; //this allows me to use display dialogs
import java.util.Arrays; //this allows me to use the sort method later on
import java.io.*; //this allows me to use commands related to reading and writing data
public class ohpassword {
//MAIN PROGRAM START DECLARING AN IO EXCEPTION CAN BE THROWN(USED IN THE READ AND WRITE SECTIONS)
public static void main (String args[]) throws IOException {
//DECLARATION OF VARIABLES
int welcome_prompt_int;
int search_prompt_int;
int description_search_int;
int update_entry_prompt_int;
int id_selection_one_prompt_int;
int id_selection_two_prompt_int;
int id_selection_three_prompt_int;
int id_selection_four_prompt_int;
int id_selection_five_prompt_int;
int id_selection_six_prompt_int;
String allstudents;
String welcome;
String welcome_prompt;
String search;
String search_prompt;
String user_search;
String pass_search;
String description_search;
String user_search_prompt;
String pass_search_prompt;
String description_search_prompt;
String user_entry;
String pass_entry;
String description_entry;
String user_entry_prompt;
String pass_entry_prompt = null;
String description_entry_prompt;
String update_entry;
String update_entry_prompt;
String id_selection_one;
String id_selection_two;
String id_selection_three;
String id_selection_four;
String id_selection_five;
String id_selection_six;
String id_selection_one_prompt;
String id_selection_two_prompt;
String id_selection_three_prompt;
String id_selection_four_prompt;
String id_selection_five_prompt;
String id_selection_six_prompt;
String alphabet_initial;
String alphabet_final;
String alphabet_list;
String dot6 = " ";
String master_entry;
String master_entry_prompt;
//ARRAY FOR USERNAMES
String[] user = new String [6];
user[0] = "phoenix";
user[1] = "maya";
user[2] = "apollo";
user[3] = "pearl";
user[4] = "miles";
user[5] = " ";
//ARRAY FOR PASSWORDS
String[] pass = new String [6];
pass[0] = "enema123";
pass[1] = "magic123";
pass[2] = "cisco567";
pass[3] = "dromo357";
pass[4] = "coolman345";
pass[5] = " ";
//ARRAY FOR DESCRIPTIONS
String[] description = new String [6];
description[0] = "Phoenix Wright";
description[1] = "Maya Fey";
description[2] = "Apollo Justice";
description[3] = "Pearl Fey";
description[4] = "Miles Edgeworth";
description[5] = " ";
//ARRAY FOR MASTER PASSWORD
String[] master = new String [1];
master[0] = "default";
//ARRAY COMBINING ALL OTHER ARRAYS TO GIVE STUDENT DETAILS
String[] student = new String [6];
student[0] = user[0] + " " + pass[0] + " " + description[0]; //Phoenix Wright
student[1] = user[1] + " " + pass[1] + " " + description[1]; //Maya Fey
student[2] = user[2] + " " + pass[2] + " " + description[2]; //Apollo Justice
student[3] = user[3] + " " + pass[3] + " " + description[3]; //Pearl Fey
student[4] = user[4] + " " + pass[4] + " " + description[4]; //Miles Edgeworth
student[5] = " ";
//MENU MESSAGE
welcome = "Press 1 to search for a student" + '\n' + "Press 2 to add a new student ID" + '\n' + "Press 3 to list all students" + '\n' +
"Press 4 to update an entry" + '\n' + "Press 5 to display students in order" + '\n' + "Press 6 to write students to file" + '\n' +
"Press 7 to read students from file" + '\n' + "Press 8 to exit" + '\n' + '\n' + "enter your selection";
//MESSAGE FOR USER SEARCHING
search = "Press 1 to search by user" + '\n' + "Press 2 to search by pass" + '\n' + "Press 3 to search by description" + '\n' + '\n' + "enter your selection";
//ENTRYS QUESTIONS STUDENT INPUT
user_entry = "enter your student ID";
pass_entry = "enter your password";
description_entry = "enter your user description";
master_entry = "Your master password for now is 'default'. Please enter:";
//-----------------------------------------------------------------------------------------------------------
//BEGINNING OF MENU
//WELCOME MESSAGE
JOptionPane.showMessageDialog(null,"Welcome to Owula Hesse's Password Storage Application");
//DEFAULT MASTER PASSWORD PROMPT
master_entry_prompt = JOptionPane.showInputDialog(master_entry);
//while (welcome_prompt_int != 8){
if (master_entry.equals("default")){
//if (welcome_prompt_int == 1) {
welcome_prompt = JOptionPane.showInputDialog(welcome);
welcome_prompt_int = Integer.parseInt(welcome_prompt);
}
else {
//JOptionPane.showMessageDialog(null,"The password 'default' was not entered, please enter it again.");
master_entry_prompt = JOptionPane.showInputDialog(master_entry);
}
//START OF WHILE LOOP
//AS LONG AS ANSWER ISNT 8 WHICH IS THE EXIT OPTION
while (welcome_prompt_int != 8){
//DISPLAYED FOR THE LIST ALL student OPTION
allstudents = "All available student: " + '\n' + '\n' +
student[0] + '\n' + //Phoenix Wright
student[1] + '\n' + //Maya Fey
student[2] + '\n' + //Apollo Justice
student[3] + '\n' + //Pearl Fey
student[4] + '\n' + //Miles Edgeworth
student[5] + '\n'; //New student ID
//DISPLAYED IN THE ENTRY UPDATE OPTION
update_entry = "please select the number of the user you would like to update" + '\n' + '\n' + "1. " + student[0] + '\n' + "2. " + student[1] + '\n' + "3. " + student[2]
+ '\n' + "4. " + student[3] + '\n' + "5. " + student[4] + '\n' + dot6 + student[5];
user_search = "username available:" + '\n' + '\n' + user[0] + '\n' + user[1] + '\n' + user[2] + '\n' + user[3] + '\n' + user[4] + '\n' + user[5] + '\n' + '\n' + "select a username";
pass_search = "passwords available:" + '\n' + '\n' + pass[0] + '\n' + pass[1] + '\n' + pass[2] + '\n' + pass[3] + '\n' + pass[4] + '\n' + pass[5] + '\n' + '\n' + "select a password";
//ALL ARRAYS NUMBERED 5 ARE RESERVED, THE USER CAN INPUT AND ARE INITIALLY SET TO " ", IF THE USER HASNT ENTERED A STUDENT THERES NO NEED TO DISPLAY THE SPACES
if (description[5].equals(" ")){
description_search = "descriptions available:" + '\n' + '\n' + description[0] + '\n' + description[1] + '\n' + description[2] + '\n' + description[3] + '\n' + description[4] + '\n' + '\n' + "select a description";
}
//IF THE USER HAS FILLED THE RESERVED ARRAYS NUMBERED 5 WITH STUDENT DETAILS THEN DISPLAY ALL
else {
description_search = "descriptions available:" + '\n' + '\n' + description[0] + '\n' + description[1] + '\n' + description[2] + '\n' + description[3] + '\n' + description[4] + '\n' + description[5] + '\n' + '\n' + "select a description";
}
//IN THE UPDATE ARRAY YOU CAN SELECT ONE OF THE STUDENTS, EACH OF THE STUDENT HAVE THEIR OWN RESPECTIVE UPDATE OPTIONS WHICH ARE SHOWN BY THE STRINGS BELOW
id_selection_one = "you have selected student: " + '\n' + '\n' + student[0] + '\n' + '\n' + "Press 1 to modify username" + '\n' + "Press 2 to modify password" + '\n' + "Press 3 to modify description"
+ '\n' + "Press 4 to modify all";
id_selection_two = "you have selected student: " + '\n' + '\n' + student[1] + '\n' + '\n' + "Press 1 to modify username" + '\n' + "Press 2 to modify password" + '\n' + "Press 3 to modify description"
+ '\n' + "Press 4 to modify all";
id_selection_three = "you have selected student: " + '\n' + '\n' + student[2] + '\n' + '\n' + "Press 1 to modify username" + '\n' + "Press 2 to modify password" + '\n' + "Press 3 to modify description"
+ '\n' + "Press 4 to modify all";
id_selection_four = "you have selected student: " + '\n' + '\n' + student[3] + '\n' + '\n' + "Press 1 to modify username" + '\n' + "Press 2 to modify password" + '\n' + "Press 3 to modify description"
+ '\n' + "Press 4 to modify all";
id_selection_five = "you have selected student: " + '\n' + '\n' + student[4] + '\n' + '\n' + "Press 1 to modify username" + '\n' + "Press 2 to modify password" + '\n' + "Press 3 to modify description"
+ '\n' + "Press 4 to modify all";
id_selection_six = "you have selected student: " + '\n' + '\n' + student[5] + '\n' + '\n' + "Press 1 to modify username" + '\n' + "Press 2 to modify password" + '\n' + "Press 3 to modify description"
+ '\n' + "Press 4 to modify all";
//THIS IS USED IN THE SORTING OPTION TO SHOW YOU HOW THE STUDENT WERE LISTED BEFORE THEY WERE SORTED
alphabet_initial = "View of original list: " + '\n' + '\n' +
student[0] + '\n' + //Phoenix Wright
student[1] + '\n' + //Maya Fey
student[2] + '\n' + //Apollo Justice
student[3] + '\n' + //Pearl Fey
student[4] + '\n' + //Miles Edgeworth
student[5] + '\n'; //New student ID
//---------------------------------------------------------------------------------------------------------------------
//SELECTION 1: USER SEARCH OPTION
//IF YOU PRESSED 1 IN THE MAIN MENU
if (welcome_prompt_int == 1) {
search_prompt = JOptionPane.showInputDialog(search);
search_prompt_int = Integer.parseInt(search_prompt);
//IF YOU CHOSE TO SEARCH BY USERNAME
if (search_prompt_int == 1) {
user_search_prompt = JOptionPane.showInputDialog(user_search);
//CHECKS IF THE USERNAME YOU ENTERED EXISTS IN AN ARRAY AND DISPLAYS STUDENT WITH THAT USERNAME
for (int x=0; x < user.length; x++){
if (user[x].equals(user_search_prompt)) {
JOptionPane.showMessageDialog(null,"student found: " + '\n' + '\n' + student[x]);
}
}
//TELLS YOU ALL STUDENTS WITH THE USERNAME YOU ENTERED HAVE BEEN DISPLAYED
JOptionPane.showMessageDialog(null,"no more students to display");
}
//IF YOU CHOSE TO SEARCH BY PASSWORDS
else if (search_prompt_int == 2) {
pass_search_prompt = JOptionPane.showInputDialog(pass_search);
//CHECKS IF THE PASSWORD YOU ENTERED EXISTS IN AN ARRAY AND DISPLAYS STUDENTS WITH THAT PASSWORD
for (int y=0; y < pass.length; y++){
if (pass_search_prompt.equals(pass[y])) {
JOptionPane.showMessageDialog(null,"student found: " + '\n' + '\n' + student[y]);
}
}
//TELLS YOU ALL STUDENTS WITH THE PASSWORD YOU ENTERED HAVE BEEN DISPLAYED
JOptionPane.showMessageDialog(null,"no more students to display");
}
//IF YOU CHOSE TO SEARCH BY DESCRIPTIONS
else if (search_prompt_int == 3) {
description_search_prompt = JOptionPane.showInputDialog(description_search);
//CHECKS IF THE DESCRIPTION YOU ENTERED EXISTS IN AN ARRAY AND DISPLAYS STUDENT WITH THAT DESCRIPTION
for (int z=0; z < description.length; z++){
if (description_search_prompt.equals(description[z])) {
JOptionPane.showMessageDialog(null,"student found: " + '\n' + '\n' + student[z]);
}
}
//TELLS YOU ALL STUDENTS WITH THE DESCRIPTION YOU SEARCHED HAVE BEEN DISPLAYED
JOptionPane.showMessageDialog(null,"no more students to display");
}
}
//END OF SELECTION 1: USERNAME SEARCH OPTION
//-------------------------------------------------------------------------------------------------------------------------------------------------
//SELECTION 2: INPUT NEW STUDENT id
//IF YOU PRESSED 2 IN THE MAIN MENU
else if (welcome_prompt_int == 2) {
//ASKS YOU TO ENTER STUDENT USERNAME
user_entry_prompt = JOptionPane.showInputDialog(user_entry);
//ASKS YOU TO ENTER STUDENT PASSWORD
pass_entry_prompt = JOptionPane.showInputDialog(pass_entry);
//ASKS YOU TO ENTER STUDENT DESCRIPTION
description_entry_prompt = JOptionPane.showInputDialog(description_entry);
JOptionPane.showMessageDialog(null,"your student has been added: " + user_entry_prompt + " " + pass_entry_prompt + " " + description_entry_prompt);
//THE THREE PIECES OF DATA YOU ENTERED ARE ASSIGNED TO THE RESERVED NUMBER 5 SLOT IN EACH ARRAY
user[5] = user_entry_prompt; pass[5] = pass_entry_prompt; description[5] = description_entry_prompt;
//FOR LOOP TO MAKE THE #5 ARRAY IN STUDENT EQUAL TO ALL THE REST OF THE DATA THE USER HAS INPUT
for (int i=5; i<student.length; i++){
student[i] = user[5] + " " + pass[5] + " " + description[5];
dot6 = "6. ";
}
}
//END OF SELECTION2: INPUT NEW STUDENT
//------------------------------------------------------------------------------------------------------------------
//SELECTION 3: LIST ALL STUDENTS
//IF YOU PRESSED 3 IN THE MAIN MENU
else if (welcome_prompt_int == 3) {
//SHOWS ALL STUDENT STRING WHICH LISTS THE WHOLE STUDENT ARRAY
JOptionPane.showMessageDialog(null,allstudents);
}
//END OF SELECTION 3: LIST ALL STUDENTS
//-------------------------------------------------------------------------------------------------------------------
//SELECTION 4: UPDATE ENTRY OPTION
//IF YOU PRESSED 4 IN THE MAIN MENU
else if (welcome_prompt_int == 4) {
update_entry_prompt = JOptionPane.showInputDialog(update_entry);
update_entry_prompt_int = Integer.parseInt(update_entry_prompt);
//FIRST STUDENT UPDATE OPTION
//IF YOU SELECTED THE FIRST STUDENT
if (update_entry_prompt_int == 1){
id_selection_one_prompt = JOptionPane.showInputDialog(id_selection_one);
id_selection_one_prompt_int = Integer.parseInt(id_selection_one_prompt);
//IF YOU CHOSE TO UPDATE THE FIRST STUDENT USERNAME
if (id_selection_one_prompt_int == 1){
user_entry_prompt = JOptionPane.showInputDialog(user_entry);
//CHANGES STUDENT ORIGINAL USERNAME TO WHAT YOU HAVE ENTERED
user[0] = user_entry_prompt;
//CHANGES STUDENT OVERALL DATA
student[0] = user[0] + " " + pass[0] + " " + description[0];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user_entry_prompt + " " + pass[0] + " " + description[0]);
}
//IF YOU CHOSE TO UPDATE THE FIRST STUDENT PASSWORD
else if (id_selection_one_prompt_int == 2){
pass_entry_prompt = JOptionPane.showInputDialog(pass_entry);
//CHANGES STUDENT ORIGINAL PASSWORD TO WHAT YOU HAVE ENTERED
pass[0] = pass_entry_prompt;
//CHANGES STUDENT OVERALL DATA
student[0] = user[0] + " " + pass[0] + " " + description[0];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user[0] + " " + pass_entry_prompt + " " + description[0]);
}
//IF YOU CHOSE TO UPDATE THE FIRST STUDENT DESCRIPTION
else if (id_selection_one_prompt_int == 3){
description_entry_prompt = JOptionPane.showInputDialog(description_entry);
//CHANGES STUDENT ORIGINAL CESCRIPTION TO WHAT YOU HAVE ENTERED
description[0] = description_entry_prompt;
//CHANGES STUDENT OVERALL DATA
student[0] = user[0] + " " + pass[0] + " " + description[0];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user[0] + " " + pass[0] + " " + description_entry_prompt);
}
//IF YOU CHOSE TO UPDATE ALL THE FIRST STUDENT ASPECTS (USERNAME, PASSWORD & DESCRIPTION)
else if (id_selection_one_prompt_int == 4){
user_entry_prompt = JOptionPane.showInputDialog(user_entry);
pass_entry_prompt = JOptionPane.showInputDialog(pass_entry);
description_entry_prompt = JOptionPane.showInputDialog(description_entry);
//CHANGES STUDENT ORIGINAL USERNAME, PASSWORD AND DESCRIPTION TO WHAT YOU HAVE ENTERED
user[0] = user_entry_prompt; pass[0] = pass_entry_prompt; description[0] = description_entry_prompt;
//CHANGES OVERALL STUDENT DATA
student[0] = user[0] + " " + pass[0] + " " + description[0];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user_entry_prompt + " " + pass_entry_prompt + " " + description_entry_prompt);
}
}
//END OF FIRST STUDENT UPDATE
//SECOND STUDENT UPDATE
//IF YOU SELECTED TO UPDATE THE SECOND STUDENT
else if (update_entry_prompt_int == 2){
id_selection_two_prompt = JOptionPane.showInputDialog(id_selection_two);
id_selection_two_prompt_int = Integer.parseInt(id_selection_two_prompt);
//IF YOU CHOSE TO UPDATE SECOND STUDENT USERNAME
if (id_selection_two_prompt_int == 1){
user_entry_prompt = JOptionPane.showInputDialog(user_entry);
//CHANGES STUDENT ORIGINAL USERNAME TO WHAT YOU HAVE ENTERED
user[1] = user_entry_prompt;
//CHANGES STUDENT OVERALL DATA
student[1] = user[1] + " " + pass[1] + " " + description[1];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user_entry_prompt + " " + pass[1] + " " + description[1]);
}
//IF YOU CHOSE TO UPDATE SECOND STUDENT PASSWORD
else if (id_selection_two_prompt_int == 2){
pass_entry_prompt = JOptionPane.showInputDialog(pass_entry);
//CHANGES STUDENT ORIGINAL PASSWORD TO WHAT YOU HAVE ENTERED
pass[1] = pass_entry_prompt;
//CHANGES STUDENT OVERALL DATA
student[1] = user[1] + " " + pass[1] + " " + description[1];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user[1] + " " + pass_entry_prompt + " " + description[1]);
}
//IF YOU CHOSE TO UPDATE SECOND STUDENT DESCRIPTION
else if (id_selection_two_prompt_int == 3){
description_entry_prompt = JOptionPane.showInputDialog(description_entry);
//CHANGES STUDENT ORIGINAL DESCRIPTION TO WHAT YOU HAVE ENTERED
description[1] = description_entry_prompt;
//CHANGES STUDENT OVERALL DATA
student[1] = user[1] + " " + pass[1] + " " + description[1];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user[1] + " " + pass[1] + " " + description_entry_prompt);
}
//IF YOU CHOSE TO UPDATE ALL THE SECOND STUDENT ASPECTS(USERNAME, PASSWORD AND DESCRIPTION)
else if (id_selection_two_prompt_int == 4){
user_entry_prompt = JOptionPane.showInputDialog(user_entry);
pass_entry_prompt = JOptionPane.showInputDialog(pass_entry);
description_entry_prompt = JOptionPane.showInputDialog(description_entry);
//CHANGES STUDENT ORIGINAL USERNAME, PASSWORD AND DESCRIPTION TO WHAT YOU HAVE ENTERED
user[1] = user_entry_prompt; pass[1] = pass_entry_prompt; description[1] = description_entry_prompt;
//CHANGES STUDENT OVERALL DATA
student[1] = user[1] + " " + pass[1] + " " + description[1];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user_entry_prompt + " " + pass_entry_prompt + " " + description_entry_prompt);
}
}
//END OF SECOND STUDENT UPDATING
//THIRD STUDENT UPDATE
else if (update_entry_prompt_int == 3){
id_selection_three_prompt = JOptionPane.showInputDialog(id_selection_three);
id_selection_three_prompt_int = Integer.parseInt(id_selection_three_prompt);
//IF YOU CHOSE TO UPDATE THE THIRD STUDENT USERNAME
if (id_selection_three_prompt_int == 1){
user_entry_prompt = JOptionPane.showInputDialog(user_entry);
//CHANGES STUDENT ORIGINAL USERNAME TO WHAT YOU HAVE ENTERED
user[2] = user_entry_prompt;
//CHANGES STUDENT OVERALL DATA
student[2] = user[2] + " " + pass[2] + " " + description[2];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user_entry_prompt + " " + pass[2] + " " + description[2]);
}
//IF YOU CHOSE TO UPDATE THE THIRD STUDENT USERNAME
else if (id_selection_three_prompt_int == 2){
pass_entry_prompt = JOptionPane.showInputDialog(pass_entry);
//CHANGES STUDENT ORIGINAL PASSWORD TO WHAT YOU HAVE ENTERED
pass[2] = pass_entry_prompt;
//CHANGES STUDENT OVERALL DATA
student[2] = user[2] + " " + pass[2] + " " + description[2];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user[2] + " " + pass_entry_prompt + " " + description[2]);
}
//IF YOU CHOSE TO UPDATE THE THIRD STUDENT DESCRIPTION
else if (id_selection_three_prompt_int == 3){
description_entry_prompt = JOptionPane.showInputDialog(description_entry);
//CHANGES STUDENT ORIGINAL DESCRIPTION TO WHAT YOU HAVE ENTERED
description[2] = description_entry_prompt;
//CHANGES STUDENT OVERALL DATA
student[2] = user[2] + " " + pass[2] + " " + description[2];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user[2] + " " + pass[2] + " " + description_entry_prompt);
}
//IF YOU CHOSE TO UPDATE ALL ATTRIBUTES OF THE THIRD STUDENT (USERNAME, PASSWORD AND DESCRIPTION)
else if (id_selection_three_prompt_int == 4){
user_entry_prompt = JOptionPane.showInputDialog(user_entry);
pass_entry_prompt = JOptionPane.showInputDialog(pass_entry);
description_entry_prompt = JOptionPane.showInputDialog(description_entry);
//CHANGES STUDENT ORIGINAL USERNAME, PASSWORD AND DESCRIPTION TO WHAT YOU HAVE ENTERED
user[2] = user_entry_prompt; pass[2] = pass_entry_prompt; description[2] = description_entry_prompt;
//CHANGES STUDENT OVERALL DATA
student[2] = user[2] + " " + pass[2] + " " + description[2];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user_entry_prompt + " " + pass_entry_prompt + " " + description_entry_prompt);
}
}
//END OF THIRD STUDENT UPDATING
//FOURTH STUDENT UPDATE
//IF YOU CHOSE TO UPDATE THE FOURTH STUDENT
else if (update_entry_prompt_int == 4){
id_selection_four_prompt = JOptionPane.showInputDialog(id_selection_four);
id_selection_four_prompt_int = Integer.parseInt(id_selection_four_prompt);
//IF YOU CHOSE TO UPDATE THE FOURTH STUDENT USERNAME
if (id_selection_four_prompt_int == 1){
user_entry_prompt = JOptionPane.showInputDialog(user_entry);
//CHANGES STUDENT ORIGINAL USERNAME TO WHAT YOU HAVE ENTERED
user[3] = user_entry_prompt;
//CHANGES STUDENT OVERALL DATA
student[3] = user[3] + " " + pass[3] + " " + description[3];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user_entry_prompt + " " + pass[3] + " " + description[3]);
}
//IF YOU CHOSE TO UPDATE THE FOURTH STUDENT PASSWORD
else if (id_selection_four_prompt_int == 2){
pass_entry_prompt = JOptionPane.showInputDialog(pass_entry);
//CHANGES STUDENT ORIGINAL PASSWORD TO WHAT YOU HAVE ENTERED
pass[3] = pass_entry_prompt;
//CHANGES STUDENT OVERALL DATA
student[3] = user[3] + " " + pass[3] + " " + description[3];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user[3] + " " + pass_entry_prompt + " " + description[3]);
}
//IF YOU CHOSE TO UPDATE THE FOURTH STUDENT DESCRIPTION
else if (id_selection_four_prompt_int == 3){
description_entry_prompt = JOptionPane.showInputDialog(description_entry);
//CHANGES STUDENT ORIGINAL DESCRIPTION TO WHAT YOU HAVE ENTERED
description[3] = description_entry_prompt;
//CHANGES STUDENT OVERALL DATA
student[3] = user[3] + " " + pass[3] + " " + description[3];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user[3] + " " + pass[3] + " " + description_entry_prompt);
}
//IF YOU CHOSE TO UPDATE ALL OF THE FOURTH STUDENT ATTRIBUTES (USERNAME, PASSWORD AND DESCRIPTION)
else if (id_selection_four_prompt_int == 4){
user_entry_prompt = JOptionPane.showInputDialog(user_entry);
pass_entry_prompt = JOptionPane.showInputDialog(pass_entry);
description_entry_prompt = JOptionPane.showInputDialog(description_entry);
//CHANGES STUDENT ORIGINAL USERNAME, PASSWORD AND DESCRIPTION TO WHAT YOU HAVE ENTERED
user[3] = user_entry_prompt; pass[3] = pass_entry_prompt; description[3] = description_entry_prompt;
//CHANGES STUDENT OVERALL DATA
student[3] = user[3] + " " + pass[3] + " " + description[3];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user_entry_prompt + " " + pass_entry_prompt + " " + description_entry_prompt);
}
}
//END OF FOURTH STUDENT UPDATING
//FIFTH STUDENT UPDATE
//IF YOU CHOSE TO UPDATE THE FIFTH STUDENT
else if (update_entry_prompt_int == 5){
id_selection_five_prompt = JOptionPane.showInputDialog(id_selection_five);
id_selection_five_prompt_int = Integer.parseInt(id_selection_five_prompt);
//IF YOU CHOSE TO UPDATE THE FIFTH STUDENT USERNAME
if (id_selection_five_prompt_int == 1){
user_entry_prompt = JOptionPane.showInputDialog(user_entry);
//CHANGES STUDENT ORIGINAL USERNAME TO WHAT YOU HAVE ENTERED
user[4] = user_entry_prompt;
//CHANGES STUDENT OVERALL DATA
student[4] = user[4] + " " + pass[4] + " " + description[4];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user_entry_prompt + " " + pass[4] + " " + description[4]);
}
//IF YOU CHOSE TO UPDATE THE FIFTH STUDENT PASSWORD
else if (id_selection_five_prompt_int == 2){
pass_entry_prompt = JOptionPane.showInputDialog(pass_entry);
//CHANGES STUDENT ORIGINAL PASSWORD TO WHAT YOU HAVE ENTERED
pass[4] = pass_entry_prompt;
//CHANGES STUDENT OVERALL DATA
student[4] = user[4] + " " + pass[4] + " " + description[4];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user[4] + " " + pass_entry_prompt + " " + description[4]);
}
//IF YOU CHOSE TO UPDATE THE FIFTH STUDENT DESCRIPTION
else if (id_selection_five_prompt_int == 3){
description_entry_prompt = JOptionPane.showInputDialog(description_entry);
//CHANGES student ORIGINAL DESCRIPTION TO WHAT YOU HAVE ENTERED
description[4] = description_entry_prompt;
//CHANGES STUDENT OVERALL DATA
student[4] = user[4] + " " + pass[4] + " " + description[4];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user[4] + " " + pass[4] + " " + description_entry_prompt);
}
//IF YOU CHOSE TO UPDATE ALL ATTRIBUTES OF THE FIFTH STUDENT (USERNAME, PASSWORD AND DESCRIPTION)
else if (id_selection_five_prompt_int == 4){
user_entry_prompt = JOptionPane.showInputDialog(user_entry);
pass_entry_prompt = JOptionPane.showInputDialog(pass_entry);
description_entry_prompt = JOptionPane.showInputDialog(description_entry);
//CHANGES STUDENT ORIGINAL USERNAME, PASSWORD AND DESCRIPTION TO WHAT YOU HAVE ENTERED
user[4] = user_entry_prompt; pass[4] = pass_entry_prompt; description[4] = description_entry_prompt;
//CHANGES STUDENT OVERALL DATA
student[4] = user[4] + " " + pass[4] + " " + description[4];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user_entry_prompt + " " + pass_entry_prompt + " " + description_entry_prompt);
}
}
//END OF FIFTH STUDENT UPDATING
//SIXTH STUDENT UPDATE
//IF YOU CHOSE TO UPDATE THE SIXTH STUDENT
else if (update_entry_prompt_int == 6){
id_selection_six_prompt = JOptionPane.showInputDialog(id_selection_six);
id_selection_six_prompt_int = Integer.parseInt(id_selection_six_prompt);
//IF YOU CHOSE TO UPDATE THE SIXTH STUDENT USERNAME
if (id_selection_six_prompt_int == 1){
user_entry_prompt = JOptionPane.showInputDialog(user_entry);
//CHANGES STUDENT ORIGINAL USERNAME TO WHAT YOU HAVE ENTERED
user[5] = user_entry_prompt;
//CHANGES STUDENT OVERALL DATA
student[5] = user[5] + " " + pass[5] + " " + description[5];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user_entry_prompt + " " + pass[5] + " " + description[5]);
}
//IF YOU CHOSE TO UPDATE THE SIXTH STUDENT PASSWORD
else if (id_selection_six_prompt_int == 2){
pass_entry_prompt = JOptionPane.showInputDialog(pass_entry);
//CHANGES STUDENT ORIGINAL PASSWORD TO WHAT YOU HAVE ENTERED
pass[5] = pass_entry_prompt;
//CHANGES STUDENT OVERALL DATA
student[5] = user[5] + " " + pass[5] + " " + description[5];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user[5] + " " + pass_entry_prompt + " " + description[5]);
}
//IF YOU CHOSE TO UPDATE THE SIXTH STUDENT DESCRIPTION
else if (id_selection_six_prompt_int == 3){
description_entry_prompt = JOptionPane.showInputDialog(description_entry);
//CHANGES STUDENT ORIGINAL description TO WHAT YOU HAVE ENTERED
description[5] = description_entry_prompt;
//CHANGES STUDENT OVERALL DATA
student[5] = user[5] + " " + pass[5] + " " + description[5];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user[5] + " " + pass[5] + " " + description_entry_prompt);
}
//IF YOU CHOSE TO UPDATE ALL ATTRIBUTES OF THE SIXTH STUDENT (USERNAME, PASSWORD AND DESCRIPTION)
else if (id_selection_six_prompt_int == 4){
user_entry_prompt = JOptionPane.showInputDialog(user_entry);
pass_entry_prompt = JOptionPane.showInputDialog(pass_entry);
description_entry_prompt = JOptionPane.showInputDialog(description_entry);
//CHANGES STUDENT ORIGINAL USERNAME, PASSWORD AND DESCRIPTION TO WHAT YOU HAVE ENTERED
user[5] = user_entry_prompt; pass[5] = pass_entry_prompt; description[5] = description_entry_prompt;
//CHANGES STUDENT OVERALL DATA
student[5] = user[5] + " " + pass[5] + " " + description[5];
JOptionPane.showMessageDialog(null,"student has been modified to: " + user_entry_prompt + " " + pass_entry_prompt + " " + description_entry_prompt);
}
}
}
//END OF UPDATING SIXTH STUDENT
//END OF SELECTION 4: UPDATE STUDENT ENTRYS
//---------------------------------------------------------------------------------------------
//SELECTION 5: LIST USERNAMES IN ALPHABETICAL ORDER
//IF YOU SELECTED 5 IN THE MAIN MENU
else if (welcome_prompt_int == 5){
JOptionPane.showMessageDialog(null,alphabet_initial);
//SORTS THE VALUES OF THE ARRAY STUDENTS IN ORDER
Arrays.sort(student);
//SINCE THE STUDENT 5 ARRAY IS RESERVED FOR THE USERNAME STUDENT ENTRY IT IS INITIALLY SET AT " ", IF THE USER HASNT SET IT YET THEN THERE IS NO NEED FOR IT TO BE DISPLAYED
if (student[0].equals(" ")){
//DISPLAYS STUDENT USERNAMES IN ALPHABETICAL ORDER
JOptionPane.showMessageDialog(null,"list of students after sorting" + '\n' + '\n' + student[1] + '\n' + student[2] + '\n' + student[3] + '\n' + student[4] + '\n' + student[5]);
}
//THIS WILL BE EXECUTED IF THE USER HAS FILLED ARRAY 5 WITH DATA, ALL THE student WILL BE LISTED INCLUDING THE USERS ENTRY STUDENT
else{
JOptionPane.showMessageDialog(null,"list of students after sorting" + '\n' + '\n' + user[0] + '\n' + user[1] + '\n' + user[2] + '\n' + user[3] + '\n' + user[4] + '\n' + user[5]);
}
}
//END OF SELECTION 5: LIST IN ORDER
//----------------------------------------------------------------------------------------------------
//SELECTION SIX: WRITE
//IF YOU SELECTED 6 IN THE MAIN MENU
else if (welcome_prompt_int == 6) {
//CREATES AN OBJECT CONNECTED TO THE FILE
final FileWriter write_to_file = new FileWriter("allstudents.txt");
//BUFFER COLLECTS CHARACTERS AND CONNECTS IT TO OUTPUTFILE
final BufferedWriter OBuffer = new BufferedWriter(write_to_file);
//VALUES ARE CONVERTED AND SENT IN A STREAM TO THE BUFFER
final PrintWriter pstream = new PrintWriter(OBuffer);
//WRITES ALL THE STUDENT DATA IN THE allstudents String TO allstudents.TXT FILE
pstream.println(allstudents);
//ENDS DATA SENDING TO FILE
pstream.close();
//TELLS YOU THE DATA HAS BEEN WRITTEN
JOptionPane.showMessageDialog(null,"All students have been written to a text file called 'allstudents.txt'");
}
//END OF SELECTION 6: WRITE
//---------------------------------------------------------------------------------------------------------
//SELECTION 7: READ FROM FILE
//IF YOU SELECTED 7 IN THE MAIN MENU
else if (welcome_prompt_int == 7){
//A FILE READING OBJECT IS CREATED WHICH WILL READ FROM allstudents.TXT
final FileReader read_the_file = new FileReader("allstudents.txt");
//A BUFFER IS CONNECTED TO THE FILE READER AND StringS OF DATA ARE STORED
final BufferedReader IBuffer = new BufferedReader(read_the_file);
//EACH String IS ASSIGNED TO A LINE NUMBER IN THE FILE allstudents.TXT
String line1 = IBuffer.readLine();
String line2 = IBuffer.readLine();
String line3 = IBuffer.readLine();
String line4 = IBuffer.readLine();
String line5 = IBuffer.readLine();
String line6 = IBuffer.readLine();
String line7 = IBuffer.readLine();
String line8 = IBuffer.readLine();
//LINES 1 AND 2 ARE MISSED OUT BECAUSE THE FIRST LINE IS A DISPLAY MESSAGE AND THE SECOND LINE IS EMPTY, THE STUDENT DATA STARTS FORM THE THIRD LINE ONWARDS
//VALUES IN THE ARRAY student ARE REDEFINED AND MADE EQUAL TO CERTAIN LINES IN THE allstudents.TXT FILE
student[0] = line3;
student[1] = line4;
student[2] = line5;
student[3] = line6;
student[4] = line7;
student[5] = line8;
//DISPLAYS THE NEW VALUES OF THE student ARRAY
JOptionPane.showMessageDialog(null,"Data read: " + '\n' + '\n' + student[0] + '\n' + student[1] + '\n' + student[2] + '\n' + student[3] + '\n' + student[4] + '\n' + student[5]);
}
//END OF SELECTION 7: READ FROM FILE
//---------------------------------------------------------------------------------------------------------------
//IF FIRST MENU ANSWER ISNT AN INTEGER BETWEEN 1-8
else {
//TELLS YOU TO ENTER A CORRECT NUMBER AND TAKES YOU BACK TO THE BEGINNING OF THE MENU
JOptionPane.showMessageDialog(null,"please enter an appropriate selection");
}
welcome_prompt = JOptionPane.showInputDialog(welcome);
welcome_prompt_int = Integer.parseInt(welcome_prompt);
}
//---------------------------------------------------------------------------------------------------------------
//END OF WHILE LOOP
//EXITING MESSAGE
JOptionPane.showMessageDialog(null,"Thank you for visiting Owula Hesse's Password Storage Application");
System.exit(0);
//----------------------------------------------------------------------------------------------------------------
//END OF MENU
}
}
//END OF PROGRAM 'OWULA HESSE'S PASSWORD STORAGE APPLICATION
This is the error that I get:
C:\Program Files (x86)\Xinox Software\JCreatorV4\Module - Java\ohpassword.java:169: error: variable welcome_prompt_int might not have been initialized
I'm using JCreator.
Also, here are my 2 other questions:
How do I set a master password and set it within the program and later change it?
How do I set a delete entry within my menu?
I will get back to you during the late evening tonight.
Thank you for your help :)-:
Re: Java 169 Error: One of my variable haven't been initialized + 3 other questions
You do realise that 1000+ lines of code is waaaay more than most people on a forum will bother to look at?
Anyway, that error says that you are attempting to read the welcome_prompt_int variable before you have given it a value.
You really need to break that code up into methods as well, since the lack of them makes it hard to follow (never mind the sheer length of that code).
Any method > about 40 lines probably ought to be broken up. Each user action should be a method. Setting up the data should be a method. That sort of thing. That way you can focus on the method doing a thing, rather than trying to pick that functionality out from a sea of other stuff.
Re: Java 169 Error: One of my variable haven't been initialized + 3 other questions
Quote:
Originally Posted by
Tolls
You do realise that 1000+ lines of code is waaaay more than most people on a forum will bother to look at?
Anyway, that error says that you are attempting to read the welcome_prompt_int variable before you have given it a value.
You really need to break that code up into methods as well, since the lack of them makes it hard to follow (never mind the sheer length of that code).
Any method > about 40 lines probably ought to be broken up. Each user action should be a method. Setting up the data should be a method. That sort of thing. That way you can focus on the method doing a thing, rather than trying to pick that functionality out from a sea of other stuff.
Apologies for the long code, it's just that I haven't really gone that deep into Java to know how to create methods.
If I knew how to do it, I would've separated them for the forum.
Thanks for the advise, I'll look into it then come back to this thread. :(y):
Re: Java 169 Error: One of my variable haven't been initialized + 3 other questions
Hi giga, you just need to initialize welcome_prompt_int =0; in the top of the code and the program will run. And as Tolls told you, try to separte your code and methods and even classes, thats what makes Java so cool, OOP and read a little bit about Refactoring.
Re: Java 169 Error: One of my variable haven't been initialized + 3 other questions
Hi there,
I've edited my code into different sections now, hopefully it has made it more simple. The only thing I didn't add is my master password which I still need...
Code:
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
public class StorageApp {
private StorageAppEntry entry[];
private int counter;
private String SName;
private int notfound = 0;
public static void main(String[] args) {
StorageApp a = new StorageApp();
a.entry = new StorageAppEntry[100];
int option = 0;
try {
while (option != 5) {
String content = "Choose an Option\n\n"
+ "[1] Add an Entry\n"
+ "[2] Delete an Entry\n"
+ "[3] Update an Entry\n"
+ "[4] View all Entries\n"
+ "[5] View Specific Entry\n"
+ "[6] Exit";
option = Integer.parseInt(JOptionPane.showInputDialog(content));
switch (option) {
case 1:
a.addEntry();
break;
case 2:
a.deleteEntry();
break;
case 3:
a.editEntry();
break;
case 4:
a.viewAll();
break;
case 5:
a.searchEntry();
break;
case 6:
System.exit(1);
break;
default:
JOptionPane.showMessageDialog(null, "Invalid Choice!");
}
}
}catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "Please Choose a Number in the displayed Menu");
}
}
public void addEntry() {
entry[counter] = new StorageAppEntry();
entry[counter].setName(JOptionPane.showInputDialog("Enter name: "));
entry[counter].setUsername(JOptionPane.showInputDialog("Enter username: "));
entry[counter].setPassword(JOptionPane.showInputDialog("Enter password: "));
entry[counter].setdescription(JOptionPane.showInputDialog("Enter description: "));
counter++;
}
public void viewAll() {
String addText = " NAME\tUSERNAME\tPASSWORD\tDESCRIPTION\n\n";
int nonNull = 0;
for (int i = 0; i < entry.length; i++) {
if (entry[i] != null) {
addText = addText + entry[i].getInfo() + "\n";
nonNull++;
}
else if (nonNull == counter) {
break;
}
}
JOptionPane.showMessageDialog(null, new JTextArea(addText));
}
public void searchEntry() {
SName = JOptionPane.showInputDialog("Enter Name to find: ");
searchMethod();
}
public void searchMethod() {
for (int i = 0; i < counter; i++) {
if (entry[i].getName().equals(SName)) {
JOptionPane.showMessageDialog(null, entry[i].getInfo2());
notfound = 0;
break;
} else {
notfound++;
}
}
if (notfound != 0) {
JOptionPane.showMessageDialog(null, "Name Not Found!");
}
}
public void editEntry() {
SName = JOptionPane.showInputDialog("Enter Name to edit: ");
for (int i = 0; i < counter; i++) {
if (entry[i].getName().equals(SName)) {
entry[i] = new StorageAppEntry();
entry[i].setName(JOptionPane.showInputDialog("Enter new name: "));
entry[i].setUsername(JOptionPane.showInputDialog("Enter new username: "));
entry[i].setDescription(JOptionPane.showInputDialog("Enter new description: "));
notfound = 0;
break;
} else {
notfound++;
}
}
if (notfound != 0) {
JOptionPane.showMessageDialog(null, "Name Not Found!");
}
}
public void deleteEntry() {
SName = JOptionPane.showInputDialog("Enter Name to delete: ");
for (int i = 0; i < counter; i++) {
if (entry[i].getName().equals(SName)) {
JOptionPane.showMessageDialog(null, "Found!");
entry[i] = null;
break;
}
}
}
}
With this I have 4 errors:
-- Quote:
------------------Configuration: StorageApp - JDK version 1.7.0_05 <Default> - <Default>--------------------
C:\Users\OH\Documents\JCreator Pro\MyProjects\passwordOH\StorageApp\src\StorageAp p.java:6: error: cannot find symbol
private StorageAppEntry entry;
^
symbol: class StorageAppEntry
location: class StorageApp
C:\Users\OH\Documents\JCreator Pro\MyProjects\passwordOH\StorageApp\src\StorageAp p.java:14: error: cannot find symbol
a.entry = new StorageAppEntry[100];
^
symbol: class StorageAppEntry
location: class StorageApp
C:\Users\OH\Documents\JCreator Pro\MyProjects\passwordOH\StorageApp\src\StorageAp p.java:57: error: cannot find symbol
entry[counter] = new StorageAppEntry();
^
symbol: class StorageAppEntry
location: class StorageApp
C:\Users\OH\Documents\JCreator Pro\MyProjects\passwordOH\StorageApp\src\StorageAp p.java:106: error: cannot find symbol
entry[i] = new StorageAppEntry();
^
symbol: class StorageAppEntry
location: class StorageApp
4 errors
Process completed.
Errors in line 6, 14, 57 & 106.
Any help? :(y):