Jump to content

Java Help Please

Go to solution Solved by jslowik,

Additionally you're never printing the value from that method. Your print line should be similar to:

System.out.println(SecondClassObject.simpleMessage(name));

And just for giggles, writing your second class like the following is a few less keystrokes:

public String simpleMessage(String name){ //this is taking the varible sent from the call method
        name += " Is a god";
        return name;
    }

 

Hey, Started learning a bit of Java and ive been messing about with calling methods in another class and i am trying to send a string from one class to another, alter it and send it back to the main class. So i have two classes.... here they are...

 

Main Class:

 public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
        
        System.out.println("Enter your name : ");
        String name = input.nextLine();
        //This is calling another class
        SecondClass SecondClassObject = new SecondClass();
        SecondClassObject.simpleMessage(name);//name is entering the variable (argunment)for other method in other class.
        
        System.out.println(name);
    }


 

Other Class :

public String simpleMessage(String name){ //this is taking the varible sent from the call method
        
        name = name +(" Is a god");
       
        
       }

 

 

 

I want to return "name" to the main method and print.

 

Any help would be appreciated. Thanks 

 

Link to comment
https://linustechtips.com/topic/563247-java-help-please/
Share on other sites

Link to post
Share on other sites

Can't you just access the variable by doing something like NameOfTheScript.NameOfTheVariable? You can do this in for example C# (In Unity3D) so I would suggest trying it out.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
https://linustechtips.com/topic/563247-java-help-please/#findComment-7407803
Share on other sites

Link to post
Share on other sites

Your method isn't returning a value. Add the return statement.

public String simpleMessage(String name){ //this is taking the varible sent from the call method
    name = name +(" Is a god");
    return name;
}

Note that you can shorten it to

public String simpleMessage(String name) {
    return name + " Is a god"; // brackets also aren't doing anything so I removed them too
}

 

Link to comment
https://linustechtips.com/topic/563247-java-help-please/#findComment-7408225
Share on other sites

Link to post
Share on other sites

Additionally you're never printing the value from that method. Your print line should be similar to:

System.out.println(SecondClassObject.simpleMessage(name));

And just for giggles, writing your second class like the following is a few less keystrokes:

public String simpleMessage(String name){ //this is taking the varible sent from the call method
        name += " Is a god";
        return name;
    }

 

Link to comment
https://linustechtips.com/topic/563247-java-help-please/#findComment-7408241
Share on other sites

Link to post
Share on other sites

1 hour ago, Adam7868 said:

it will help keep the input on one line

Probably mostly personal preference. Don't worry too much about it as you won't be writing console style applications for very long.

 

Just to explain a little, println adds a line break after the printed line, and print doesn't. Some examples here...

 

System.out.println("This is a line");
System.out.println("This is a new line");

will look like:

This is a line

This is a new line

 

Where

System.out.print("This is a line");
System.out.println(" This isn't a new line");

Will look like:

This is a line This isn't a new line

Link to comment
https://linustechtips.com/topic/563247-java-help-please/#findComment-7409205
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×