0

The following code compiles in jgrasp, but it reads out You null null. I can't figure out how to get my text files to read and store into their arrays?

import java.io.*; 
import java.util.Scanner;
import java.util.Random;
public class InsultGenerator {

//randomly picks an adjective and a noun from a list of 10 random nouns and adjectives
//it then creates a random insult using one adjective and one noun
public static void main (String [] args)throws IOException
{
    String[]adjectives = new String [10];
    String[]nouns = new String [10];
    int size = readFileIntoArray (adjectives);
    int size2 = readFileIntoArray2 (nouns);
        String adjective = getAdjective(adjectives, size);
    String noun = getNoun(nouns, size2);
    printResults(adjectives, nouns, adjective, noun );
}

public static int readFileIntoArray (String[] adjectives)throws IOException
{  
    Scanner fileScan= new Scanner("adjectives.txt");
    int count = 0;  
    while (fileScan.hasNext()) 
    {
        adjectives[count]=fileScan.nextLine();
        count++;
    }
    return count;
}
public static int readFileIntoArray2(String[] nouns)throws IOException
{
    Scanner fileScan= new Scanner("nouns.txt");
    int count = 0;  

    while (fileScan.hasNextLine()) 
    {
        nouns[count]=fileScan.nextLine();
        count++;
    }   
    return count;
}
public static String getAdjective(String [] adjectives, int size)
{
    Random random = new Random();
    String adjective = "";
    int count=0;
    while (count < size)
    {
        adjective = adjectives[random.nextInt(count)]; 
        count ++;
    }
    return adjective;
}
public static String getNoun(String[] nouns, int size2)
{
    Random random = new Random();
    String noun = "";
    int count=0;
    while (count < size2)
    {
        noun = nouns[random.nextInt(count)]; 
        count ++;
    }
    return noun;
}
public static void printResults(String[] adjectives, String[] nouns, String adjective, String noun) 
{
    System.out.println("You " + adjective + " " + noun);
}
}

The teacher wants us to use run argument and put each text file in there. So my run argument says adjectives.txt, nouns.txt (each of those files is a list of 10 nouns or adjectives).
I want to store them into arrays then get the program to randomly pick one from each list and make a statement.

0

2 Answers 2

1

You should use new Scanner(new File("adjectives.txt")). Also as you need to use command arguments - use them! Write method that takes filename and returns array of strings:

public String[] readLines(String filename) throws IOException {
    String[] lines = new String[10];
    Scanner fileScan= new Scanner(new FIle(filename));
    // read lines
    // ...
    return lines;
}

This way you don't need to have 2 almost identical method readFileIntoArray(2).

Sign up to request clarification or add additional context in comments.

Comments

0

As mentioned in another answer, use the correct syntax for your Scanners.

Also, check your getNoun() and getAdjective() methods. I'm not sure they produce the expected result, and if they do, they seem a bit complicated. Try something like this:

public static String getString(String[] str) {      
    Random rand = new Random();

    String retVal = str[rand.nextInt(str.length)];

    return retVal;
}

Java arrays have their size stored in the instance variable length. nextInt(int upperBound) also need an upper bound.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.