File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Can't figure out why variable isn't recognized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can Watch "Can New topic
Author

Can't figure out why variable isn't recognized

Todd Springberg
Greenhorn

Joined: Jan 28, 2005
Posts: 2
I get these errors when I try to compile, and can't figure out why. The array is created, I checked that, and I checked for spelling too.

D:\JavaPrograms\program1b\App.java:56: cannot resolve symbol
symbol : variable accounts
location: class App
int index = login (accounts);
^
D:\JavaPrograms\program1b\App.java:58: cannot resolve symbol
symbol : variable accounts
location: class App
accountProcessing(accounts[index]);


PROGRAM:
// Make it possible to reference required packaged code.

import java.io.*;
import java.text.*;
import java.security.*;
import java.util.*;

// This class defines a text-oriented, menu-driven program that creates and
// processes an array of Account objects.
// ----- Written by: anonymous

public class App {
public static void main(String[] args) {

// Declare and initialize an array of Account objects.
try{
Account[] accounts = new Account[5];
accounts[0] = new Account(311, "Garson", "3tx4z7", 2000);
accounts[1] = new Account(348, "Bogart", "h3qs6y", 10);
accounts[2] = new Account(365, "Stewart", "kx55nv", 10);
accounts[3] = new Account(370, "Gable", "jw3k19", 1000);
accounts[4] = new Account(383, "Davis", "42gr6c", 10);
}

catch (Exception err){
System.out.println(err.getMessage());
Utility.pressEnter();
System.out.println("");
}
// Constants and variables needed for main menu processing.

final int ACCOUNT_ACCESS = 1;
final int EXIT = 3;
int choice = 0;

// This loop handles main menu processing. The loop will continue until
// the user chooses to exit.

do {

// Display the menu and get the user's choice.

System.out.println("MAIN MENU");
System.out.println("");
System.out.println("1 - Access your account");
System.out.println("3 - Exit");
System.out.println("");
System.out.print("Enter choice: ");
choice = Utility.readInt();
System.out.println("");

// This block enables a user to access their account.

if (choice == ACCOUNT_ACCESS) {
try {
int index = login (accounts);
System.out.println("");
accountProcessing(accounts[index]);
}
catch (GeneralSecurityException err) {
System.out.println("Login failed(" + err.getMessage() + ")");
Utility.pressEnter();
System.out.println("");
}
catch (Exception err) {
System.out.println(err.getMessage());
Utility.pressEnter();
System.out.println("");
}

}

// This block processes a request to exit.

else if (choice == EXIT) {
System.out.println("Exit in progress");
}

// This block processes an invalid menu selection.

else {
System.out.println("Invalid menu choice");
Utility.pressEnter();
System.out.println("");
}
} while (choice != EXIT); // End of main menu loop.

// Display a termination message.

System.out.println("Processing complete");
}

// This method handles account processing.

public static void accountProcessing (Account account) {
final int EXIT = 6;
int choice = 0;
do {
System.out.println("ACCOUNT MENU");
System.out.println("");
System.out.println("6 - Exit");
System.out.println("");
System.out.print("Enter choice: ");
choice = Utility.readInt();
System.out.println("");
if (choice == EXIT) {
}
else {
System.out.println("Invalid menu choice");
Utility.pressEnter();
System.out.println("");
}
} while (choice != EXIT); // End of account menu loop.
}

// This method can be called to handle account login. If the user enters a
// valid account number and password, the Account object's index is returned.
// Otherwise, a GeneralSecurityException is thrown.

public static int login (Account[] accounts) throws GeneralSecurityException {
int index = -1;
System.out.print("Enter account number: ");
int acctNum = Utility.readInt();
System.out.print("Enter password: ");
String pw = Utility.readString();
for (int i = 0; i < accounts.length; i++) {
if (accounts[i].getNumber() == acctNum &&
accounts[i].getPassword().equals(pw)) {
index = i;
}
}
if (index == -1) {
throw new GeneralSecurityException(acctNum + ":" + pw);
}
return index;
}

}


