public class NestedWhileLoop {
public static void main (String[] args) {
int num_stars,
num_lines,
which_star,
which_line;
// System.out.print("How many stars? ");
// num_stars = kb.nextInt();
// System.out.print("How many lines? ");
// num_lines = kb.nextInt();
num_stars = getInteger("How many stars? ");
num_lines = getInteger("How many lines? ");
which_line = 1;
while (which_line <= num_lines) {
which_star = 1;
while(which_star <= num_stars) {
System.out.print("*");
which_star++;
}
which_line++;
System.out.println();
}
} //End of main
public static int getInteger(String msg) {
final int MIN_NUM = 2;
Scanner kb = new Scanner(System.in);
int num;
do {
System.out.print(msg + "(" + MIN_NUM + " or more): ");
num = kb.nextInt();
}while (num < MIN_NUM);
return num;
}
}
Brace on line 26 in in the wrong direction.
getInteger ()
needs a return statement
and has one brace too many at the end.
You need
static Scanner kb = new Scanner(System.in);
above main ().
and take it out of getInteger ()
You only have one keyboard. When you call getInteger () the 2nd time it tries to create a second keyboard (because of the "new" keyword).
import java.util.Scanner;
public class NestedWhileLoop
{
public static void main (String[] args)
{
int num_stars,
num_lines,
which_star,
which_line;
// System.out.print("How many stars? ");
// num_stars = kb.nextInt();
// System.out.print("How many lines? ");
// num_lines = kb.nextInt();
num_stars = getInteger("How many stars? ");
num_lines = getInteger("How many lines? ");
which_line = 1;
while (which_line <= num_lines)
{
which_star = 1;
while(which_star <= num_stars)
{
System.out.print("*");
which_star++;
{
which_line++;
System.out.println();
}
} //End of main
public static int getInteger(String msg)
{
final int MIN_NUM = 2;
Scanner kb = new Scanner(System.in);
int num;
do
{
System.out.print(msg + "(" + MIN_NUM +
" or more): ");
num = kb.nextInt();
}while (num < MIN_NUM);
}
}
}
The illegal start of expression is on line 32. Any ways to fix this? Thanks!