- Returning String Methods in Java?
- Return a String in Java
- Related Article — Java String
- How to Return a String in Java
- How to Return a String in Java?
- Method 1: Return a String in Java Without Using return Statement
- Example 1: Return a String Using System.out.println() Method
- Example 2: Return a String Using Static Java Method
- Method 2: Return a String in Java Using return Statement
- Conclusion
- About the author
- Farah Batool
- How to return string and int type in a method?
Returning String Methods in Java?
I’m in a beginning programming class, and a lot of this had made sense to me up until this point, where we’ve started working with methods and I’m not entirely sure I understand the «static,» «void,» and «return» statements. For this assignment in particular, I thought I had it all figured out, but it says it «can not find symbol histogram» on the line in the main method, although I’m clearly returning it from another method. Anyone able to help me out? Assignment: «You see that you may need histograms often in writing your programs so you decide for this program to use your program 310a2 Histograms. You may add to this program or use it as a class. You will also write a class (or method) that will generate random number in various ranges. You may want to have the asterisks represent different values (1, 100, or 1000 units). You may also wish to use a character other than the asterisk such as the $ to represent the units of your graph. Run the program sufficient number of times to illustrate the programs various abilities. Statements Required: output, loop control, decision making, class (optional), methods. Sample Output: Sales for October Day Daily Sales Graph 2 37081 ************************************* 3 28355 **************************** 4 39158 *************************************** 5 24904 ************************ 6 28879 **************************** 7 13348 ************* » Here’s what I have:
import java.util.Random; public class prog310t < public static int randInt(int randomNum) //determines the random value for the day < Random rand = new Random(); randomNum = rand.nextInt((40000 - 1000) + 1) + 10000; return randomNum; >public String histogram (int randomNum) //creates the histogram string < String histogram = ""; int roundedRandom = (randomNum/1000); int ceiling = roundedRandom; for (int k = 1; k < ceiling; k++) < histogram = histogram + "*"; >return histogram; > public void main(String[] Args) < System.out.println("Sales for October\n"); System.out.println("Day Daily Sales Graph"); for (int k = 2; k < 31; k++) < if (k == 8 || k == 15 || k == 22 || k == 29) < k++; >System.out.print(k + " "); int randomNum = 0; randInt(randomNum); System.out.print(randomNum + " "); histogram (randomNum); System.out.print(histogram + "\n"); > > >
Edit: Thanks to you guys, now I’ve figured out what static means. Now I have a new problem; the program runs, but histogram is returning as empty. Can someone help me understand why? New Code:
import java.util.Random; public class prog310t < public static int randInt(int randomNum) //determines the random value for the day < Random rand = new Random(); randomNum = rand.nextInt((40000 - 1000) + 1) + 10000; return randomNum; >public static String histogram (int marketValue) //creates the histogram string < String histogram = ""; int roundedRandom = (marketValue/1000); int ceiling = roundedRandom; for (int k = 1; k < ceiling; k++) < histogram = histogram + "*"; >return histogram; > public static void main(String[] Args) < System.out.println("Sales for October\n"); System.out.println("Day Daily Sales Graph"); for (int k = 2; k < 31; k++) < if (k == 8 || k == 15 || k == 22 || k == 29) < k++; >System.out.print(k + " "); int randomNum = 0; int marketValue = randInt(randomNum); System.out.print(marketValue + " "); String newHistogram = histogram (randomNum); System.out.print(newHistogram + "\n"); > > >
Return a String in Java
In Java, the prototype of a method must contain a return type always based on the data type specified in the declaration.
Below is the code block to explain the function of returning a string.
public class Main public static void main(String[] args) String s = doSomething(); System.out.println("Print the value from the function: " + s); > private static String doSomething() return "Hi,I am in doSomething Function"; > >
In the driver class above, there is a private function that returns a String value. The prototype of the doSomething method is also present above.
First, it has an access modifier private that tells the scope or the visibility of a function. A public or protected keyword defines visibility other than private .
The static keyword is optional; it means that the method is called without creating the driver class instance. So, the main function is always static that can be called directly without the name of the driver class.
The next value is the return type of the method; it states that the primitive data types, user-defined classes, or generic instances can be returned.
In our case, the string is the return type of the method. The compiler checks for the return type when the coder writes the program. It throws a compile-time error if the return type does not match the prototype given.
Next to it is the method’s name; it can be any name other than the pre-fixed keywords present in Java. The function name follows the set of the parameters passed.
The code block above has no parameters in the () parenthesis. But depending on our needs, we can give one or a set of parameters. Within the curly braces <> , defining the beginning and the end of the function is what’s often called a block .
There can be multiple statements present in the function block. The return statement must be the last. As the return type is a string, the return keyword is preceded with the String value present in » » double quotations.
The output of the code block is printed below.
Print the value from the function: Hi, I am in doSomething Function.
Rashmi is a professional Software Developer with hands on over varied tech stack. She has been working on Java, Springboot, Microservices, Typescript, MySQL, Graphql and more. She loves to spread knowledge via her writings. She is keen taking up new things and adopt in her career.
Related Article — Java String
How to Return a String in Java
A String is a group of characters used to store text data. In Java, methods are declared with their return types like int, double, String, and so on. More specifically, a string can be returned with or without a return statement, depending on your program requirements.
This post will illustrate how to return Java strings.
How to Return a String in Java?
There are two ways to return a String in Java:
We will now check out both of the mentioned methods one by one!
Method 1: Return a String in Java Without Using return Statement
The simplest way to return a string without the return statement is using the “System.out.println()” method. This Java method is utilized for printing the passed argument on the console.
Here, “s” represents the string that will be returned to the console.
See the examples below to have a better understanding of the concept.
Example 1: Return a String Using System.out.println() Method
First of all, create a string named “s” having the following value:
Then, we will return the created string using the “System.out.println()” method:
Example 2: Return a String Using Static Java Method
Here, first, we will create a static void function that utilizes the “System.out.println()” method:
static void sMethod ( ) {
System. out . println ( «The String is returned without Return Statement» ) ;
}
Now, we will call the “sMethod()” in main() to print the specified string on the screen:
The given output indicates that we have successfully returned a string using a static method:
Now, let’s head towards the second method!
Method 2: Return a String in Java Using return Statement
Another way to return a string is by using a “return” statement at the end of the method. When a programmer writes a program, the compiler examines the return type. If the return type is set as “String”, then the added string will be returned.
Here, the “return” keyword represents the added return statement that will return the String specified in the double quotes.
Example
In this example, we will create a String type static method that returns the following string:
Next, we will call our “sMethod()” in main() method, store the returned value in “s” variable, and print it on console using the “System.out.println()” method:
public static void main ( String [ ] args ) {
String s = sMethod ( ) ;
System. out . println ( s ) ;
}
We compiled all the simplest methods related to returning a String in Java.
Conclusion
There are two methods to return a String in Java: the “System.out.println()” method or the “return” statement. The System.out.println() method can be utilized simply in the main() method or in any user-defined static method. However, to use the return statement, you have to create a method with a “String” return type and specify the required string with the “return” keyword within the method definition. This post illustrated the methods to return a Java String.
About the author
Farah Batool
I completed my master’s degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.
How to return string and int type in a method?
Write a class for a video game character. The character should have a name, a type (scout, soldier, medic, etc.) and current health. Therefore it needs three attributes: String name, String type, int health This class should have the following methods: GameCharacter( String newName, String newType, newCurHealth ) Constructor that takes three inputs. changeHealth( int change ) A method that changes the health of the character. The character’s health will change by change amount, so it will go down if change is negative, and up if it’s positive. If the health goes below 0, changeHealth should return the String «Your character is dead».
Here is my code so far. Is there anything I can do to make it better? & a way to return a string in my second method?
public class GameCharacter < private String name; private String type; private int health; public GameCharacter(String newName, String newType, int newCurHealth)< name = newName; type = newType; health = newCurHealth; >public int changeHealth (int change) < if (change < 0)< return health - change; >else if (change > 0) < return health + change; >else if (health < 1)< // string that character is dead >> public static void main(String[] args) < GameCharacter Mario = new GameCharacter ("Mario", "Hero", 100); GameCharacter Luigi = new GameCharacter ("Luigi", "Sidekick", 100); GameCharacter Bowser = new GameCharacter ("Bowser", "Villian", 100); >>