Results 1 to 7 of 7

Thread: whats wrong

  1. #1
    atenv is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default whats wrong

    Hello,

    can anyone tell me why on earth am i getting only zeros while i'm iterating the array in main method ?

    Java Code:
        public static int[] readFile(String fName) {
            try {
                Scanner scanner = new Scanner(new File(fName));
                while (scanner.hasNext()) {
                    myNext = new int[scanner.toString().length()];
                    int c = 0;
                    myNext[c] = scanner.nextInt();
                    System.out.print( myNext[c] + " "); //dbg output
                    c++;
                }
    
            } catch (FileNotFoundException e) {
                System.err.println("FileNotFoundException: "
                        + e.getMessage());
            }
    
            return myNext;
        }
    
        public static void main(String[] args) {
    
            System.out.println( System.getProperty("user.dir"));
            int fileArrayLength = readFile("readme.txt").length;
            int[] fileArray = readFile("readme.txt");
    
           // selectionSort(fileArray);
            System.out.println();
            for (int i = 0; i <= fileArrayLength-1; i++) {
                System.out.print(fileArray[i] + " ");
            }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Do you want to create a new myNext array each time the while loop iterates? If myNext is being returned by the readFile method, should it be a static class field (my own opinion here is "no")? What the do you think this code does:
    Java Code:
    myNext = new int[scanner.toString().length()];
    ?
    Have you checked what scanner.toString() returns? And why does the length of this String matter? Finally, what does the input fill look like?
    Last edited by Fubarable; 06-15-2010 at 01:59 AM.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Means about this.

    Java Code:
    while (scanner.hasNext()) {
                    myNext = new int[scanner.toString().length()];
    }
    As Fubarable explains, what happen if the scanner found more than one line of text. Think about that on each line found in the scanner what happen in myNext. Put a print statement to check the length of it and check.

  4. #4
    atenv is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Do you want to create a new myNext array each time the while loop iterates? If myNext is being returned by the readFile method, should it be a static class field (my own opinion here is "no")? What the do you think this code does:
    Java Code:
    myNext = new int[scanner.toString().length()];
    ?
    Have you checked what scanner.toString() returns? And why does the length of this String matter? Finally, what does the input fill look like?
    Since i dont know the length of the file input - i thought i'll have to create every time a new array with its new length.

    Java Code:
    myNext = new int[scanner.toString().length()];
    was suppose to read the length of scanner and parse it as an array length for myNext

    How else would i get the scanner length ?

    Add: the file contains only space separated numbers
    Last edited by atenv; 06-15-2010 at 10:54 AM.

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Also, in your loop, you always set c to 0. You do increment it at the end of the loop, but in the next iteration it gets set back to 0.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  6. #6
    atenv is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by m00nchile View Post
    Also, in your loop, you always set c to 0. You do increment it at the end of the loop, but in the next iteration it gets set back to 0.
    aah, true - what a stupid mistake. :(

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by atenv View Post
    Since i dont know the length of the file input - i thought i'll have to create every time a new array with its new length.
    and delete any contents held in the array previously. No I wouldn't do this.

    Java Code:
    myNext = new int[scanner.toString().length()];
    was suppose to read the length of scanner and parse it as an array length for myNext

    How else would i get the scanner length ?
    But if you display what scanner.toString() holds, you'll find that your code does nothing of the kind. To toString method here will only display some information about the scanner object and taking its length is useless to your program. As to getting the length of data, that depends upon how the data is stored. Previously I asked for you to display it, but we've yet to see it. You may have to either use an ArrayList which won't care about the size of the data, or else run through the data file twice, first to get the length and the second time to extract the data itself.

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 02:37 AM
  2. What did I do wrong?
    By GoingThroAPhase in forum New To Java
    Replies: 4
    Last Post: 04-03-2010, 05:51 AM
  3. Threads don't start after few iterations
    By gaurav2211 in forum Threads and Synchronization
    Replies: 2
    Last Post: 12-18-2009, 10:34 AM
  4. what am i doing wrong here??
    By tornbacchus in forum New To Java
    Replies: 19
    Last Post: 04-16-2009, 04:54 AM
  5. what wrong
    By pro85 in forum New To Java
    Replies: 3
    Last Post: 02-09-2009, 02:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •