0

I'm at the last part of my HW and it's to "Write out the square of the numbers starting with the user entered number, and up to 100." We have to use JOPtionPane for this problem. My for loop keeps getting errors, right now it says the String cannnot be converted to int.

I've tried converting i to an int by using int i = Integer.parseInt(); before the for loop but that didn't work and I didn't expect it to because it feels off.

String n =  JOptionPane.showInputDialog("Enter a number 1-50");  
int number = Integer.parseInt(n);     
while(number<1 || number>50)
{
     n = JOptionPane.showInputDialog("Bad number, Enter a number 1-50");
     number = Integer.parseInt(n);  

}

String filename = JOptionPane.showInputDialog("What is the name of the file?");

File f = new File(filename);
FileWriter fw = new FileWriter(filename);
PrintWriter pw = new PrintWriter(fw);
//PrintWriter file = new PrintWriter(filename)
//file.println(filename);

for(int i=n; i<=100; i*i)

5
  • 2
    n is still a String. You meant to use number Commented Mar 25, 2019 at 19:19
  • Just switched it to number, now getting the error "not a statement." for(int i=number; i<=100; i*i) Commented Mar 25, 2019 at 19:24
  • 1
    Did you mean i = i*i? Commented Mar 25, 2019 at 19:26
  • 1
    @mpasko256 No, they meant i++. Commented Mar 25, 2019 at 19:28
  • Just ran it and it's almost perfect! I used the number '2' but it was only squared twice. for(int i=number; i<=100; i = i*i) { pw.println("your numbers are " + i); } pw.close(); //filename.close(); When i tried i++ the loop just kept on running and printing out whatever number I entered, it didn't square it. Commented Mar 25, 2019 at 19:36

1 Answer 1

1

You probably need this:

int number = 4;
int currentNumber = 0;
while (++number <= 100 && currentNumber < 100) {
    currentNumber = number * number;
    pw.println("your numbers are " + currentNumber);
}

Output:

your numbers are 25
your numbers are 36
your numbers are 49
your numbers are 64
your numbers are 81
your numbers are 100
Sign up to request clarification or add additional context in comments.

8 Comments

That helped but it doesn't stop at 100, for some reason it goes all the way up to 10,000
The loop should stop when the value of number is 10
@Nitro It prints the square root not the number itself.
Do you need to print square of all number between the choosen one and 100 or you just want to print numbers up to 100? Because 10 * 10 is already 100 and you have this condition in your code while(number<1 || number>50)
Write out the square of the numbers starting with the user entered number, and up to 100 - is what I need to do. It stops at 100 now but when I entered 4 and ran it the program wrote your numbers are 25 your numbers are 36 your numbers are 49 your numbers are 64 your numbers are 81 your numbers are 100
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.