-
January 16th, 2018, 07:16 PM
#1
Method returning multiple values using an array.
I've been working through problems I found from when I was in school. Came across this one problem where I am a bit confused. The question states:
Implement a method named positive. The method accepts 3 integer parameters named: alfa, beta, gama.
The method returns the number of non-negative integers it received.
This is what I have so far:
Code:
public class MyMethods {
public int[] positive(int alfa, int beta, int gama){
int count=0;
int[] temp = new int[3];
if(alfa > 0){
count++;
temp[count-1] = alfa;
}
if(beta>0){
count++;
temp[count-1] = beta;
}
if(gama>0){
count++;
temp[count-1] = gama;
}
int [] value = new int[count];
for (int i=0; i<3; i++)
{
value[i] = temp[i];
}
return value;
}
}
In my main method I wrote this code to test it but the array does not print out:
Code:
import java.util.Scanner;
public class RunMyMethods {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
MyMethods mm = new MyMethods();
int a, b, g;
System.out.println("Enter 3 integers (positive or negative): ");
a = scan.nextInt();
b = scan.nextInt();
g = scan.nextInt();
System.out.println(mm.positive(a, b, g));
}
}
Am i supposed to be using a for loop instead in the positive method? if so how would I use that with the three values alfa, beta, gama? Or am i suppose to create a temporary array in each if statement and then copy and resize the original array? I'm probably over thinking this but I feel like i hit a dead end.
Last edited by rcheeks23; January 16th, 2018 at 09:09 PM.
Reason: updated my code
-
January 17th, 2018, 02:27 AM
#2
Re: Method returning multiple values using an array.
Put a breakpoint on the return statement of the positive method and check the values veriable. If this looks correct, then most likely the println method isn't able to print the contents of an array. You'll need to loop through the array and use println on each item.
-
January 17th, 2018, 02:32 AM
#3
Re: Method returning multiple values using an array.
Originally Posted by rcheeks23
Am i supposed to be using a for loop instead in the positive method? if so how would I use that with the three values alfa, beta, gama? Or am i suppose to create a temporary array in each if statement and then copy and resize the original array? I'm probably over thinking this but I feel like i hit a dead end.
In positive() you create an array, insert some values and return it. That's fine, now you want to print it.
Using a loop is one possibility. There are several ways. Here are a few,
An ordinary for-loop,
Code:
int[] result = mm.positive(a, b, g);
for (int i=0; i<result.length; i++) System.out.print(result[i] + " ");
System.out.println();
An enhanced for-loop
Code:
int[] result = mm.positive(a, b, g);
for (int n : result) System.out.print(n + " ");
System.out.println();
An enhanced for-loop (alternative)
Code:
for (int n : mm.positive(a, b, g)) System.out.print(n + " ");
System.out.println();
This actually also worked,
Code:
System.out.println(mm.positive(a, b, g));
only it didn't print what you expected.
If System.out.println() is called with an array object it will print the internal representation (*) of the object (which is what happened). You get something more in line with what you expect if you do,
Code:
System.out.println(Arrays.toString(mm.positive(a, b, g)));
Here the Arrays.toString() method produces a String representation of the contents of your array which you then print. The square brackets and the commas are added by Arrays.toString(). I'd say this printing alternative is best suited for testing purposes.
---
(*) System.out.println() gets the internal representation of an object by calling the toString() method of the object. The toString() method is available in every Java object because every Java class extends the Object class where toString() is defined. This includes arrays because they are objects too.
Last edited by wolle; January 18th, 2018 at 01:32 AM.
-
January 17th, 2018, 06:56 AM
#4
Re: Method returning multiple values using an array.
Norm
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|