Thread: Need java help

  1. #1

    Need java help

    Ok so, what I'm trying to do here is read strings from a file and put them in an array so I can randomly return one of the strings in the array. I believe I'm doing everything right except for actually constructing the array. The array is apparently constructing itself as {null, word in file at i}, {null, null, word in the file at i}, etc. In other words my failed attempt at creating this array in the end becomes {null, null, null, null, the last word in the file}. Essentially I'm thinking I need to initialize the array outside of the while loop, however I don't know how I can do this with what we've learned in class so far. A lot of forums say to use lists (haven't learned) or some other method that I'm not allowed to use because we haven't learned it yet. So any help would be appreciated. How can I read Strings from a file and convert them into an array so that I can return a random String from that array???

    Here's where I'm at

    Code:
    public static String pickWord(String f) throws FileNotFoundException
       {
           Scanner file = new Scanner(new File(f));
           String word = ""; 
           Random ranWord = new Random();
           int i = 0;
           int ranInt = 0;
           String[] wordChoices;
           while (file.hasNextLine())
           {
               //I realize this initializes the array every time the loop runs which apparently sets the values in the array back to null. I need it not to do this, 
               //however I'm not sure how to get the length of the array outside of the loop. I've thought of finding it in a different while loop, but that doesn't
               //work because the first while loop is the only one that runs. 
               wordChoices = new String[i+1];
               wordChoices[i] = file.nextLine();
               ranInt = ranWord.nextInt(i+1);
               word = wordChoices[ranInt];
               //used println to see what the array lookes like.
               System.out.println(Arrays.toString(wordChoices));
               i++;
           }
           //only printing to see what it actually returns
           System.out.println(word);
           return word;
       }
    Last edited by simplemind; 2012-12-01 at 11:22 PM.

  2. #2
    Probably not the best for this as I have not done Java(but I have done C# which is pretty much the same). Can you remove the random part of the program and tell me what the error is. I just want to know if the error is to do with the random or the program its self(I am assuming it is something in the program, but the random part is just adding un-need complexity to finding what the error actually is).

  3. #3
    The Lightbringer N-7's Avatar
    10+ Year Old Account
    Join Date
    Apr 2012
    Location
    UK
    Posts
    3,572
    Can't you just do "word = word + wordChoices[ranInt];" so it adds both the previous "word" + the new one in the loop?

    I haven't used Java in a long time so sorry if that doesn't help

  4. #4
    Ok so ignoring the random part. I'm having trouble creating the array from the file of strings. I want to create an array that is as long as how many strings are in the file (wordChoices = new String [i+1]; ) however I'm not sure how to find how many strings are in the file before I actually create the array. If I create the array outside of the while loop I wouldn't know how to initialize each index to be its corresponding string in the file. Whereas, in my current code I have the array essentially recreating itself every time the loop loops and its only initializing the index at the end of the array and leaving the previous indexes at null.

    ---------- Post added 2012-12-01 at 11:21 PM ----------

    Quote Originally Posted by N-7 View Post
    Can't you just do "word = word + wordChoices[ranInt];" so it adds both the previous "word" + the new one in the loop?

    I haven't used Java in a long time so sorry if that doesn't help
    Actually I'm not having problems with the random part. Just creating the array. I don't want to add each random word every time the loop runs because then I would end up with a bunch of random words smashed together. I know I'm using random in a way that it runs every time the loop runs which may be inefficient, however I believe in the end I'll get one random word, so I'm pretty sure I don't need help with the random part.

  5. #5
    Also really rusty with Java(and practically no experience with it), but would something like this work for creating it outside of the loop?

    Code:
    public static String pickWord(String f) throws FileNotFoundException
       {
           Scanner file = new Scanner(new File(f));
           String word = ""; 
           Random ranWord = new Random();
           int i = 0;
           int ranInt = 0;
           String[] wordChoices;
           wordChoices = new String[file.length];
           while (file.hasNextLine())
           {
               wordChoices[i] = file.nextLine();
               ranInt = ranWord.nextInt(i+1);
               word = wordChoices[ranInt];
               i++;
           }
           //only printing to see what it actually returns
           System.out.println(word);
           return word;
       }

  6. #6
    Quote Originally Posted by Mizzeeh View Post
    Also really rusty with Java(and practically no experience with it), but would something like this work for creating it outside of the loop?
    I don't believe there's a length method for Scanner. I may be wrong but it doesn't compile at least as you have it written.

  7. #7
    The Lightbringer N-7's Avatar
    10+ Year Old Account
    Join Date
    Apr 2012
    Location
    UK
    Posts
    3,572
    Quote Originally Posted by simplemind View Post
    Actually I'm not having problems with the random part. Just creating the array. I don't want to add each random word every time the loop runs because then I would end up with a bunch of random words smashed together. I know I'm using random in a way that it runs every time the loop runs which may be inefficient, however I believe in the end I'll get one random word, so I'm pretty sure I don't need help with the random part.
    I am still not sure what you're trying to achieve in your code but have you tried "buffered reader" then? That what I used to use when I was trying to read from a .txt file.

    http://www.roseindia.net/java/exampl...edReader.shtml

  8. #8
    haven't learned buffered reader yet so I'm not allowed to use it.

  9. #9
    Quote Originally Posted by Mizzeeh View Post
    Also really rusty with Java(and practically no experience with it), but would something like this work for creating it outside of the loop?
    Can't do that. "file.length()" will return the length of the file in bytes, not the number of lines.

    As for the OP, you're not allowed to use any form of I/O or "beginner" data structures on assignments? Sounds suspect of a poor teacher. . .Anyways, Mizzeh already showed this (albeit slightly incorrectly), but you haven't initialized your array of Strings properly. This bit of code:

    Code:
    wordChoices = new String[i+1];
    Is telling the machine to create a new instance of an array of Strings with size [i + 1] every time your file reaches a valid new line. You need to initialize your String array before the while loop, then "fill in" the space with Strings read in from the file. Your approach so far also seems off. You need to use the while() loop only to "fill" the array.

    Code:
    Random r = new Random();
    int ranInt = r.nextInt(SIZE_GOES_HERE);
    String[] wordChoices = new String[SIZE_GOES_HERE];
    
    while (file.hasNextLine()) {
    	//"fill" wordChoices
    	}
    
    return wordChoices[ranInt];
    How you determine the "size", which represents the number of lines in the file, is up to you. You can certainly use a scanner to scan through the file and increment a counter until it reaches the end of the file, then use that counter as your size for your array. This is why people on the internet have suggested using lists (specifically, ArrayLists) - you don't need to worry about initializing a size because lists are dynamic, thus you do half the work (scan the file only once).

    Confirm with your teacher that you can only use arrays. It's rather dumb to have that kind of restriction on this kind of assignment.

Posting Permissions

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