Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: ArrayList Boolean Issue

  1. #1
    Junior Member kite98765's Avatar
    Join Date
    Jan 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Red face ArrayList Boolean Issue

    Hello everyone!

    I was busy writing an application program with mulitple classes, and it deteremines if a applicant is approved or not is like an application database. I was running through the first two options, and noticed that a boolean was returning incorrectly. I know this is simple, but the coding looks fine to me. Here's the ApplyList class code that puts everything an applicant says into ArrayLists:
     import java.util.ArrayList;
     
    public class ApplyList {
    		Applicant app = new Applicant();
    		static ArrayList <String> FirstName = new ArrayList <String>();
    		static ArrayList <String> LastName = new ArrayList <String>();
    		static ArrayList <String> SSN = new ArrayList <String>();
    		static ArrayList <String> Approved = new ArrayList <String>();
    		static String temp;
    		static String temp1;
    		static String temp2;
    		static String temp3;
     
     
    	public void List() {
     
    			FirstName.add(Applicant.Firstname);
    			LastName.add(Applicant.Lastname);
    			SSN.add(Applicant.SSN);
    			if(Applicant.pending)
    				Approved.add("Approved");
    			else
    				Approved.add("Denied");
     
     
    		for(int q = 0; q <FirstName.size(); q++){
    			System.out.println((q + 1) +". " + FirstName.get(q) + " " +
    					LastName.get(q) + " " + SSN.get(q) + " " + Approved.get(q));
    			}


    Here's the Applicant Class, where the information is inputted:
     import java.util.*;
     
     
    public class Applicant {
     
    	public static String Firstname;
    	public static String Lastname;
    	public static String SSN;
    	public static String Married;
    	public static boolean pending;
    	int income, age, approval;
    	public static int run = 0;
     
     
    	public void App(){
    		Scanner indata = new Scanner(System.in);				
     
    		System.out.println("Please enter your first name: ");
    		Firstname = indata.next();		
    		System.out.println("Please enter your last name: ");
    		Lastname = indata.next();
    		System.out.println("Please enter your Social Security Number: ");
    		SSN = indata.next();
    		System.out.println("Please enter your yearly income: ");
    		income = indata.nextInt();
    		System.out.println("Please enter your marital status(Y/N): ");
    		Married = indata.next();
    		System.out.println("Please enter your age: ");
    		age = indata.nextInt();
     
    		if(Married.equals("y") || Married.equals("Y")){
    		income += 5000;
    		}
     
    		approval = (income / 1000)/ age;
     
    		run++;
    		}
     
    	void Approve(){
    		App();
    		pending = Yes();
    		}
     
     
    	public boolean Yes(){
    			return (approval >= 1.0);
    		}
     
    }

    And finally here's the main class that runs everything:
     import java.util.*;
    public class ApplicationCenter {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner indata = new Scanner(System.in);
    		ApplyList list = new ApplyList();
    		Applicant app = new Applicant();
    		int selection = 0;
    		String choice;
     
    		do{
    			System.out.println("1. Add an applicant");
    			System.out.println("2. Search applicants by Social Security Number");
    			System.out.println("3. View a list of all applicants");
    			System.out.println("4. View a list of approved applicants");
    			System.out.println("5. View a list of declined applicants");
    			System.out.println("6. Exit the Application Database");
    			selection = indata.nextInt();
     
    			switch(selection){
    			case 1 : System.out.println("You've decided to fill out an application.");
    					System.out.println();
    					app.App();
    					list.List();
    					System.out.println();
    					break;
    			case 2 : System.out.println("You've decided to search for an applicant by Social Security Number.");
    					System.out.println();
    					System.out.println("Please enter the Social Security Number you would like to search: ");
    					choice = indata.next();
    					int location = Collections.binarySearch(ApplyList.SSN, choice);
    					if(location >= 0){
    						System.out.println("Applicant found: ");
    						System.out.println((ApplyList.FirstName.get(location)) + " " + 
    							(ApplyList.LastName.get(location)) + ", " + ApplyList.SSN.get(location) + 
    							", Status: ");
    						if("Y".compareTo(ApplyList.Approved.get(location)) == 0){
    							System.out.println("Approved");
    						}
    					}
    					System.out.println();
    					break;
    			case 3 : System.out.println("You've decided to view a list of all applicants.");
    					for(int q = 0; q < ApplyList.FirstName.size(); q++){
    						System.out.println((q + 1) +". " + ApplyList.FirstName.get(q) + " " +
    						ApplyList.LastName.get(q) + " " + ApplyList.SSN.get(q) + " " + ApplyList.Approved.get(q));
    					}
    					break;

    Thanks in advance for any help you can give.


  2. #2
    Member
    Join Date
    Aug 2009
    Posts
    53
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: ArrayList Boolean Issue

    Your structure seems a little confusing.
    First of, you should generaly aim to avoid using static fields and objects. While it is usefull at times, OOP is exactly aiming to avoid this as much as possible.
    A solution could be include storing each application in an Arraylist object, like this ArrayList<Applicant>.
    As far as I can see, the only place the applicant is stored is in the app field in ApplicationCenter class. So if you use selection one, you overwrite existing data in that field.

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. boolean value in a simple loop (do-while)
    By chronoz13 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 18th, 2009, 12:05 AM
  3. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  4. [SOLVED] Modification of Boolean function in Java program
    By lotus in forum Java SE APIs
    Replies: 4
    Last Post: July 10th, 2009, 10:15 AM
  5. Java operaton on boolean varibles
    By big_c in forum Java Theory & Questions
    Replies: 5
    Last Post: May 12th, 2009, 04:40 AM