?
Solved

How to insert contents of text file into an array?

Posted on 2003-03-23
7
Medium Priority
?
192 Views
Last Modified: 2010-03-31
Hello - I am having trouble getting the contents of a text file into the array. The while loop prints the contents of the text file (4 ints, on consecutive lines)so I know the FileReader is working, yet it does gives me a nullPointer Exception.

As I said, I can't figure out how to get the file contents into the array. The intent is apply the bubbleSort method to the numbers. You can see where I'm calling the bubbleSort method. I hope this is enough info. Thanks for any help!

import java.io.*;
import java.lang.*;

public static void main(String[] args) throws IOException
{
  String input = "";
  try
  {
    BufferedReader br =
       new BufferedReader(new FileReader("test.txt"));

    System.out.println("Numbers in unsorted order:");

   while ((input = br.readLine()) != null)
   {
      System.out.println(input);
   }

   input = br.readLine();

  //Create an array of integer objects
  Integer[] myNumbers = new Integer[4];

  //Sort the numbers with BubbleSort
  bubbleSort(myNumbers, myNumbers.length);

  //Print the sorted array
  System.out.println("\nSorted numbers: ");
  for(int i=0; i < myNumbers.length; ++i)
  System.out.print(myNumbers[i] + " ");
  System.out.println();

  br.close();   // close BufferedReader

} // end try
catch(FileNotFoundException e)
{
   System.out.println("Error: File not found.");
}
catch(NumberFormatException nfe)
{
   System.out.println("Number Format Error.");
}

}//end main
0
Comment
Question by:Jappa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 9

Accepted Solution

by:
yongsing earned 200 total points
ID: 8193522
You have created the array, but you did not store anything into the array at all. So what your array contains are just null references.

//Create an array of integer objects
Integer[] myNumbers = new Integer[4];

// Read a line and create an Integer object to store in array.
int i = 0;
while ((input = br.readLine()) != null && i != 4) {
  myNumbers[i++] = new Integer(input);
  System.out.println(input);
}



0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8193792
Integer[] myNumbers = new Integer[4];

StringTokenizer st = new StringTokenizer ( input, " " ) ;
int i = 0 ;

while ( st.hasMoreElements () )
  myNumbers[i++] = Integer.parseInt ( st.nextElement ().toString () ) ; // end while

bubbleSort ( myNumbers, myNumbers.length ) ;


That'll do it.

Mayank.

0
 
LVL 9

Expert Comment

by:yongsing
ID: 8193803
I thought he meant that there are 4 integers, and each integer is on a line by itself.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 8193867
In that case, he hasn't put a loop for reading the lines at all. Something like:

Integer[] myNumbers = new Integer[4] ;
int i = 0 ;

while ( ( input = br.readLine () ) != null )
  myNumbers[i++] = Integer.parseInt ( input ) ; // end while

bubbleSort ( myNumbers, myNumbers.length ) ;

Actually, I just went through the coding and saw that there's no loop, so I thought that all the numbers are in the same line in the text-file, probably separated by blanks :-)

Mayank.
0
 

Author Comment

by:Jappa
ID: 8197362
Thank you. Your code worked very well and I learned something as well.

Jappa
0
 

Author Comment

by:Jappa
ID: 8197419
Mayank - Thank you also. Your code also worked fine, but I had to make one small change to get it to work.

Where you had:
myNumbers[i++] = Integer.parseInt ( input ); // end while

I had to change to:
myNumbers[i++] = Integer.valueOf ( input ); // end while

Thanks again,
Jappa
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8200269
Oh... so sorry. Your array was an Integer[] array and not an int[] array.... that's why it happened. parseInt () returns an 'int' whereas valueOf returns an Integer object.

Mayank.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question