Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Why is it saying the object is not in the ArrayList? (java)
New Posts  All Forums:Forum Nav:

Why is it saying the object is not in the ArrayList? (java)

post #1 of 4
Thread Starter 
Sorry for all the questions but I am getting frustrated. Just to be brief I create an account using the add method, then when it loops back around I want to change one of the components of that same account (password or status). I type in the exact same thing but it says it cant find the object. So I used the size method and it returns a 3 saying those 3 objects are there in the list. Please help me I am not trying to cheat just having lots of trouble, writing code from scratch.

Main Method:

import java.io.*;
import java.util.*;

public class Arraylist1
{

public static void main(String args[])
{
String activity;
System.out.println("Welcome to the Employee Security Program(ESP)");

do
{
Scanner kb = new Scanner(System.in);
System.out.println();
System.out.println("Please enter your activity: A = add, C = change, D = delete, P = print the file, X = exit ESP");
System.out.println();
System.out.print("Activity: ");
activity = kb.nextLine();
activity.toUpperCase();

LoginID obj2 = new LoginID(activity);
obj2.addMethod();
obj2.changeMethod();
}
while(!activity.equals("X"));

}

}


LoginID method:

import java.io.*;
import java.util.*;

public class LoginID
{
ArrayList obj = new ArrayList();
Scanner kb = new Scanner(System.in);
String ch;
String pass;
String stat;
String log;
String act;
int index;
int x;
int ind1;
int ind2;

public LoginID(String a)
{
act = a;
}

public void addMethod()
{
if(act.equals("A"))
{
System.out.print("Login? "); //typing in over
log = kb.nextLine();
System.out.print("Password? "); //typing in 000
pass = kb.nextLine();
System.out.println("Employee? "); //typing in Y
stat = kb.nextLine();

obj.add(log);
obj.add(pass);
obj.add(stat);

}
}

public void changeMethod()
{
if(act.equals("C"))
{
System.out.print("Login? "); // typing in over
log = kb.nextLine();
System.out.print("Password? "); //typing in 000
pass = kb.nextLine();

ind1 = obj.indexOf(log);
ind2 = obj.indexOf(pass);
System.out.println(ind1+" " +ind2); //I am checking what index it is locating, says they are not there, returning -1

if(ind2 - ind1 == 1) // set it up so that login is always one index before password, fails if statement everytime
{
System.out.print("Please choose an option: P = change password, S = change employment status");
ch = kb.nextLine();
ch.toUpperCase();

if(ch.equals("P"))
{
System.out.println();
System.out.print("New Password: ");
pass = kb.nextLine();
obj.set(ind2, pass);
}

if(ch.equals("S"))
{
System.out.println();
System.out.print("Employed? Y/N: ");
stat = kb.nextLine();
obj.set(ind2+1, stat);
}
}
else
System.out.println("Login and/or password are incorrect");
}
}


}

I put some comments to maybe help you see what I am doing if you have questions ask.
Eres
(15 items)
 
  
CPUMotherboardGraphicsRAM
intel i7 930 (4Ghz) asus rampage iii extreme SLI gtx 480's ddr3 corsair domintator (1600mhz) 6gb 
Hard DriveOptical DriveCoolingCooling
corsair 32GB SSD/ 1tb 7200rpm storage its a 20 dollar piece of junk EK supreme HF full nickel (cpu) EK-FC480 GTX acetal/ nickel (cards) 
OSMonitorPowerCase
windows 7 premium HP 2159m LCD 1080p antec HCP-1200 Corsair 800D 
  hide details  
Reply
Eres
(15 items)
 
  
CPUMotherboardGraphicsRAM
intel i7 930 (4Ghz) asus rampage iii extreme SLI gtx 480's ddr3 corsair domintator (1600mhz) 6gb 
Hard DriveOptical DriveCoolingCooling
corsair 32GB SSD/ 1tb 7200rpm storage its a 20 dollar piece of junk EK supreme HF full nickel (cpu) EK-FC480 GTX acetal/ nickel (cards) 
OSMonitorPowerCase
windows 7 premium HP 2159m LCD 1080p antec HCP-1200 Corsair 800D 
  hide details  
Reply
post #2 of 4
Honestly that code is so hard to read. If you could edit your post to preserve indentation it would be hugely helpful.
Balder
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k @ 4.4ghz Gigabyte Sniper 3 (z77) EVGA 980ti SC+ ACX 2.0 16GB (2x8) Corsair Vengeance - 1600mhz 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 (512MB) 4 x WD black 1TB LG Blueray Burner Corsair H100 
OSMonitorKeyboardPower
Windows 10 x64 Pro NEC Multisync 3090wqxi (2560x1600) Corsair k90 Corsair AX1200i 
CaseMouseAudioAudio
Corsair 600t white Naos 8200 Sennheiser HD 650 Zero DAC / Onkyo 875 
  hide details  