// This class encapsulates the processing of an Account object.
// ----- Written by: Jon Huhtala

class Account {

// Class constants.

public static final double MONTHLY_INTEREST_RATE = .005;

// Every Account object will have these instance variables.

private int number;
private String name;
private String password;
private double balance;

// This constructor can be called to create an Account object from values
// received as parameters.

public Account(int iNumber, String iName, String iPassword, double iBalance)
throws Exception {
setNumber(iNumber);
setName(iName);
setPassword(iPassword);
setBalance(iBalance);
}

// These methods can be called to "set" the value of their respective
// instance variable. Each method edits the data it receives and throws an
// Exception if the data is invalid.

public void setNumber(int nNumber) throws Exception{
if (nNumber > 0) {
number = nNumber;
}
else {
throw new Exception("Invalid account number");
}
}
public void setName(String nName) throws Exception {
if (nName.length() > 0) {
name = nName;
}
else {
throw new Exception("Invalid name");
}
}
public void setPassword(String nPassword) throws Exception {
if (nPassword.length() >= 6) {
password = nPassword;
}
else {
throw new Exception("Invalid password");
}
}
public void setBalance(double nBalance) throws Exception {
if (nBalance >= 0) {
balance = nBalance;
}
else {
throw new Exception("Invalid balance");
}
}

// These methods can be called to "get" the value of their respective
// instance variable.

public int getNumber() {
return number;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public double getBalance() {
return balance;
}

// This method returns a formatted string of object information.

public String toString() {
return getNumber() + ", " + getName() + ", " + getPassword() + ", " +
Utility.moneyFormat(getBalance());
}
}

// This class contains custom methods that support application processing.
// ----- Written by: Jon Huhtala

class Utility {

// This BufferedReader object is used within this class to read data from the
// input stream (keyboard).

private static BufferedReader kbReader =
new BufferedReader(new InputStreamReader(System.in));

// This method can be called to read an int value from the keyboard.

public static int readInt() {
Integer result = null;
while (result == null) {
try {
result = new Integer(kbReader.readLine());
}
catch(Exception err) {
System.out.print("ERROR! Please try again: ");
}
}
return result.intValue();
}

// This method can be called to read an double value from the keyboard.

public static double readDouble() {
Double result = null;
while (result == null) {
try {
result = new Double(kbReader.readLine());
}
catch(Exception err) {
System.out.print("ERROR! Please try again: ");
}
}
return result.doubleValue();
}

// This method can be called to read a String value from the keyboard.

public static String readString() {
String result = null;
while (result == null) {
try {
result = kbReader.readLine();
}
catch(Exception err) {
System.out.print("ERROR! Please try again: ");
}
}
return result;
}

// This method can be called to convert a double value into a formatted
// currency string.

public static String moneyFormat(double amount) {
return NumberFormat.getCurrencyInstance().format(amount);
}

// This method can be called to prompt the user to press the ENTER key to
// continue.

public static void pressEnter() {
System.out.print("Press ENTER to continue...");
readString();
}
}
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
Accounts accounts is declared inside the try block, so the stuff outside the block (like "int index = login (accounts);") can't see it.

May I make a couple of suggestions?

1) 'CODE' tags will make your code easier to read by retaining your indentation.

2) Only post the code that is relevant to the problem rather than posting all 300 lines.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Todd Springberg
Greenhorn

Joined: Jan 28, 2005
Posts: 2
Thank you very much. Moving the 'try' line down one line worked. I knew I had to handle the Exceptions in putting the elements into the array, but didn't think about the declaration. This was my first posting and I am sorry for my ignorance in posting. I will do better in the furure. thanks foe the tip in fixing the problem and posting etiquette.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't figure out why variable isn't recognized
 
Similar Threads
StackOverflowError
endless loop
Need some help with Java program Airline.java
I cannot fix this program! if anyone can help!
accessing non-static method from static context