Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6932161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:40:15+00:00 2026-05-27T11:40:15+00:00

I am learning java, using the book Java how to program. I am solving

  • 0

I am learning java, using the book “Java how to program”. I am solving exercises. In this actual exercise I am supposed to make a program which reads an integer from the user. The program should then display a square of asterisks (*) corresponding to the integer read from the user. F.eks user inputs the integer 3, the program should then display:

***
***
***

I try to nest a while-statement inside another, the first one to repeat the asterisks on one line, the other one to repeat this the right amount of times. Unfortunately, I only get the program to display one line. Could anyone tell me what I am doing wrong please?
The code is as follows:

import java.util.Scanner;
public class Oppgave618 
{

    public static void main(String[] args) 
    {
    int numberOfSquares;
    Scanner input = new Scanner(System.in);
    System.out.print("Type number of asterixes to make the square: ");
    numberOfSquares = input.nextInt();

        int count1 = 1;
    int count2 = 1;

    while (count2 <= numberOfSquares)

        {
        while (count1 <= numberOfSquares)
            {
            System.out.print("*");
            count1++;
            }
        System.out.println();
        count2++;
        }

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:40:15+00:00Added an answer on May 27, 2026 at 11:40 am

    You should reset count1 back in each iteration of the outer loop

    public static void main(String[] args)  {
        int numberOfSquares;
        Scanner input = new Scanner(System.in);
        System.out.print("Type number of asterixes to make the square: ");
        numberOfSquares = input.nextInt();
                 //omitted declaration of count1 here
        int count2 = 1;
        while (count2 <= numberOfSquares) {
            int count1 = 1; //declaring and resetting count1 here
            while (count1 <= numberOfSquares) {
                System.out.print("*");
                count1++;
            }
            System.out.println();
            count2++;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using the Learning Java 2nd Edtion book to try make my
I'm currently learning Java, using a combination of the Head First Java book and
I am learning Java using the book Java: The Complete Reference. Currently I am
I am learning Java using Java how to program (Deitel and Deitel). Right now
I am in the process of learning Java and the book I am using
I'm learning Java, and using this for statement for looping: for (int i =
I'm learning Java here, and using a GLabel object. It's in the ACM Graphics
Forgive my ignorance - still learning here. I am using Eclipse Ganymede (Java 1.5)
I'm learning Java by reading the online Java book, but im finding it hard
I've spent the last year learning and using Java in university but we didn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.