So, I'm writing a program in which I need to have a loop that "reads and writes the first character of the strings stored in each element of the array to the output file".
I keep getting a NullPointerException at: a = planets[i].charAt(0);
String[] planets = new String[8];
char a = 'a';
String pl = "planets.txt";
File file = new File(pl);
Scanner inputFile = new Scanner(file);
for(int i = 0; i < planets.length; i++){
while(inputFile.hasNext()){
planets[i] = inputFile.nextLine();
}
}
inputFile.close();
System.out.println("closed.");
String b = "planetfirst.txt";
PrintWriter outputFile = new PrintWriter(b);
for (int i = 0; i< planets.length; i++){
a = planets[i].charAt(0);
outputFile.println(a);
}
outputFile.close();
System.out.println("Data written to the file.");
Thanks in advance!
edit:
I added the rest of my program for some context :)
Your while loop is inside your for loop, so all the text will be inside planets[0], and the rest of the indices will be empty (i.e null). When you later iterate through the array with
for(int i = 0; i < planets.length; i++) {
a = planets[i].charAt(0);
}
you will get a NullPointerException when i is larger than 0.
If your textfile has 8 lines, then there is no need for the while-loop, because you have a for loop that iterates 8 times, and an array of length 8.
If the number of lines in your textfile varies, however, you shouldn't use an array, and instead use an arraylist, and instead of a for loop, only have your while loop.
Something like
List<String> planets = new ArrayList<String>();
while(inputFile.hasNext()){
planets.add(inputFile.nextLine());
}
Related
I'm writing some code to read an input file of book titles, and putting the read lines into an array and trying to print out the array. But when I try to print out the array, it just returns 'null' for each read line. I'm not sure what I'm doing wrong or what my code is doing. Any suggestions? Thanks!
Code:
import java.io.*;
import java.util.*;
public class LibraryInputandOutputs {
public static void main(String args[]) throws IOException{
int lineCount = 0;
File inputFile = new File("bookTitles.inp.txt");
Scanner reader = new Scanner(inputFile);
while(reader.hasNextLine()) {
reader.nextLine();
lineCount++;
}
String[] bookArray = new String[lineCount];
while (reader.hasNextLine()) {
for (int i = 0; i < lineCount; i++) {
bookArray[i] = reader.next();
}
}
for (int k = 0; k < lineCount; k++) {
System.out.println(bookArray[k]);
}
reader.close();
inputFile.close();
}
}
My text file I'm reading from is 20 book titles, all on different lines.
My output on the terminal is 20 lines of null.
Lets break this down:
This reads every line of the input file, counts each one, and then discards them:
while(reader.hasNextLine()) {
reader.nextLine();
lineCount++;
}
You are now at the end of file.
Allocate a string array that is large enough.
String[] bookArray = new String[lineCount];
Attempt to read more lines. The loop will terminate immediately because reader.hasNextLine() will return false. You are already at the end of file.
So you the statement assigning to bookArray[i] won't be executed.
while (reader.hasNextLine()) {
for (int i = 0; i < lineCount; i++) {
bookArray[i] = reader.next();
}
}
Since bookArray[i] = ... was never executed above, all of the array elements will still be null.
for (int k = 0; k < lineCount; k++) {
System.out.println(bookArray[k]);
}
One solution is to open and read the file twice.
Another solution is to "reset" the file back to the beginning. (A bit complicated.)
Another solution would be to use a List rather than an array so that you don't need to read the file twice.
Another solution is to search the javadocs for a method that will read all lines of a file / stream as an array of strings.
(Some of these may be precluded by the requirements of your exercise. You work it out ... )
The nested loop in step 3 is also wrong. You don't need a for loop inside a while loop. You need a single loop that "iterates" the over the lines and also increments the array index (i). They don't both need to be done by the loop statement itself. You could do one or the other (or both) in the loop body.
Stephen C has already pointed out the main problems with your logic. You're trying to loop twice through the file but you've already reached the end of the file the first time. Don't loop twice. "Merge" both the while loops into one, remove that for loop inside the while loop and collect all the book titles. You can then use the size of the list to print them later on. My Java might be rusty but here it goes -
import java.io.*;
import java.util.*;
public class LibraryInputandOutputs {
public static void main(String args[]) throws IOException {
// int lineCount = 0; - You don't need this.
File inputFile = new File("bookTitles.inp.txt");
Scanner reader = new Scanner(inputFile);
// Use an array list to collect book titles.
List<String> bookArray = new ArrayList<>();
// Loop through the file and add titles to the array list.
while(reader.hasNextLine()) {
bookArray.add(reader.nextLine());
// lineCount++; - not needed
}
// Not needed -
// while (reader.hasNextLine()) {
// for (int i = 0; i < lineCount; i++) {
// bookArray[i] = reader.next();
// }
// }
// Use the size method of the array list class to get the length of the list
// and use it for looping.
for (int k = 0; k < bookArray.size(); k++) {
System.out.println(bookArray[k]);
}
reader.close();
inputFile.close();
}
}
I agree with Stephen C. In particular, using a List is usually better than an array because it's more flexible. If you need an array, you can always use toArray() after the List is filled.
Are your book titles on separate lines? If so you might not need a Scanner class, and could use something like a BufferedReader or LineNumberReader.
The code below runs perfectly fine if the text file consists of lines of numbers but once it gets to for example a line that says "I am 40" it skips it instead of putting 40 into the array.
Scanner inFile = null;
File file = null;
String filePath = (JOptionPane.showInputDialog("Please enter a file path"));
int size = 0;
int[] result = new int[10];
try {
file = new File(filePath);
inFile = new Scanner(file);
int skippedCounter = 0;
for(int i = 0; inFile.hasNext(); i++){
if(inFile.hasNextInt())
result[i] = inFile.nextInt();
else{
String strOut = "";
String data = inFile.next();
for(int j = 0; j <= data.length() - 1; j++){
if(!Character.isLetter(data.charAt(j))){
strOut += data.charAt(j);
}
else
skippedCounter++;
}
result[i] = Integer.parseInt(strOut);
}
}
}
next() will give you the next token not the next Line. so variable i may go past ten. you would realize this if you didnt have an empty catch : that your array is going out of bounds
solution:
don't use a result array, use a result list, and append to its end whenever you have another result
note:
another hidden exception that could be occurring is when your parseInt fails due to non-numeric data. So don't wrap everything in a giant try/catch, it just makes it harder to debug!
I suggest you to use nextInt function just one time to keep the requested value, then use that variable whenever you need it. I think nextInt function moves to the next int each time you appeal it.
The following
result[i] = Integer.parseInt(strOut)
will result in a NumberFormatException when trying to process any letter. As strOut results in an empty String ""
You'll have to check for an empty String before attempting to parse
if (!strOut.isEmpty()) {
result[i] = Integer.parseInt(strOut);
}
I'm cating a file using the cat text-file | Java my-program on my terminal
the result when I print the lines after i store it into a array results in null
can someone explain why?
Scanner scan2 = new Scanner(System.in);
Scanner scan = new Scanner(System.in);//create new scanner object
int index = 0;//create index to increment through array
while(scan.hasNextLine()){//while looop to execute if file has length
String line = scan.nextLine();//store line into string input
count++;
}
if(count < BUFSIZE){
stringArray = new String[count];
}
else{
stringArray = new String[BUFSIZE];
}
while(scan2.hasNextLine()){
String line2 = scan2.nextLine();
if(index > stringArray.length-1)
{
stringArray = expandArray(stringArray,BUFSIZE);//call method to increase array length
}
stringArray[index] = line2;//store line into array at given index
index++;//increment index
}
/*while(sorted){
sorted = false;
for(int i = 0; i < stringArray.length-1; i++){
if(stringArray[i].compareTo(stringArray[i+1]) > 0){
temp = stringArray[i];
stringArray[i] = stringArray[i+1];
stringArray[i+1] = temp;
sorted = true;
}
}
}*/
for(int i = 0; i < stringArray.length; i++){
System.out.println(stringArray[i]);
}
}
}
private static String [] expandArray(String [] array, int extend){
String newArray [] = new String[array.length+extend];//create new array with given array and int as length to extend
//for loop to copy data from old array into new created array
for( int i = 0; i < array.length; i++){
newArray[i] = array[i];
}
return newArray;//return newly created array
}
}
the program bubble sorts the array of strings. If I read in the file through Scanner file its fine, but why I cat it doesn't. The expand array method is to dynamically expand the array every-time it reaches max capacity. Thank you
The following is wrong:
Scanner scan2 = new Scanner(System.in);
Scanner scan = new Scanner(System.in);//create new scanner object
Basically, each scanner grabs the System.in stream, but streams can only be read once. You should change your code to use only one scanner, and then only use it once.
When you wrote the program to use a file, the Scanner would actually open two streams to the file so that it can be read twice, but this won't work when all you have is one stream.
EDIT:
Here is a version where you use only one Scanner (and thus one stream) :
Scanner scan = new Scanner(System.in);//create new scanner object
int count = 0;
stringArray = new String[BUFSIZE];
while (scan.hasNextLine()){//while looop to execute if file has length
String line = scan.nextLine();//store line into string input
if (count >= stringArray.length) {
//call method to double array length
stringArray = expandArray(stringArray, stringArray.length);
}
stringArray[count] = line;
count++;
}
// Shrink array to required size
String[] temp = stringArray;
stringArray = new String[count];
System.arraycopy(temp, 0, stringArray, 0, count);
Please note I didn't test it, but this is conceptually how you could do it.
The other alternative is to use an ArrayList<String> which will automatically expand and shrink.
Scanner scan = new Scanner(System.in);
List<String> list = new LinkedList<>();
while(scan.hasNext()) list.add(scan.next());
scan.close();
Collections.sort(list);
for(String line : list) System.out.println(line);
lol
UPDATE #JBert:
System.out.println(StringUtils.join(Ordering.<String>natural().sortedCopy(IOUtils.readLines(System.in)), "\r\n"));
looool
In this code
while(scan2.hasNextLine()){
String line2 = scan2.nextLine();
if(index > stringArray.length-1)
you are doing something if index is greater than the length of the array, but no doing anything otherwise.
I have to read a text file into a 2d Array.
The only problem I am having, is the width of the array varies, with a maximum size of 9 columns.
I don't know how many rows there will be.
Some lines will have 6 columns for example, and some will have 9.
here is a small section of my CSV file:
1908,Souths,Easts,Souths,Cumberland,Y,14,12,4000
1909,Souths,Balmain,Souths,Wests,N
1910,Newtown,Souths,Newtown,Wests,Y,4,4,14000
1911,Easts,Glebe,Glebe,Balmain,Y,11,8,20000
1912,Easts,Glebe,Easts,Wests,N
1913,Easts,Newtown,Easts,Wests,N
and here is my code so far
import java.io.*;
import java.util.*;
public class ass2 {
public static void main(String[] args) throws IOException {
readData();
}
public static void readData() throws IOException{
BufferedReader dataBR = new BufferedReader(new FileReader(new File("nrldata.txt")));
String line = "";
ArrayList<String[]> dataArr = new ArrayList<String[]>(); //An ArrayList is used because I don't know how many records are in the file.
while ((line = dataBR.readLine()) != null) { // Read a single line from the file until there are no more lines to read
String[] club = new String[9]; // Each club has 3 fields, so we need room for the 3 tokens.
for (int i = 0; i < 9; i++) { // For each token in the line that we've read:
String[] value = line.split(",", 9);
club[i] = value[i]; // Place the token into the 'i'th "column"
}
dataArr.add(club); // Add the "club" info to the list of clubs.
}
for (int i = 0; i < dataArr.size(); i++) {
for (int x = 0; x < dataArr.get(i).length; x++) {
System.out.printf("dataArr[%d][%d]: ", i, x);
System.out.println(dataArr.get(i)[x]);
}
}
}
The error I get is:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at ass2.readData(ass2.java:23)
at ass2.main(ass2.java:7)
Can someone please help :'(
Thank you!
You can use OpenCSV for read the CSV file.
// Read all
CSVReader csvReader = new CSVReader(new FileReader(new File("nrldata.txt")));
List<String[]> list = csvReader.readAll();
// Convert to 2D array
String[][] dataArr = new String[list.size()][];
dataArr = list.toArray(dataArr);
The problem is with your inner loop. You are trying to access 9 elements of value regardless of how many values there are on the line. First, you should move the assignment to value to be before the inner loop. Then, you need to limit the loop iterations to the minimum of 9 and the length of value:
String[] value = line.split(",", 9);
int n = Math.min(value.length, data.length);
for (int i = 0; i < n; i++) { // For each token in the line that we've read:
data[i] = value[i]; // Place the token into the 'i'th "column"
}
Note that the trailing elements of data will be null.
You get the error, because you try to access a 7th token (index 6) on a line that contains only 6. Replace that:
for (int i = 0; i < 9; i++) { // For each token in the line that we've read:
String[] value = line.split(",", 9);
data[i] = value[i]; // Place the token into the 'i'th "column"
}
with this:
String[] value = line.spkit(",", 9); // Split the line into max. 9 tokens
for (int i = 0; i < value.length; i++) {
data[i] = value[i]; // Add each token to data[]
}
You could, in fact, replace the whole while-loop body with this one-liner:
dataArr.add(Arrays.copyOf(line.split(",", 9), 9));
See, also, this short demo.
Instead of array, you can use ArrayList of List. As List is dynamically growable so you do't need to think of it size either.
List<List<String>> dataArr = new ArrayList<List<String>>();
and
while ((line = dataBR.readLine()) != null){
for (int i = 0; i < 9; i++)
dataArr.add(Arrays.asList(line.split(",", 9)));
}
Hi all I wrote a mergesort program for a string array that reads in .txt files from the user. But what I want to do now is compare both files and print out the words in file one and not in file two for example apple is in file 1 but not file 2. I tried storing it in a string array again and then printing that out at the end but I just cant seem to implement it.
Here is what I have,
FileIO reader = new FileIO();
String words[] = reader.load("C:\\list1.txt");
String list[] = reader.load("C:\\list2.txt");
mergeSort(words);
mergeSort(list);
String x = null ;
for(int i = 0; i<words.length; i++)
{
for(int j = 0; j<list.length; j++)
{
if(!words[i].equals(list[j]))
{
x = words[i];
}
}
}
System.out.println(x);
Any help or suggestions would be appriciated!
If you want to check the words that are in the first array but do not exist in the second, you can do like this:
boolean notEqual = true;
for(int i = 0; i<words.length; i++)
{
for(int j = 0; j<list.length && notEqual; j++)
{
if(words[i].equals(list[j])) // If the word of file one exist
{ // file two we set notEqual to false
notEqual = false; // and we terminate the inner cycle
}
}
if(notEqual) // If the notEqual remained true
System.out.println(words[i]); // we print the the element of file one
// that do not exist in the second file
notEqual = true; // set variable to true to be used check
} // the other words of file one.
Basically, you take a word from the first file (string from the array) and check if there is a word in file two that is equal. If you find it, you set the control variable notEqual to false, thus getting out of the inner loop for and not print the word. Otherwise, if there is not any word on file two that match the word from file one, the control variable notEqual will be true. Hence, print the element outside the inner loop for.
You can replace the printing statement, for another one that store the unique word in an extra array, if you wish.
Another solution, although slower that the first one:
List <String> file1Words = Arrays.asList(words);
List <String> file2Words = Arrays.asList(list);
for(String s : file1Words)
if(!file2Words.contains(s))
System.out.println(s);
You convert your arrays to a List using the method Arrays.asList, and use the method contains to verify if the word of the first file is on the second file.
Why not just convert the Arrays to Sets? Then you can simply do
result = wordsSet.removeAll(listSet);
your result will contain all the words that do not exist in list2.txt
Also keep in mind that the set will remove duplicates ;)
you can also just go through the loop and add it when you reached list.length-1.
and if it matches you can break the whole stuff
FileIO reader = new FileIO();
String words[] = reader.load("C:\\list1.txt");
String list[] = reader.load("C:\\list2.txt");
mergeSort(words);
mergeSort(list);
//never ever null
String x = "" ;
for(int i = 0; i<words.length; i++)
{
for(int j = 0; j<list.length; j++)
{
if(words[i].equals(list[j]))
break;
if(j == list.length-1)
x += words[i] + " ";
}
}
System.out.println(x);
Here is a version (though it does not use sorting)
String[] file1 = {"word1", "word2", "word3", "word4"};
String[] file2 = {"word2", "word3"};
List<String> l1 = new ArrayList(Arrays.asList(file1));
List<String> l2 = Arrays.asList(file2);
l1.removeAll(l2);
System.out.println("Not in file2 " + l1);
it prints
Not in file2 [word1, word4]
This looks kind of close. What you're doing is for every string in words, you're comparing it to every word in list, so if you have even one string in list that's not in words, x is getting set.
What I'd suggest is changing if(!words[i].equals(list[j])) to if(words[i].equals(list[j])). So now you know that the string in words appears in list, so you don't need to display it. if you completely cycle through list without seeing the word, then you know you need to explain it. So something like this:
for(int i = 0; i<words.length; i++)
{
boolean wordFoundInList = false;
for(int j = 0; j<list.length; j++)
{
if(words[i].equals(list[j]))
{
wordFoundInList = true;
break;
}
}
if (!wordFoundInList) {
System.out.println(x);
}
}