Solutions to the Try It Out Exercises in Beginning Programming with Java For Dummies, 5th Edition
by Barry Burd

Chapter 14: Creating Loops within Loops

In this chapter:

End of the road

The output is JAVA.

Seeing stars

The output is
*****
***
*****

Loop soup

The output is
5
321
4
321
3
321
2
321
1
321

Make some changes around here


public class LoopSoup {

    public static void main(String[] args) {
        int i = 5;
        int j;
        while (i > 0) {
            System.out.println(i);
            i--;
            j = 1;
            while (j < 4) {
                System.out.print(j);
                j++;
            }
            System.out.println();
        }

    }

}

Seeing stars

import java.util.Scanner;

public class SeeingStars1 {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int howMany, count;
        
        System.out.print("How many stars? ");
        howMany = keyboard.nextInt();
        
        count = 0;
        while (count < howMany) {
            System.out.print("*");
            count++;
        }
        
        System.out.println();
        
        keyboard.close();
    }

}
import java.util.Scanner;

public class SeeingStars2 {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int howMany, count;
        char reply;

        System.out.print("Do you want a row of stars? (y/n) ");
        reply = keyboard.findWithinHorizon(".", 0).charAt(0);

        while (reply == 'y') {
            System.out.print("How many stars? ");
            howMany = keyboard.nextInt();

            count = 0;
            while (count < howMany) {
                System.out.print("*");
                count++;
            }

            System.out.println();
            System.out.println();
        
            System.out.print("Do you want a row of stars? (y/n) ");
            reply = keyboard.findWithinHorizon(".", 0).charAt(0);
        }

        keyboard.close();
    }

}

Apropos of nothing

jshell> String name = null;
name ==> null

jshell> System.out.println(name.length());
|  java.lang.NullPointerException thrown:
|        at (#2:1)
You're trying to find the length of a string of characters referred to by the name variable. But the name variable has no value. So Java displays an error message.