Reply
Balder
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k @ 4.4ghz Gigabyte Sniper 3 (z77) EVGA 980ti SC+ ACX 2.0 16GB (2x8) Corsair Vengeance - 1600mhz 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 (512MB) 4 x WD black 1TB LG Blueray Burner Corsair H100 
OSMonitorKeyboardPower
Windows 10 x64 Pro NEC Multisync 3090wqxi (2560x1600) Corsair k90 Corsair AX1200i 
CaseMouseAudioAudio
Corsair 600t white Naos 8200 Sennheiser HD 650 Zero DAC / Onkyo 875 
  hide details  
Reply
post #3 of 4
you can wrap code in [ code ] tags to make it easier to read. The posting is very incomplete, I don't even see your array list declaration or usage. And there is a bit of code smell here but I'll be nice and pretend that's out of scope.

I"m guessing that somewhere you have,

Code:
List<LoginID> theArrayList = new ArrayList<LoginID>();
theArrayList.add(new LoginID("john smith", "password");
theArrayList.add(new LoginID("john smith2", "password2");

// and later on you're doing,

LoginID checkForThisInList = new LoginID("john smith", "password") // from the user
boolean inList = theArrayList.contains(checkForThisInList); // expecting true, but always false.
// (contains says indexOf() >= 0 under the hood)

If my guesses are correct and that's like what you have, it is not working because you also need to @Override both equals() and hashCode() in the LoginID class so the comparison will succeed. Under the hood, indexOf() essentially does,

Code:
public int indexOf(Object o)  {
   int result = -1;
   for (int i = 0, n = this.underlyingCollection.size(); i < n; ++i)  {
       Object oneInList = this.underlyingCollection.get(i);
       if (oneInList.equals(o))  {
           result = i;
           break;
       }
    }
    return result;
}
^ that's obviously not exact, you can check the source code of rt.jar to see the implementation of indexOf in AbstractCollection. But essentially it uses .equals() to compare each one in the list. You can override equals() to define how it should compare one object to the other.

A correct implementation of equals is a lot more complex than you'd expect, you have to check for null, reference equality (obj == obj2), the same type, and then your custom compairson logic based on a composition of the object's properties. You'll need to google "java equals and hashCode" and read up quite a bit. If you provide one override, you should always provide the other. hope that helps
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
post #4 of 4
Code:
do
{
Scanner kb = new Scanner(System.in);
System.out.println();
System.out.println("Please enter your activity: A = add, C = change, D = delete, P = print the file, X = exit ESP");
System.out.println();
System.out.print("Activity: ");
activity = kb.nextLine();
activity.toUpperCase();

LoginID obj2 = new LoginID(activity);
obj2.addMethod();
obj2.changeMethod();
}
while(!activity.equals("X"));
if you look at your loop, you are initiating a NEW loginiD everytime the loop goes around and accepts a command...

I have not implemented any of this into Java to test, just trying to read through your code while in class, but it seems that your overwriting the object every time the loop comes around?

EDIT: After looking at your code more, it maybe does make sense... but I'm a bit foggy on your object orientation...

You should try making a class of just your users id... so this object contains things like the user type, status, login and pw... and then store these objects in an array, or arraylist or however you want to manage that data.

Right now your variables and control over them seem very clustered and all over the place... try planning out your data and what classes you may need...

Class - user
-ID
-login
-pw
-status
-permissions?

+change pw
+change status
+other data retrevial and changes?

class - Authenticate
+validation
+?

then this can all be handled with very minimal code in the main, by either accessing the users list, and/or calling methods of action

Not fully indepth on your scope of your project, but hopefully this will help?

Cheers
Edited by Sidious - 1/24/11 at 6:24am
Title System
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X4 965 Gigabyte GA-MA785GMT-UD2H Sapphire HD 6850 1GB DDR3 Crucial Ballistix Tracers 4GB 
Hard DriveOptical DriveCoolingOS
Kingston SSD 64GB BENQ DVD-RW Custom W/C Kit Windows 7 Professional x64 
MonitorKeyboardPowerCase
24" LG LED Logitech Wave Corsair 700W Modular Coolmaster Half-X 
MouseAudio
Logitech MX Revolution 5.1 Surround / Studio Recording Headphones 
  hide details  
Reply
Title System
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X4 965 Gigabyte GA-MA785GMT-UD2H Sapphire HD 6850 1GB DDR3 Crucial Ballistix Tracers 4GB 
Hard DriveOptical DriveCoolingOS
Kingston SSD 64GB BENQ DVD-RW Custom W/C Kit Windows 7 Professional x64 
MonitorKeyboardPowerCase
24" LG LED Logitech Wave Corsair 700W Modular Coolmaster Half-X 
MouseAudio
Logitech MX Revolution 5.1 Surround / Studio Recording Headphones 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Why is it saying the object is not in the ArrayList? (java)