+ 1

How to use a void method in Java?

package tombtombbekerekit; import java.util.Scanner; public class TombTombbeKerekit { public static int round(int osszeg) { int last_Digit = osszeg % 10; if(last_Digit < 3) return osszeg - last_Digit; else if(last_Digit > 7) return osszeg + (10 - last_Digit); else return osszeg - (last_Digit) + 5; } public static void roundSelf(int [] numbers) { int [] array = numbers; for (int i = 0; i < array.length; i++) return; } public static int [] roundNew(int [] numbers) { int [] newArray = numbers; for (int i = 0; i < newArray.length; i++) { newArray[i] = round(newArray[i]); } return newArray; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Kérem az összegek számát: "); int size = sc.nextInt(); System.out.println("Kérem az összegeket: "); int [] array = new int[size]; for (int i = 0; i < array.length; i ++) { array[i] = sc.nextInt(); } int [] ujTomb = roundNew(array); System.out.println("Kerekítve: "); for (int i = 0; i < ujTomb.length; i++) System.out.println(ujTomb[i]); } } I wrote this code to round the elements of an array by a custom rounding rule. There are 3 methods, round is used to round a number by a custom rule, roundSelf is for modifying the input array and roundNew for making a new array with the same size of the input array and the program returns to this method. The problem is I don't understand what is the purpose of the roundSelf method because it's void and haven't got a return value so I can't call it from the main and can't call from roundNew method. Are there any suggestion to call it from the main program?

17th Mar 2019, 10:45 AM
Tibor Tóth
Tibor Tóth - avatar
5 odpowiedzi
+ 5
Void means that the method returns nothing so you dont need to use "return" although you can still use return to exit a void method at certain points.
17th Mar 2019, 11:22 AM
D_Stark
D_Stark - avatar
+ 3
In this case you can call the roundSelf method but it does not do anything (at least based on your code). A static void method might be useful to print some output or to raise some error. A void method (not static) is typically a setter method in which case you instantiate your class and change some instance variable by calling the void method, and you don't return any result.
17th Mar 2019, 11:36 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Solved. I can call the array to the void method with: roundSelf(array);
17th Mar 2019, 11:20 AM
Tibor Tóth
Tibor Tóth - avatar
0
The task is the next. The main program asks for the size of an array then asks for numbers. After that calls a method which is freely choosable. The round method rounds the given numbers by a custom rule which is used in hungarian bank systems. We have 5, 10, 20, 50, 100 and 200 coins now but we has 1 and 2 before they are retired from the money system. The void method's purpose to modify the input array. The roundNew method's purpose to make a new array with the same size as the input array, call the round method to its elements and then return to the new array and the input array must be leave unchanged.
17th Mar 2019, 7:54 PM
Tibor Tóth
Tibor Tóth - avatar
0
I find the answer...copy the input array and call the copied array so the original will leave untouched.
17th Mar 2019, 10:51 PM
Tibor Tóth
Tibor Tóth - avatar