"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. :D
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. ~X( 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.
Code Java:
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!
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.
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. <:-P
Thank you, Norm!
Re: "null" in my output :( Scanner problem?