Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Can you return more then one primitive in a method? (java)
New Posts  All Forums:Forum Nav:

Can you return more then one primitive in a method? (java)

post #1 of 9
Thread Starter 
I needed to return 3 strings in a method, a login, password, and employment status and I was wondering if that was even possible. If you know any way please help me out Ill show you the method:

public String addMethod()
{
if(act.equals("A"))
{
System.out.print("Login? ");
log = kb.nextLine();
System.out.print("Password? ");
pass = kb.nextLine();
System.out.println("Employee? ");
stat = kb.nextLine();

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

But it says the last two are unreachable so I was wondering how I could do this.
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 9
It sounds like you are missing a "User" class for encapsulation of those 3 fields.

but to simply answer your question you can return a String[] array. And String is not a primitive type. String[] array = new String[](3); array[0] = x; array[1] = y; array[2] = z; ... i wouldn't do that however, you should make a User class and set the properties. Also if you're doing a string comparison on the stat field, probably add an enum with that set of values.

The last two are unreachable because the first return kicks you out of that method. In general it is usually preferred to have a single point of exit in any method, but that's not a hard rule - it depends. One thing that sometimes bothers me about java is you can only ever return one object or primitive. Sometimes it would be nice to return a tuple, but you end up having to create an encapsulating bean and return that, or modify input parameters... C# is a bit nicer in this regard, but some would argue returning more than one thing is a symptom of poor design.
Edited by lloyd mcclendon - 1/23/11 at 12:22pm
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 #3 of 9
Thread Starter 
Quote:
Originally Posted by lloyd mcclendon View Post
It sounds like you are missing a "User" class for encapsulation of those 3 fields.

but to simply answer your question you can return a String[] array. And String is not a primitive type. String[] array = new String[](3); array[0] = x; array[1] = y; array[2] = z; ... i wouldn't do that however, you should make a User class and set the properties. Also if you're doing a string comparison on the stat field, probably add an enum with that set of values.

The last two are unreachable because the first return kicks you out of that method. In general it is usually preferred to have a single point of exit in any method, but that's not a hard rule - it depends. One thing that sometimes bothers me about java is you can only ever return one object or primitive. Sometimes it would be nice to return a tuple, but you end up having to create an encapsulating bean and return that, or modify input parameters... C# is a bit nicer in this regard, but some would argue returning more than one thing is a symptom of poor design.
ok thank you
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 #4 of 9
hope that helps. if so please +rep as i need to sell some stuff

one thing, the line

if(act.equals("A"))

If act is null you'll get a null pointer exception. String comparisons against a literal ("a constant string in quotes") should always have the literal part on the left hand side.

if ("A".equals(act))

is safer in that it will never throw a null pointer exception. It is never a bad idea to check first that act is not null however.

Also string comparisons like that are bad in general - you're hiding domain logic into program code. You should at least use an enumeration, or data that is loaded in through an config file or database. For this project I doubt you'll want do all of that - at least use a type safe enum listing the types of actions.
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 #5 of 9
Thread Starter 
Quote:
Originally Posted by lloyd mcclendon View Post
hope that helps. if so please +rep as i need to sell some stuff

one thing, the line

if(act.equals("A"))

If act is null you'll get a null pointer exception. String comparisons against a literal ("a constant string in quotes") should always have the literal part on the left hand side.

if ("A".equals(act))

is safer in that it will never throw a null pointer exception. It is never a bad idea to check first that act is not null however.

Also string comparisons like that are bad in general - you're hiding domain logic into program code. You should at least use an enumeration, or data that is loaded in through an config file or database. For this project I doubt you'll want do all of that - at least use a type safe enum listing the types of actions.
no because act is a never null, it is a input variable from the user(inputed in beginning of program), so this won't be an issue. Plus this is not for a company or anything I am in AP computer science in high school so that won't be a requirement. But +1. Ill give you another plus one if you answer one more question, also I got rid of the returns and decided to do it an easier way. But thanks
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 #6 of 9
lol thanks. i'm all ears, go ahead.

one more thing, you're calling addMethod() even if act == "B" ... going through the expense of a method call to have it be a noop and just kick out right away. It looks like the if == A check should be around the outer call to addMethod to potentially save the call altogether if not required. but as always it depends on the broader context
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 #7 of 9
Thread Starter 
Quote:
Originally Posted by lloyd mcclendon View Post
lol thanks. i'm all ears, go ahead.

one more thing, you're calling addMethod() even if act == "B" ... going through the expense of a method call to have it be a noop and just kick out right away. It looks like the if == A check should be around the outer call to addMethod to potentially save the call altogether if not required. but as always it depends on the broader context
sorry I figured it out and yes I know thats why the if statement is wrapped around all the code in that method so it will call it yes, but won't execute any of the code. Nah I sent a a very very small part of my program I have all the little nooks and crannies sorted out.
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 #8 of 9
Thread Starter 
Sorry if I made myself sound dumb I am actually a good student and I make high A's and usually can do assigned programs quickly without trouble. Just got confused for a sec. but I think we all do . I am making a employer security program. Basically its just a login, you can add, change, delete, and print information. Also it has to verify you of all updates. And it includes a search by login majigure(hehe) which basically you search for a specific login and find it by login or sequence number or you can just display the whole file. And the last thing I have to do which I may need help is override the toString() method to format the output a specific way so if you could show that sweet. It has to be formated "Employee xxx has password yyy and is/is not currently employed".
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 #9 of 9
based on what you've posted i don't see an encapsulation class of those fields. The "User" object i was hinting at in the first post. essentially something like this will take care of it,

Code:
public class Employee  {
    private String name;
    private String password;
    private boolean isEmployed;  /* you may have a string or something to denote this
                                               * and then a method to calculate the isEmployed boolean
                                               */ based off of that.  idk

    public Employee()  {
        this(null, null, false);
    }

    public Employee(String name, String password, boolean isEmployed)  {
         super();
         this.name = name;
         this.password = password;
         this.isEmployed = isEmployed;
    }

    public String getName()  {
        return this.name;
    }
    public void setName(String name)  {
        this.name = name;
    }
   /* getters and setters for other fields ommited.
    * If you're setting up via constructors, you may not want to add the setters.
    * And/or you could remove the constructors and use setters.
    */

   public boolean isEmployed()  {
       return isEmployed; // if you have a string or something, add calculations
       // return "employed".equals(stringStatusOrSomething);
   }

   @Override
   public String toString()  {
      return MessageFormat.format("Employee {0} has password {1} and is currently{2} employed.", getName(), getPassword(), (isEmployed() ? "" : " not"));  // lots of ways you could do this... 
   }
}
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
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Can you return more then one primitive in a method? (java)