Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to read a 2 dimensional text file from an array?

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to read a 2 dimensional text file from an array?

    import java.io.*;
    import java.util.*;
    import java.text.*;
     
    public class 2DArray
    {
        int Mark[] [] = new int [100] [100]; // max numbers for a 3 by 4 array
        String[] Names = new String [4];
        int count = 0;
     
        void read () throws IOException
        {
            BufferedReader reader = new BufferedReader (new FileReader ("Class.txt")); // reads the 5 students
     
            String line = null;
     
            while ((line = reader.readLine ()) != null)
            {
                Names [count] = (line);
                line = reader.readLine ();
                Mark [0] [0] = Integer.parseInt (line);
                line = reader.readLine ();
                Mark [0] [0] = Integer.parseInt (line);
                line = reader.readLine ();
                Mark [0] [0] = Integer.parseInt (line);
                line = reader.readLine ();
                Mark [0] [0] = Integer.parseInt (line);
                line = reader.readLine ();
                count++;
            }
            reader.close ();
        }
     
     
        void displayorg () throws IOException
        {
            System.out.println ("What has been read: ");
     
            for (int i = 1 ; i <= 3 ; i++)
            {
                for (int j = 1 ; j <= 4 ; j++)
                {
                    System.out.print (Names [j]);
                    System.out.print (Mark [i] [j] + "\t");
                }
                System.out.println ("\n");
            }
     
     
        }
     
     
        public static void main (String str[]) throws IOException
        {
            BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
     
            2DArray k = new 2DArray(); // The methods dont have to be static now
     
     
            k.read ();
            k.displayorg ();
     
     
     
        }
    }

    My text file looks like this: A name of a student followed by his 4 marks

    Caral
    34
    23
    55
    33
    Katrina
    87
    78
    98
    87
    Sam
    54
    65
    76
    56
    Mathew
    46
    76
    55
    86

    How can I properly read this off. It tells me:

    java.lang.NumberFormatException: For input string: "Sam"
    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at Array42.read(Array42.java:44)
    at Array42.main(Array42.java:77)

    I am also using Ready to Program java.... So scanners wont work most probably


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to read a 2 dimensional text file from an array?

    java.lang.NumberFormatException: For input string: "Sam"
    You can not parse the String: "Sam" to an int value.
    To see what the code is doing, add a println statement that prints out every line that is read so you can see what the program sees.

    Check that your input file is in the right format.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. What is the best way to read from a text file?
    By Kerr in forum File I/O & Other I/O Streams
    Replies: 20
    Last Post: January 4th, 2012, 05:41 PM
  2. how to read a text delimited file using 2 dimentional array in java ??
    By pooja123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 1st, 2011, 09:11 AM
  3. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  4. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 05:47 AM
  5. Read A File and Store Values into a 2-Dimensional Integer Array?
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 9th, 2011, 09:13 PM