Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Local Development Environments Exploring Your IDE Clean up this mess

I don't know what is wrong with my code after I run it, it is saying import java.util.*; suspect

import java.util.*;

public class Messy { public static void main(String[] args) { System.out.println("one"); System.out.println("two"); System.out.println("three"); System.out.println( "four");
System.out.println("five"); /* * Please comment out this line and * this line as well with a hotkey * that does multi-line commenting */ List <String> numberWords = Arrays.asList("six", "seven", "eight", "nine"); for (String numberWord: numberWords) { // Use the sout shortcut to write out numberWord; System.out.println(numberWord); } } }

Messy.java
import java.util.*;

public class Messy {
    public static void main(String[] args) {
        System.out.println("one");
        System.out.println("two");
        System.out.println("three");
        System.out.println("four");     
        System.out.println("five");
        /*
         * Please comment out this line and
         * this line as well with a hotkey
         *  that does multi-line commenting
         */
        List <String> numberWords = Arrays.asList("six", "seven", "eight", "nine");
        for (String numberWord: numberWords) {
          // Use the sout shortcut to write out numberWord;
            System.out.println(numberWord);
    }
  }
}
results.txt
one
two
three
four
five
six
seven
eight
nine

4 Answers

Andrew Winkler
Andrew Winkler
37,739 Points

Hello, what it is asking you to do is to refactor and use the smart imports, which will only import the utils that you need - List and Arrays. The java.util.* is not considered correct because it is not memory nor speed efficient.

Also, I'm not sure if you're there yet, but you need to fix the for loop to count where your System.out's leave off ( 5-10, you'll need to hand key "ten" in) It should look something like:

List<String> numberWords = Arrays.asList("six", "seven", "eight", "nine", "ten");
        for (String numberWord : numberWords) {
            System.out.println(numberWord);

Thanks, let me know if this doesn't help.

when I paste the code at the end of the challenge, it is still giving me a compiler error. when i run the code on eclipse it runs fine, i don't know why is not working here

Andrew Winkler
Andrew Winkler
37,739 Points
import java.util.Arrays;
import java.util.List;

public class Messy {
    public static void main(String[] args) {
        System.out.println("one");
        System.out.println("two");
        System.out.println("three");
        System.out.println("four");     
        System.out.println("five");
        /*Please comment out this line and
        this line as well with a hotkey that does multi - line commenting*/
        List<String> numberWords = Arrays.asList("six", "seven", "eight", "nine", "ten");
        for (String numberWord : numberWords) {
            // Use the sout shortcut to write out numberWord;
            System.out.println(numberWord);
        }
    }
}

What is the error code when you run this?

Andrew Winkler
Andrew Winkler
37,739 Points
import java.util.Arrays;
import java.util.List;

public class Messy {
    public static void main(String[] args) {
        System.out.println("one");
        System.out.println("two");
        System.out.println("three");
        System.out.println("four");     
        System.out.println("five");
        /*Please comment out this line and
        this line as well with a hotkey that does multi - line commenting*/
        List<String> numberWords = Arrays.asList("six", "seven", "eight", "nine", "ten");
        for (String numberWord : numberWords) {
            // Use the sout shortcut to write out numberWord;
            System.out.println(numberWord);
        }
    }
}

What is the error code when you run this?

Oh thanks it worked. I guess the problem was that instead of putting import java.util.List; I put import.java.ArrayList;

Andrew Winkler
Andrew Winkler
37,739 Points

Awesome! Yeah, Arrays and List are both children of the java.util class. Think of it like a file structure family tree. java -> util -> list

In the future, you should use an IDE and use it's shortcuts to tell you which resources you need to import to facilitate your code. Not only will you dodge errors like this one, but it'll be much faster and simplify how much you need to memorize/know about the java resources library. That way you can focus more so on just code and creation.

Thanks man!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Thanks man!!!!!!!!!!!!!!!!!!!!!!!!!!!!!