Jake0991 Jake0991 - 26 days ago 7
Java Question

How to print tabs according to row number

I'm trying to write a program to output a set of 2 arrays entered by the user. It's a poem generator, the user enters a number of nouns followed by a number of adjectives, then my program combines an adjective with a noun randomly and displays it on the screen. A noun or an adjective cannot be chosen again after being already displayed. My problem is that I have to print a certain amount of tabs according the the rows, and then print the poem combination. For example, row 1 = 1 tab, row 2 = 2 tab, etc.

Here is a sample of the output desired:

https://postimg.org/image/5mp3niqvx/

And here is my code:

import java.util.Scanner;
public class A3Question1
{

public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
boolean loop1 = false;
boolean loop2 = false;
boolean loop3 = false;
int numNouns = 0, numAdjectives = 0;
String[] nouns = new String[numNouns];
String[] adjectives = new String[numAdjectives];

System.out.println("-----------------------------------------");
System.out.println(" Let's write a poem! ");
System.out.println("-----------------------------------------");
System.out.println();

while (!loop1)
{
System.out.print("How many nouns? (min 3): ");
numNouns = keyboard.nextInt();

if (numNouns < 3)
{
continue;
}
else
{
loop1 = true;
}

System.out.println("Enter " + numNouns + " nouns: ");
nouns = new String[numNouns];

for (int i = 0; i < numNouns; i++)
{
nouns[i] = keyboard.next();
}
}
while (!loop2)
{
System.out.print("How many adjectives? (min 3): ");
numAdjectives = keyboard.nextInt();

if (numAdjectives < 3)
{
continue;
}
else
{
loop2 = true;
}

System.out.println("Enter " + numAdjectives + " adjectives: ");
adjectives = new String[numAdjectives];

for (int j = 0; j < numAdjectives; j++)
{
adjectives[j] = keyboard.next();
}
}

while (!loop3)
{
System.out.println("\n-----------------------------------");
System.out.println(" Here is my Java Poem! ");
System.out.println(" **LOOK AROUND** ");
System.out.println("-----------------------------------");
System.out.println();

boolean[] noDuplicate1 = new boolean[numNouns];
boolean[] noDuplicate2 = new boolean[numAdjectives];

for(int i = 0; i < 1000; i++)
{
int num1 = (int) (Math.random() * numNouns);
int num2 = (int) (Math.random() * numAdjectives);

if (noDuplicate1[num1] == false && noDuplicate2[num2] == false)
{
for (int j = 0; j < i; j++)
{
System.out.print("\t");
}
System.out.println(adjectives[num2] + nouns[num1]);
noDuplicate1[num1] = true;
noDuplicate2[num2] = true;
}
}

System.out.print("\nAnother poem? (y/n): ");
String again = keyboard.next();

if (again.charAt(0) == 'y' || again.charAt(0) == 'Y')
{
continue;
}
else
{
loop3 = true;
}
}

System.out.println("Thank you for using the POEM GENERATOR! Have a good day!");

keyboard.close();
}


}

Answer

The bug in your code is this part:

for (int j = 0; j < i; j++)
{
    System.out.print("\t");
}

Here, i is the number of iterations after which you found a non-duplicate noun and adjective.

Rather, add a new variable, called numTabsToPrint or something, that tracks how many tabs you have to print in each line, and increment it by one after printing each line.

Here's the changed snippet of your code, where I have explicitly commented on changed lines:

        boolean[] noDuplicate1 = new boolean[numNouns];
        boolean[] noDuplicate2 = new boolean[numAdjectives];

        int numTabsToPrint = 0;                             // Tracks number of tabs to print on each line.

        for(int i = 0; i < 1000; i++)
        {               
            int num1 = (int) (Math.random() * numNouns); 
            int num2 = (int) (Math.random() * numAdjectives);

            if (noDuplicate1[num1] == false && noDuplicate2[num2] == false)
            {
                for (int j = 0; j < numTabsToPrint; j++)    // change 'i' to 'numTabsToPrint' in your original code.
                {
                    System.out.print("\t");
                }
                System.out.println(adjectives[num2] + nouns[num1]);
                noDuplicate1[num1] = true;
                noDuplicate2[num2] = true;
                numTabsToPrint++;                           // increment after printing current line.
            }
        }

Working example of the complete code can be found here: http://ideone.com/Gf71Ty

Comments