# Nested Loops Study Help Needed

Printable View

• 07-11-2010, 08:47 AM
gysnewbie
Nested Loops Study Help Needed
Hello,
I tried to search for any relative threads but as I'm new to Java I have no clue what would be closest to my following issues:

I've been experimenting and self-teaching myself and I've learned a lot about the construction of methods and statements. But sadly, I'm not too bright when it comes to combining both logic and math inside java.
I'm having trouble in figuring out how to use for loops inside nested while loops, while I'm practicing I'm trying to achieve something similar to:

Example input/output:
Enter the next number: 4 [enter]
\$\$\$\$
Enter the next number: 14 [enter]
That is an invalid number.
Enter the next number:
...etc.

I'm clueless about how to break the number 4 into 1 2 3 4 and convert 1 2 3 4 into \$\$\$\$.
After this, the program should ask the user to input another integer and repeat the same thing as above till the user has typed in 5 integers then the program shows the output as symbols.

The other experiment is to create a code that writes a line of asterisks depending on the value of a number. Say that I have an int = 1900; , and I want to divide it by 100, and for every 100 is one asterisk so it would show *******************. This experiment is the same as the first, as I'm trying to convert numbers into symbols according to given data.

I currently don't have any code to provide as I'm struggling with the logical sense of constructing it.

Your help will be very much appreciated, as it will give me some mind peace lol. Thank You
• 07-11-2010, 08:57 AM
JosAH
Quote:

Originally Posted by gysnewbie
Hello,
I tried to search for any relative threads but as I'm new to Java I have no clue what would be closest to my following issues:

I've been experimenting and self-teaching myself and I've learned a lot about the construction of methods and statements. But sadly, I'm not too bright when it comes to combining both logic and math inside java.
I'm having trouble in figuring out how to use for loops inside nested while loops, while I'm practicing I'm trying to achieve something similar to:

Example input/output:
Enter the next number: 4 [enter]
\$\$\$\$
Enter the next number: 14 [enter]
That is an invalid number.
Enter the next number:
...etc.

I'm clueless about how to break the number 4 into 1 2 3 4 and convert 1 2 3 4 into \$\$\$\$.
After this, the program should ask the user to input another integer and repeat the same thing as above till the user has typed in 5 integers then the program shows the output as symbols.

The other experiment is to create a code that writes a line of asterisks depending on the value of a number. Say that I have an int = 1900; , and I want to divide it by 100, and for every 100 is one asterisk so it would show *******************. This experiment is the same as the first, as I'm trying to convert numbers into symbols according to given data.

I currently don't have any code to provide as I'm struggling with the logical sense of constructing it.

Your help will be very much appreciated, as it will give me some mind peace lol. Thank You

So, what I read from your story, what you basically want is a simple method that can print a symbol 's' 'n' times in a row, so, e.g. when you do printSymbol('*', 10) you want ten asterisks printed in a row (line). Well, here goes:

Code:

```public void printSymbol(char s, int n) {   for (int i= 0; i < n; i++)       System.out.print(s);   System.out.println(); }```
The method doesn't return anything (what should it return?) and it uses a loop: it prints symbol 's' 'n' times, just as we wanted. After the loop has finished it moves to the next line. We can use that method for lots of things; e.g. try this little code snippet:

Code:

```for (int j= 1; j <= 5; j++)   printSymbol('*', j);```
First it prints a row of one asterisk, then a row with two asterisks and so on, up to a row with five asterisks. Effectively we have used a nested loop: the one above and the one in the printSymbol method which is called from inside the outer loop above.

kind regards,

Jos
• 07-11-2010, 09:40 AM
gysnewbie
Hey Jos,
Thanks for your quick reply.
However, I need to make sure I'm on right track. So, basically if a user inputs a number X, for example...

int userInput;

userInput = keyboard.nextInt();

how would I manage it to work with int i, and int j; at the for loops? Because I only have one number to work with. Yes, I'm very disoriented at the row/column logic in for loops.
Also, as I stated first, I need it to work with a while loop.
Say that I use this:

while (userInput <= 10)
for (for loop statement logic here)...
{

How would it work so that the while loop won't just show the userInput. I ask because I have often encountered strange outputs where the while loop would result in the initial input but not the result from the for loop.

Please let me know if I make sense, it's hard for me to explain.
• 07-11-2010, 09:47 AM
JosAH
Quote:

Originally Posted by gysnewbie
Hey Jos,
Thanks for your quick reply.
However, I need to make sure I'm on right track. So, basically if a user inputs a number X, for example...

int userInput;

userInput = keyboard.nextInt();

how would I manage it to work with int i, and int j; at the for loops? Because I only have one number to work with. Yes, I'm very disoriented at the row/column logic in for loops.
Also, as I stated first, I need it to work with a while loop.
Say that I use this:

while (userInput <= 10)
for (for loop statement logic here)...
{

How would it work so that the while loop won't just show the userInput. I ask because I have often encountered strange outputs where the while loop would result in the initial input but not the result from the for loop.

Please let me know if I make sense, it's hard for me to explain.

Well, you don't make much sense to me but probably that's just because it's Sunday ;-) I just used 'i' and 'j' for examples and brevity, you can use other names for those variables if you want. Your variable 'userInput' has a value (read from the user) and if you don't change it it stays that way, it doesn't suddenly evaporate or something ... Please can you rephrase your original question again, i.e. what is it exactly that you want?

kind regards,

Jos

ps. and keep that printSymbol( ... ) method for later, you'll need it.
• 07-11-2010, 07:52 PM
gysnewbie
Ok, what I have here is a crappy code lol, but the comments explains what I'm trying to achieve. Basically to use a for loop inside a while loop where the user inputs 5 integers, and after 5 the program terminates. If the user inputs an integer outside of 1-10 range, the program displays an error message.
Outputs should display "\$" symbol instead than numbers.

Code:

``` /** - Have the user input 5 integers ranging from 1 to 10 - For each integer, display output in currency formats, e.g. 4: \$\$\$\$ - If the user writes an integer out of the range of 1-10 display an error message */ import java.util.Scanner; public class Print_Symbol_Experiment {         public static void main (String [] args)         {                         int userInput;                                 Scanner keyboard = new Scanner (System.in);                                 System.out.print("Enter five integers ranging from 1 to 10.");                                 //Ask for first integer, keep asking until the user inputs 5 integers                                 System.out.println("Enter first Integer: ");                                 userInput = keyboard.nextInt(0);                         while (userInput <= 10)                                        printSymbol();                                 public static void printSymbol(char s, userInput)                 {                         for (int i= 0; i < userInput; i++)                         {               System.out.print(s);                           System.out.println();                         }                         for (int j= 1; j <= 5; j++)                         {                           printSymbol('*', j);                         }                 }         } }```
• 07-12-2010, 08:14 AM
JosAH
That code doesn't make any sense at all, it doesn't even compile: you're trying to use a method printSymbol() without any parameters and there isn't any such method; you're also trying to define my method, printSymbol with two parameters, in the middle of a while loop body.

Start simple: try to write a program that prompts the user for a (positive) number and it calls my method with that number and a '*' character, so that, if the user types, say, 7, the line ******* will be printed.

kind regards,

Jos