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

  • Home
  • SEARCH
  • 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 7491225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:56:39+00:00 2026-05-29T15:56:39+00:00

I’m doing a magicsquare program that allows a user to input numbers >0 to

  • 0

I’m doing a magicsquare program that allows a user to input numbers >0 to form a magicsquare. What a magic square is, is pretty much a square, meaning that n has to have n(squared) numbers. Much like ticTacToe, all the rows, columns, and diagonals each have the same sum to be considered a magic square When I run my program, It always confuses the 2D array set and claim that the set of numbers are a magicsquare when usually, it isnt necessarily so. Please help!

import java.util.Scanner;

public class SquareRunner
{
 public static void main(String[] args)
 {
     Scanner in = new Scanner(System.in);
     Square test = new Square();
     System.out.println("Enter a row of integers. When you are finished, type 'n' in a new line");
     boolean flag = false;
     while(!flag)
     {
        String numbers = in.next();
        if(numbers.equals("n"))
        flag = true;
        else
        test.add(numbers);
     }
     test.isMagic();

 }
}

public class Square
{
  private int[][] values;
  private int row;

public Square()
{
    row = 0;
}

public void add(String numbers)
{
  int b = 1;
  int amount = numbers.length();
  values = new int[amount][amount];

  for(int j =0;j<amount;j++)
  {
      String a = numbers.substring(j,b);
      int convert = Integer.parseInt(a);
      values[row][j] = convert;

      b++;
   }
    row++;

}


public Boolean isMagic()
{
    int checkAmountColumns = values[0].length;
    int checkAmountRows = values.length;
    int isSquare = checkAmountColumns * checkAmountRows;

        for(int q = 0;q<values.length;q++)
        {
            for(int w=0;w<values[0].length;w++)
            {
                int checkZero = values[q][w];
                if(checkZero == 0)
                {
                    System.out.print("To be a perfect square, your number of rows and columns, n must be a perfect ");
                    System.out.println("Square i.e. 9 total numbers is 3 numbers per row");
                    return false;
                }
            }
        }
    if(checkAmountColumns != checkAmountRows || Math.sqrt(isSquare) != checkAmountColumns)
    {
        System.out.print("To be a perfect square, your number of rows and columns, n must be a perfect ");
        System.out.println("Square i.e. 9 total numbers is 3 numbers per row");
        return false;
    }

  else
    {
        int magicNumber = 0;
        int counter = 0;
        int compareTo = 0;

       //row to row
        for(int i =0;i<values.length;i++)
        {
            for(int j = 0;j<values[0].length;j++)
            {

                values[i][j] += compareTo;
                if(counter == 0)
                values[i][j] += magicNumber;

            }
            counter ++;
            compareTo = 0;
            if(compareTo != magicNumber)
            {
                System.out.println("This Selection of numbers is not a perfect square");
                return false;
            }
        }

        //column to column
        for(int i =0;i<values[0].length;i++)
        {
             for(int j = 0;j<values.length;j++)
             {

                  values[j][i] += compareTo;
                  if(counter == 0)
                  values[j][i] += magicNumber;

              }
              counter ++;
              compareTo = 0;
              if(compareTo != magicNumber)
              {
                System.out.println("This Selection of numbers is not a perfect square");
                return false;
              }

        }
        System.out.println("This selection of numbers is a MagicSquare!");
        return true;

    }
}
}
  • 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-29T15:56:39+00:00Added an answer on May 29, 2026 at 3:56 pm

    The first thing I notice is that your add() method probably won’t work correctly. Every time you call it, you overwrite the previous values member with a newly allocated array. This throws away the previous row that you entered.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm trying to create an if statement in PHP that prevents a single post

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.