tags:

views:

101

answers:

3

have two strings, String1 = hello String2 = world, I want to call a class Hello and send to the two strings. The class should return a boolean value and a string. If the boolean is true it should do the followig:

 System.out.println("Hello to you too!");

Can someone help me out with this code?

+1  A: 

First, a terminology problem: you cannot "call a class." You can call a method on a class, such as:

someObject.someMethod(string1, string2);

More to the point, you can't return two different values from a method. You could certainly store two different values in the object and return them from different methods, though. Perhaps a class like:

public class Foo {
    protected boolean booleanThing;
    protected String stringThing;

    public void yourMethod(String string1, String string2) {
        // Do processing
        this.booleanThing = true;
        this.stringThing = "Bar";
    }
    public String getString() {
        return this.stringThing;
    }
    public boolean getBoolean() {
        return this.booleanThing;
    }
}

Which would be used as:

someObject.yourMethod(string1, string2);
boolean b = someObject.getBoolean();
String s = someObject.getString();

Having said all that, though, this may not at all be the best way to solve your actual problem. Perhaps you can explain better what you're trying to accomplish. Perhaps throwing an Exception is better than trying to return a boolean, or perhaps there's another solution entirely.

The more detail we have, the better.

VoteyDisciple
A: 

You should review your definition of classes but for now I'll assume this is what you meant, comment if this isn't what your looking for:

public class Hello {
private final String first;
private final String second;

public static void main(String[] args) {
    String s1 = "Hello";
    String s2 = "World";
    Hello h = new Hello(s1,s2);
    if(h.isHelloWorld()) {
        System.out.println("Hello to you too!");
    }
}
private Hello(String first, String second) {
    this.first = first;
    this.second = second;
}

private boolean isHelloWorld() {
    return (first.equals("Hello") && second.equals("World"));
    //If that scares you then do this instead: 
    /**
    if(first.equals("Hello") && second.equals("World") {
        return true;
    } else { return false; }
    **/
}
}

When you run this program it will always print "Hello to you too!", if you change s1 or s2 it won't print anything.

Valchris
A: 
public class Hello{

  boolean val = false;
  String str = "";

  public Hello(String a, String b){
   if(a == "hello" && b == "world"){
     this.val = true;
     this.str = "hello to you too";
   }
 }
 public static void main(String args[]){
   String a = "hello";
   String b = "world";
   Hello hello = new Hello(a,b);
   if(hello.val == true)
      System.out.println(hello.str);
 }
}
Orbit
Did you type that in MS Word or something? Why the capital `Public` and `Class`? :)
Kirk Woll
uh... blaming that one on the brownies.
Orbit
Function isn't a keyword in Java... (-1)
Platinum Azure
This looks like a mix between Java and JavaScript. I recommend either deleting this answer, or edit it into valid Java. The indentation makes my eyes bleed as well!
Bart Kiers
edited into valid java, please repair my shattered reputation! :) (i even fixed the indentation)
Orbit
@Brandon, sorry, I won't remove the downvote since I don't think your example is all that good. You really shouldn't compare Strings using `==`. If you want to know why, search SO or Google for the answer.
Bart Kiers