1

I am writing a method that receives a file and returns an array list with every other word from the file. The files can contain multiple lines.
For example, if I scan through the File and it reads

{"red", "orange", "yellow", "green", "blue", "purple"} 

The method should return the list

{"red","yellow","blue"} 

I'm aware this is a rather simple question and if it has already been answered, please point me in the right direction. Also, I believe these are comma separated variables. So far I have my header and scanner declaration:

public static ArrayList<String>Skip(File file) ArrayList<String> newList = new ArrayList<String>(); Scanner scanner = new scanner(file); while(scanner.hasNextLine()){ *WHAT DO I DO HERE* newList.add(____); } scanner.close; } return newList; 
3
  • you get the filename using scanner. then add it. Commented Apr 1, 2016 at 5:51
  • One way to accomplish the "every other word" thing: Use a boolean variable addThisWord that tells you whether you're going to add the word or not. Then after you see a word, addThisWord = !addThisWord; will cause it to flip back and forth between true and false. Commented Apr 1, 2016 at 5:55
  • Do you want a list of unique words from the file ? Commented Apr 1, 2016 at 6:16

2 Answers 2

1

By default a Scanner uses whitespace as a delimiter, but you can explicitly set a different one. E.g.

scanner.useDelimiter(","); 

To choose alternate lines, the easiest thing is to define a boolean variable outside of the loop and then negate it (false -> true and true -> false) each time round the loop.

boolean includeItem = true; while (...) { ... includeItem = !includeItem; } 

SPOILER ALERT

Here's some code that puts it together and seems to do what you need:

public static List<String> alternateItemsFrom(File commaSeparatedFile) throws FileNotFoundException { List<String> results = new ArrayList<String>(); boolean includeItem = true; try (Scanner scanner = new Scanner(commaSeparatedFile)) { scanner.useDelimiter(","); while (scanner.hasNext()) { String item = scanner.next().trim(); if (includeItem) { results.add(item); } includeItem = !includeItem; } } return results; } 

Also note:

  • When you create a Scanner for a file, a FileNotFoundException may be thrown. I needed to declare that exception on the method.

  • Although you can close a the scanner after the loop with scanner.close(), there is a better way to do it in Java 7+ with a so-called try-with-resources block. This guarantees that the scanner will be closed even if an exception occurs while you're looping.

  • I've renamed the method and some of the variables to try to express their intentions more clearly.

Sign up to request clarification or add additional context in comments.

4 Comments

The while loop should use hasNext(), not hasNextLine(). It's sad that you just made the OP's homework, instead of just giving hints to let it implement his own solution.
I think the OP's a she. She didn't mention it was homework.
Indeed. Sorry for that gender confusion. Even if it is not homework, you learn more by doing things than by copy and pasting things.
I agree, but also think sometimes it can be useful to see an idiomatic solution written by someone more experienced. Obviously, it would be completely pointless just copying and pasting the code without understanding though. I did try to explain the details in my response.
0

The Scanner allows reading tokens, and tokens are, by default, separated by whitespace characters. But, as the javadoc indicates, you can use another delimiter.

Choose the appropriate delimiter for your file, then read the file token by token (and not line by line), and add each odd token to your List (you can use a boolean that goes from true to false and vice-versa at each iteration to know if the token must be kept or ignored).

Finally, a Scanner on a file must be closed, even if your code happens to throw an exception. So you should use try-with-resources to initialize your scanner, to make sure it's closed no matter what.

The javadoc is there to help. Read it carefully and try something.

6 Comments

I suspect you misread the question--where does it say anything about unique words?
Also wondering the same thing. And didn't I close the scanner correctly?
No, you didn't. If an exception happens for whatever reason in your loop, the scanner won't be closed.
Thank you for the help though! Instead of searching for unique words, how would I search for every other word in the array? I guess they would be located at even indexes
I edited my answer to explain that. You can indeed use the index and test if it's even, or use a boolean that is toggled at each iteration
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.