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 8 of 8

Thread: Copying a file to an array and getting exception thrown for input

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Copying a file to an array and getting exception thrown for input

    I am trying to copy the contents of a file into an array.
    The contents of the file is
    John X Doe 4.0 6 8.2 9

    The code the instructor wrote on the board. I inputted it and when trying to run I get the error
    Exception in thread "main" java.lang.NumberFormatException: For input string: "Doe"
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1224)
    at java.lang.Double.parseDouble(Double.java:510)
    at fileToArray.getInput(fileToArray.java:37)
    at fileToArray.main(fileToArray.java:16)

    I am not sure what happened. Can anyone assist? Thanks

    import java.util.Scanner;
    import java.io.*;
    import java.util.StringTokenizer;
     
    public class fileToArray
    {
      public static void main(String[] args) throws IOException
      {
        String[] names = new String[39]; // array to hold names from a file
        double[][] reals = new double[39][2]; 
        int[][] ints = new int[39][2];
     
        Scanner inFile = new Scanner(new File("Summer13.txt")); // file to get input
        PrintWriter outFile = new PrintWriter("Summer13.out");  // file to write output
     
        int n = getInput(names, reals, ints, inFile);
        int left = leftOver(inFile);
     
      }
       //Methods
         public static int getInput(String[] names, double[][] reals, int[][] ints, Scanner inFile)
         {
          int i = 0,  m;
          String line;
          StringTokenizer st;
     
          //while loop to read file
          while(i < names.length && inFile.hasNext())
          {
             line = inFile.nextLine();
             st = new StringTokenizer(line);
             m = st.countTokens();
             for(int j = 1; j < m-4; j++)
             {
                names[i] = names[i] + " " + st.nextToken();
             }
             reals[i][0] = Double.parseDouble(st.nextToken());
             ints[i][0] = Integer.parseInt(st.nextToken());
             reals[i][1] = Double.parseDouble(st.nextToken());
             ints[i][1] = Integer.parseInt(st.nextToken());
             i ++;
          }
     
              return i;
         }
     
     
     
     
          public static int leftOver(Scanner inFile)
          {
            int left = 0;
            while(inFile.hasNext())
            {
              left++;
              inFile.nextLine();
            }
             return left;
          }
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Copying a file to an array and getting exception thrown for input

    Out of respect of Java naming conventions, class names should begin with capital letters.

    Are you required to use StringTokenizer, or can you use String.split()? If you can use split(), I recommend it.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Copying a file to an array and getting exception thrown for input

    Instructor gave us an assignment to do which was the final for her CS1 class. I did not have this instructor and did not learn StringTokenizer or String.split() in my CS1 class. StringTokenizer was what she wrote when putting this up on the board. I did not notice the typo in my class header. Thanks.

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Reading from a file and storing to array's. Names are working but numbers are not

    I am trying to copy the contents of a file into an array.
    The contents of the file is
    John X Doe 4.0 6 8.2 9

    I had some typos and it would not read the names. Now I got the names storing to an array and when I
    print out the index it shows the John X Doe

    When I am trying to store a number it works with no error but when I try and output the index[0] its not a number but this is shown
    [I@1f934ad

    I am not sure what happened. Can anyone assist? Thanks

    import java.util.Scanner;
    import java.io.*;
    import java.util.StringTokenizer;
     
    public class FileToArray
    {
      public static void main(String[] args) throws IOException
      {
        String[] names = new String[39]; // array to hold names from a file
        double[][] reals = new double[39][2]; 
        int[][] ints = new int[39][2];
     
        Scanner inFile = new Scanner(new File("Summer13.txt")); // file to get input
        PrintWriter outFile = new PrintWriter("Summer13.out");  // file to write output
     
        int n = getInput(names, reals, ints, inFile);
        int left = leftOver(inFile);
     
      }
       //Methods
         public static int getInput(String[] names, double[][] reals, int[][] ints, Scanner inFile)
         {
          int i = 0,  m;
          String line;
          StringTokenizer st;
     
          //while loop to read file
          while(i < names.length && inFile.hasNext())
          {
             line = inFile.nextLine();
             st = new StringTokenizer(line);
             m = st.countTokens();
             names[i] = st.nextToken();
     
             for(int j = 1; j < m-4; j++)
             {
                names[i] = names[i] + " " + st.nextToken();
             }
             reals[i][0] = Double.parseDouble(st.nextToken());
             ints[i][0] = Integer.parseInt(st.nextToken());
             reals[i][1] = Double.parseDouble(st.nextToken());
             ints[i][1] = Integer.parseInt(st.nextToken());
             i ++;
          }
     
              return i;
         }
     
     
     
     
          public static int leftOver(Scanner inFile)
          {
            int left = 0;
            while(inFile.hasNext())
            {
              left++;
              inFile.nextLine();
            }
             return left;
          }
     
    }

  5. #5
    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: Reading from a file and storing to array's. Names are working but numbers are not

    when I try and output the index[0] its not a number but this is shown
    [I@1f934ad
    That is what is returned by the toString() method of a single dim int array.
    I don't see a variable named: index in the code. There is a two dim int array named: ints. Is that what you are talking about? ints[0] refers to an int array. ints[0][1] refers to an int
    If you want to print the contents of an array, you can do this:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    KristopherP (August 31st, 2013)

  7. #6
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Reading from a file and storing to array's. Names are working but numbers are not

    Hello.
    Can you please provide your print statements?
    If you want to print an array using System.out.println(array) then it will not print the elements of array. Instead it will print the hashcode of an object. Because arrays are treated as objects in java.
    So you will need to traverse the entire array and print individual elements by yourself.

    Syed.

  8. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Reading from a file and storing to array's. Names are working but numbers are not

    Sorry that was just meant to imply that I was printing index 0 of the array's. When I went back to check the code I seen that I was trying to print a [] array when they were [][]. I made the change and now it is working. Thanks a lot.

  9. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Copying a file to an array and getting exception thrown for input

    Quote Originally Posted by KristopherP View Post
    I am trying to copy the contents of a file into an array.
    I'm confused over the instructions. An array, as in singular?

    Quote Originally Posted by KristopherP View Post
    The contents of the file is
    John X Doe 4.0 6 8.2 9
    One single line? Or are there more lines? Will each line contain the same exact setup, which appears to be:
    firstName middleInitial lastName double int double int

    Quote Originally Posted by KristopherP View Post
    The code the instructor wrote on the board.
    The instructor give you the code you are supposed to write? Again I do not understand what you mean here.

    Quote Originally Posted by KristopherP View Post
    Exception in thread "main" java.lang.NumberFormatException: For input string: "Doe"
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1224)
    at java.lang.Double.parseDouble(Double.java:510)
    at fileToArray.getInput(fileToArray.java:37)
    at fileToArray.main(fileToArray.java:16)

    I am not sure what happened. Can anyone assist? Thanks
    The exception means that "Doe" is not a number.
    Add some printlns to see the value of the variables like line, st, m... this will show the symptoms in more detail and help discover the cause

    --- Update ---

    Please do not double post your questions
    Threads merged

Similar Threads

  1. File Input and Output and Exception Handling
    By MagicTricksKill in forum File I/O & Other I/O Streams
    Replies: 21
    Last Post: January 21st, 2013, 07:52 PM
  2. file input to two dimensional array
    By t8ntboy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 20th, 2013, 06:54 PM
  3. Input file to Array
    By Azayth in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 25th, 2012, 03:05 AM
  4. Exception Thrown
    By Frame in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 29th, 2011, 11:14 PM
  5. Replies: 2
    Last Post: May 6th, 2011, 05:19 PM