0

I'm still learning the basics of coding in Java and have moved onto using methods which has thrown me. My issue is nothing is being returned to the console and I have no idea why. Here's my code:

public class MethodsPractice {

public int returnInteger (int num1, int num2){
    return num1 + num2;
}

public String[] upperCaseString(String[] strings){
    String[] upperStrings = new String[strings.length];
    for (int i = 0; i < strings.length; i++){
        upperStrings[i] = strings[i].toUpperCase();
    }
    return upperStrings;
}

public static void main(String[] args) {
    MethodsPractice myMethods = new MethodsPractice();

    int result = myMethods.returnInteger (10,20);
    String[] names = {"Bob", "Alex", "Luke"};
    String[] newNames = myMethods.upperCaseString(names);

   }

}
  • 1
    To show something in the console is actually an instruction (`System.out.Println` or something like that) . You have no such instructions. – Pac0 Dec 30 '17 at 14:55
  • 2
    perhaps `System.out.println()` will help you ? – vapurrmaid Dec 30 '17 at 14:56
  • @Pac0 am I wrong in thinking that 'return' should return what I specify in the console? – Mike Foski Dec 30 '17 at 16:03
  • 1
    @MikeFoski yes. Depending on your system and how you are calling your java program, it *might* show the return valule at the end of the program. But that's not actually your program doing it, only the things wrapped around (your operating system / shell / java launcher). Also, **your `main` method doesn't return anything** . Maybe this is what you are confused about. The values returned by any other function / method in your program are not the value returned by the program, which is the one returned by `main`. – Pac0 Dec 30 '17 at 16:25

4 Answers4

0

Simply put, you just aren't putting anything to console. Your code creates a new object, does some math, then creates an array of strings, but it never does anything with those newly created objects, for ex: If you wanted to print the result of the math function you need to use: System.out.println(result);

Java (mostly)does not print anything to console you don't tell it to.
System.out is the stream where you can print to console.
Of course, println is not the only function :) https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#out

  • Thanks for the answer, it was my understanding that putting 'return' would show what I was wanting in the console? – Mike Foski Dec 30 '17 at 15:46
  • No, return in java means you want to return an object to the class/function that called it. So if I return a string, the other class can have the string object and use it. Here is the doc link: https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html – Matthew Parks Dec 31 '17 at 15:38
0

upperCaseString just returns the a new array, which your main method saves to the newNames variable, and then does nothing with it. If you want to print it, you would have to do so explicitly, e.g.:

System.out.println(Arrays.toString(newNames));
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

To see content in the console, you need to call the methods from System.out like

  • print() : show content
  • println() : show content and go to next line

To easily print arrays, there is tips like :

String[] newNames = myMethods.upperCaseString(names);
System.out.println(Arrays.toString(newNames));

The classic way to print array will be with for (or foreach loop)

for(int i = 0; i<newNames.length ; i++) // for loop
    System.out.print(newNames[i] + ", ");

 for(String str : newNames) // foreach loop
    System.out.print(str + ", ");
azro
  • 53,056
  • 7
  • 34
  • 70
-1

You have to add an Console Output with:

System.out.print(); //without new line

or

System.out:println(); //starts a new line after output

For example:

public class MethodsPractice {

public int returnInteger (int num1, int num2){
    return num1 + num2;
}

public String[] upperCaseString(String[] strings){
    String[] upperStrings = new String[strings.length];
    for (int i = 0; i < strings.length; i++){
        upperStrings[i] = strings[i].toUpperCase();
    }
    return upperStrings;
}

public static void main(String[] args) {
    MethodsPractice myMethods = new MethodsPractice();

    int result = myMethods.returnInteger (10,20);
    String[] names = {"Bob", "Alex", "Luke"};
    String[] newNames = myMethods.upperCaseString(names);
    System.out.print(name[0]);
    System.out.println(name[1]);
    System.out.print(name[2])
   }

}

In the Consol will output:

BobAlex

Luke

Moritz
  • 139
  • 1
  • 10
  • PS: When you type sysout and press then STRG+SPACE it will autogenerate System.out.println() – Moritz Dec 30 '17 at 15:02