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

Thread: "null" in my output :( Scanner problem?

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Location
    San Francisco, CA
    Posts
    10
    My Mood
    Yeehaw
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Question "null" in my output :( Scanner problem?

    Hello! I'm a bit stuck, and I've come here because y'all are epic at unstuck-ing.

    The homework assignment -- Write a program that uses randomness and the orthographic rules of any natural language to generate memorizable passwords. These passwords are gibberish text, about ten letters long, with unambiguous pronunciations. Your program should output ten possible passwords. Collect syllables or other parts of words to assemble in a random order.

    Most of my code seems to be workable, if inelegant, but here's my problem: when I run it, it prints out my ten gibberish passwords....BUT the word "null" pops in there a few times, too. So output looks like so ("nulls" bolded by me):

    chomolsald
    xhejastold
    ghanogzinull
    psefoksost
    whajitsabz
    clunangasp
    xyokundanj
    hyipelzols
    lyikilpunj
    zluviskenull

    From what I've read, I think this is related to using the Scanner class? Because after it reads the final line, it returns "null," right? But I can't figure out how exactly to change that, or get it to not print out in my passwords, and I'm tearing my hair out. I'm sure it's a simple solution, but I can't seem to figure it out using my textbook or the interwebs. I've been moving some things around, snipping and adding, and testing it out to see what changes, but so far nothing has helped.

    Code is pasted below; any pointers in the right direction would be much appreciated! <3 It is homework, but it's not due for another week, so if you want to send me really digging for the answer...I've got the time! Hehe. I just want to know I'm moving in the right direction, if possible.

    import java.io.*; //For File class and FileNotFoundException
    import java.util.Scanner; //For Scanner class
     
    /** A program to generate random, pronounceable passwords.
    */
     
    public class Homework12
    {
     public static void main(String[] args)
     {
     
      /** Declare and initialize four arrays: vowels, consonants, two types of 
         consonant clusters. */
      String[] vowel = {"a", "e", "i", "o", "u"};
      String[] consonant = {"b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
                           "n", "p", "q", "r", "s", "t", "v", "w", "x", "y",
                           "z"};
      String[] cluster_start = new String[84];
      String[] cluster_end = new String[37];
     
     
    /** Use the first file, which contains consonant clusters
    typically used to begin words in English, to populate array cluster_start[]. */
      try
      {
       //Open the file
       File file = new File("consonantclusters_start.txt");
       Scanner inputFile = new Scanner(file);
     
       //Read from the file
        int index;
        for (index = 0; index < 83; index++)
        {
         cluster_start[index] = inputFile.next();
        }
      }
      catch (FileNotFoundException e)
      {
       System.out.println("File not found.");
      }
     
     
    /** Use the second file, which contains consonant clusters
    typically used to end words in English, to populate array cluster_end[]. */
     
      try
      {
       //Open the file
       File file2 = new File("consonantclusters_end.txt");
       Scanner inputFile = new Scanner(file2);
     
       //Read from the file
       int index;
       for (index = 0; index < 36; index++)
       {
        cluster_end[index] = inputFile.next();
       }
      }
      catch (FileNotFoundException e)
      {
       System.out.println("File not found.");
      }
     
      /** Generate random numbers within the length ranges of each array, use 
        those numbers to pick index locations from the arrays, and String those
        locations' values together to form the passwords.  Repeat (and print) 10x.
      */
      for (int counter = 1; counter <= 10; counter++)
      {
      int randCluster = (0 + (int)(Math.random() * ((83-0) + 1)));
      int randVowel = (0 + (int)(Math.random() * ((4-0) + 1)));
      int randConsonant = (0 + (int)(Math.random() * ((20-0) + 1)));
      int randVowel2 = (0 + (int)(Math.random() * ((4-0) + 1)));
      int randCluster2 = (0 + (int)(Math.random() * ((36-0) + 1)));
      int randVowel3 = (0 + (int)(Math.random() * ((4-0) + 1)));
      int randCluster3 = (0 + (int)(Math.random() * ((36-0) + 1)));
     
      String bit1 = cluster_start[randCluster];
      String bit2 = vowel[randVowel];
      String bit3 = consonant[randConsonant];
      String bit4 = vowel[randVowel2];
      String bit5 = cluster_end[randCluster2];
      String bit6 = vowel[randVowel3];
      String bit7 = cluster_end[randCluster3];
     
      System.out.println(bit1 + bit2 + bit3 + bit4 + bit5 + bit6 + bit7);
      }
     }
    }

    Many thanks for looking it over!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: "null" in my output :( Scanner problem?

    Which print statement prints the line with the null?
    Is there a variable with a null value that is being printed?
    Find the variable that has the null value, then backtrack in the code to see where it gets the null value. Use println statements to print out the contents of the arrays and the values of variables used to index the array.
    If you don't understand my answer, don't ignore it, ask a question.

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

    crys (December 15th, 2012)

  4. #3
    Junior Member
    Join Date
    Sep 2012
    Location
    San Francisco, CA
    Posts
    10
    My Mood
    Yeehaw
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: "null" in my output :( Scanner problem?

    Ah! YES, there was a variable with a null value being printed...the "null" was showing up where the cluster_end value should be. My final value in the cluster_end array was null.

    It looks like my for loop in the section where I'm reading from the second file to populate the array cluster_end stops one line early -- (index = 0; index < 36; index++) ought to be (index=0; index <37; index++) -- ...so my final value was null because I stopped reading lines into the array before I filled it. Derp!

    I have fixed this, and my nulls have disappeared.

    Thank you, Norm!

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: "null" in my output :( Scanner problem?

    Glad you found it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. String.split("") puts null in the first index
    By sonicjr in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 10th, 2023, 03:11 AM
  2. error with "Scanner" class in basic calculator program.
    By wheezebeez in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 2nd, 2012, 09:49 AM
  3. Bizarre Issue with Scanner Failing to "notice" rest of file
    By nitrogenFingers in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: November 14th, 2011, 11:56 PM
  4. Scanner.useDelimiter("\n") Problem
    By Noob_Programmer in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 31st, 2011, 09:28 AM
  5. Replies: 4
    Last Post: August 13th, 2009, 05:54 AM