0

My goal is to print out all the words in the array and make sure they are in a string.( In order). So far I'm at:

public class Freddy {
    private String[] words = new String[]{"Hello", "name", "is", "Bob"};


    public String toString() {
        for (int i = 0; i <words.length; i++)
            System.out.println(words[i]);

        return null;
    }

    public static void main(String[] args) {
    System.out.println();//Not sure not to print out here. When I say print(words) it gives error "Error:(16, 24) java: non-static variable words cannot be referenced from a static context"
    }
}

Thanks!

bob9123
  • 725
  • 1
  • 9
  • 31
  • Your `toString()` method you have should return type of `void` if you plan on returning nothing (mind you, that wouldn't be the usual approach to implementing `toSting()`). Instead you should actually return a `String`. – jnd Feb 03 '15 at 01:24
  • So instead of System.out.println(words[i]); I should do return(words[i]); – bob9123 Feb 03 '15 at 01:30
  • You can use the `StringBuilder` class. The first line of the method can be `StringBuilder stringBuilder = new StringBuilder();` So instead of `System.out.println(words[i]);` you would put `stringBuilder.append(words[i]);` Then you would `return stringBuilder.toString();` – jnd Feb 03 '15 at 01:32
  • 1
    I believe this has been answered before please see this link: [Click Here][1] [1]: http://stackoverflow.com/questions/5283444/convert-array-of-strings-into-a-string-in-java – SeekingAlpha Feb 03 '15 at 01:39

3 Answers3

2

You error is because you are trying to access to instance variable in static method(main) without creating the instance.

To fix your error:

  1. make your array static:

    private static String[] words = ...//make it static

or

  1. create an instance before you get access to it:

    System.out.println(new Freddy());//This will call it'stoString()method.

In order to convert an array to String, use Arrays#toString is a better way:

public String toString() {
    return Arrays.toString(words);//Convert the array into string, toString returns null is a bad habbit.
}

Check How to convert an int array to String with toString method in Java for more details

Community
  • 1
  • 1
JaskeyLam
  • 15,405
  • 21
  • 114
  • 149
  • @EmanOdame I think you have to do it by yourself, if your string is easy, you may simplily use :"Arrays.toString(words).replace(","," ")".replace("{|}","") – JaskeyLam Feb 03 '15 at 01:46
  • @EmanOdame, try this: `Arrays.toString(words).replaceAll(","," ")".replaceAll("{|}","").replaceAll("^\\[|\\]$", "");` – JaskeyLam Feb 03 '15 at 03:20
1

A static method can only use static fields from class. Your main() method is static, then your method toString() and array String[] words must be static too.

However, in your case, you should follow the way that @Arvind show.

Antony Dao
  • 425
  • 2
  • 14
0

Problem is main() is static, whereas words is an instance variable, meaning it cannot exist without the instance of Freddy.

Simply pass the instance of class:

System.out.println(new Freddy());