0

I'm a beginner learning to program in Java. I have written a program that reads words from a file and prints them with spaces in-between in addition to as they exist in the file. When I run my program I get an error denoting an "exception in thread main".

Any help would be appreciated!

Thank You!

The File I'm reading from:

APPLE
ELABORATE
FUTURE
PROOF
LOGICAL

My Program:

//imports
import java.awt.*;
import java.util.*;
import java.io.*;

class Untitled {
    public static void main(String[] args) throws IOException {

        //variables
        String filePath = "/Users/cameronburgess/Programming/I:O/words.txt";
        String word = "";
        int cv = 0;
        String spacedWord = "";

        //objects
        BufferedReader readBot = new BufferedReader(new FileReader (filePath));

        //user facing
        System.out.println("This program will read from a File at :" + filePath);
        System.out.println();
        while (readBot.readLine() != null) {
            word = readBot.readLine();

            //spliter
            while (word.length() != cv) {   
                    spacedWord = spacedWord + " " + word.charAt(cv);
                    cv = cv + 1;
                }   
                System.out.println(word + " OR " + spacedWord);
        }
    }       
}

The Console Return:

This program will read from a File at :/Users/cameronburgess/Programming/I:O/words.txt

ELABORATE OR  E L A B O R A T E
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 9
    at java.lang.String.charAt(String.java:686)
    at Untitled.main(Strings6.java:36)
supercgeek
  • 25
  • 5
  • 2
    You should use a [`StringBuilder`](http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html) instead of the concatation operator to repeatdely concat strings. – vandale Nov 25 '13 at 05:23

3 Answers3

2

Have a try to make some change for the while-loop,

You need to reset the value to cv =0; spacedWord=""; when you are trying to split the word.

like,

 while ((word=readBot.readLine()) != null) {
        //spliter
        cv =0;
        spacedWord="";
        while ( cv <word.length()) {   
                spacedWord = spacedWord + " " + word.charAt(cv);
                cv = cv + 1;
            }   
            System.out.println(word + " OR " + spacedWord);
    }

You will get the result as follows:

This program will read from a File at :TEST.txt

APPLE OR  A P P L E
ELABORATE OR  E L A B O R A T E
FUTURE OR  F U T U R E
PROOF OR  P R O O F
LOGICAL OR  L O G I C A L
Mengjun
  • 3,159
  • 1
  • 15
  • 21
  • That makes sense to reset those values to 0, I updated my code to reflect your recommendations. But I still receive this: ELABORATE OR E L A B O R A T E PROOF OR P R O O F Exception in thread "main" java.lang.NullPointerException at Untitled.main(Strings6.java:37) – supercgeek Nov 25 '13 at 06:11
  • The way you tried to read the file content one-by-one is incorrect. Change it as well. From while (readBot.readLine() != null) { word = readBot.readLine(); to while ((word = readBot.readLine()) != null) { and in addiation. please change the second while-contion to //spliter while (cv < word.length()) { – Mengjun Nov 25 '13 at 06:41
  • I was able to get it working, but I did not need to change the second while condition to do so. Thanks! – supercgeek Nov 25 '13 at 07:44
1

You should reinitialize your cv counter to 0 each time you read your line:

word = readBot.readLine();
// Add this:
cv = 0
while (word.length() != cv) {   
    spacedWord = spacedWord + " " + word.charAt(cv);
    cv = cv + 1;
}   
System.out.println(word + " OR " + spacedWord);
Hernan Velasquez
  • 2,770
  • 14
  • 21
0

If you don't want to handle the length of the word at your end, then use the foreach loop with a character array as follows

    char[] charactersArray = word.toCharArray();
    for (char character : charactersArray) {
            //do whatever you want to with the 'character'
    }
Saif Asif
  • 5,516
  • 3
  • 31
  • 48