0

looking to interpret this info better or additional resources.

class Main {
public static void main(String[] args) {
    FileReader file = new FileReader("C:\\test\\a.txt");
    BufferedReader fileInput = new BufferedReader(file);
      
    // Print first 3 lines of file "C:\test\a.txt"
    for (int counter = 0; counter < 3; counter++) 
        System.out.println(fileInput.readLine());
      
    fileInput.close();
doug22
  • 77
  • 1
  • 8
  • 2
    Hi @doug22 - you've already returned the array - `return values`. Depending what you want to print you do a for loop over the array and print out what you want? – Mr R May 25 '21 at 06:48
  • Does this answer your question? [What's the simplest way to print a Java array?](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – Thomas Kläger May 25 '21 at 06:54
  • I'm not completely sure what the question is but if you say 'the address of the array is printed', are you using ```System.out.println(values)```? If so, you have to use ```System.out.println(Arrays.toString(values))``` instead. – geanakuch May 25 '21 at 06:54
  • I'm guessing that you probably need to [override method 'toString'](https://www.geeksforgeeks.org/overriding-tostring-method-in-java/) in your classes, including `Address`, `Agency` and `Property`. – Abra May 25 '21 at 07:39
  • Have you read [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) ? – Abra May 25 '21 at 07:44
  • hi thanks, removed the extra code. I do have a toString in my other classes, is there a way i can have two toStrings? – doug22 May 25 '21 at 08:39

3 Answers3

1

What you interpreted as the address of the array was probably its hashcode, which is part of Object's default implementation of toString(). An array is a type of object, and any objects that don't implement their own toString() will show up this way when printed.

To print the contents, you'll have to loop over them. One way to do that would be

System.out.println(
    Arrays.stream(values).collect(Collectors.joining(","))
    );

One more remark: If you need a method to return several values, it's more idiomatic to return a Collection (or a subtype like List), because those are easier to work with in Java. That would also spare you having to loop over the data twice to determine the necessary size first.

OhleC
  • 2,821
  • 16
  • 29
0
public Property[] getPropertiesBetween(int minUsd, int maxUsd) {
    int counter = 0;
    int a = 0;
    for (Property land : properties.values()) {
        if (land.getPriceUsd() > minUsd && land.getPriceUsd() < maxUsd) {
            counter++;
        }
    }
    Property[] values = new Property[counter];
    for (Property land : properties.values()) {
        if (land.getPriceUsd() > minUsd && land.getPriceUsd() < maxUsd) {
            values[a] = land;
            a++;
        }
    }

   for(int i : values) {
      System.out.println(i);
    }


    return values; 
}
OhleC
  • 2,821
  • 16
  • 29
-2
import java.util.Arrays;      
public class ReturnArray1
    {  
     public static void main(String args[])  
     {  
        int[] a=numbers();           //obtain the array  
        for (int i = 0; i < a.length; i++) //for loop to print the array  
           System.out.print( a[i]+ " ");     
     }  
     public static int[] numbers()  
     {  
        int[] arr={5,6,2,8,3,1,4,7,9,0};  //initializing array  
        return arr;  
     }  

    }  

The method returns an array of integer types.

sudmong
  • 2,036
  • 13
  • 12