简体   繁体   中英

How to print something out and return another thing from a method in Java?

So I've got a method in Java that does some things with strings. At the end of the method, I do:

System.out.println(array[i-2]);
return(array[i-3]);

Basically, in this method I work with an array of strings. At the end of it, I have to print out one value, and return another value. Now I need to create a main method in which I will let the user input things as long as he doesn't input an empty row, then call this method, pass it that string, make the method do its work with the string and write out (print out) both of these values in the console (array[i-2] and array[i-3]). Lets say my method is named "StringFormating". How can I do this in main method? I've tried doing this:

Scanner input = new Scanner();
String result="";
do{
 result=input.nextLine();
 }while(!result.isBlank());

and then doing something like System.out.println(StringFormating(result));

but it just gives me null references and I still don't understand how to actually print out both of those values in the console. Any help, please?

I think you are trying to ask about Concatenation. There are different ways to do this in Java it just depends on the source and use of the data. The stringbuilder would work with this. You can add new segments of data to the string builder and then print out its contents at the end.

Just a Note... Taking data in from a user endlessly can be dangerous and throw overflow. Of course no one will do this if your just doing a school project but in deploying products you need to make sure the user cannot break your code and this could cause a possible vulnerability.

I'm not sure if I understand you correctly but this might be solution.

Scanner input = new Scanner();
StringBuilder sb = new StringBuilder("");
while(!input.nextLine().isBlank()){
  sb.append(StringFormatting(input.nextLine()));
}
String result = sb.toString();
System.out.println(result);

I'm not sure I'm understanding your problem correctly, but it might be due to the do while. With a do while you always execute the body of the do before checking the condition in the while , so if you have an empty input you are going to save it in result . You might try doing this:

while(!input.nextLine().isBlank()){
    result = input.nextLine();
}

In addition to this, I would recommend you writing the name of the method with the first letter lowercase, this way stringFormatting , as it is a Java naming convention.

If I understood your requirement correctly, you need something like

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String result = "";
        do {
            System.out.print("Enter your string: ");
            result = input.nextLine();
            if (!result.isBlank()) {
                System.out.println(stringFormating(result));
            } else {
                System.out.println("Goodbye!");
            }
        } while (!result.isBlank());
    }

    static int stringFormating(String str) {
        String[] data = null;
        int n = 0;
        if (str != null) {
            data = str.split(",");
        }
        if (data.length == 2) {
            System.out.println(data[0]);
            try {
                n = Integer.parseInt(data[1]);
            } catch (NumberFormatException e) {
                System.out.println("Error occured while processing data.");
            }
        }
        return n;
    }
}

A sample run:

Enter your string: abc,200
abc
200
Enter your string: xyz,40
xyz
40
Enter your string: 
Goodbye!

Feel free to comment in case of any doubt/issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM