I can't figure out why my code isn't printing correctly

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 989 Views - Last Post: 17 October 2013 - 12:46 PM Rate Topic: -----

#1 ThatProgrammerAlex  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 15-October 13

I can't figure out why my code isn't printing correctly

Posted 15 October 2013 - 07:25 PM

Ok so this is what I have so far. When I type in my input, which is 10,000 20,000 and 100,000, the program is supposed to print out the approximations of pi but my code only computes the first input which is 10,000. It prints that out and then prints out the same thing for the last two output statements, no matter what I input for them. Idk what I'm doing wrong. Can someone help me ASAP? This thing is due at midnight

 public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        System.out.print("Enter the number of iterations: ");
        double NumOfIterations1 = input.nextInt();
        int odd=1;
        double pi=0.0;
        for (int i = 1; i <= NumOfIterations1; i++)
        {
            double currentTerm;
            if (i%2==0)
            {
                currentTerm=(double)-4/odd;
            }
            else
            {
                currentTerm=(double)4/odd;
            }
            odd=odd+2;
            pi=pi+currentTerm;
         } 
        double NumOfIterations2 = input.nextInt();
        for (int i = 1; i <= NumOfIterations2; i++)
                    {
            double currentTerm2;
            if (i%2==0)
            {
                currentTerm2=(double)-4/odd;
            }
            else
            {
                currentTerm2=(double)4/odd;
            }
            odd=odd+2;
            pi=pi+currentTerm2;
         }
        
         double NumOfIterations3 = input.nextInt(); 
         for (int i = 1; i <= NumOfIterations3; i++)
         {
            double currentTerm3;
            if (i%2==0)
            {
                currentTerm3=(double)-4/odd;
            }
            else
            {
                currentTerm3=(double)4/odd;
            }
            odd=odd+2;
            pi=pi+currentTerm3;
         }
         
        System.out.println("The value of pi is: " + pi + " when i = " + NumOfIterations1);
        System.out.println("The value of pi is: " + pi + " when i = " + NumOfIterations2);
        System.out.println("The value of pi is: " + pi + " when i = " + NumOfIterations3);
            
        }

    }


*** EDIT ***
Please use code tags when posting code!
:code:

This post has been edited by GunnerInc: 15 October 2013 - 07:26 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: I can't figure out why my code isn't printing correctly

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7290
  • View blog
  • Posts: 12,090
  • Joined: 19-March 11

Re: I can't figure out why my code isn't printing correctly

Posted 15 October 2013 - 07:56 PM

       System.out.println("The value of pi is: " + pi + " when i = " + NumOfIterations1);
       System.out.println("The value of pi is: " + pi + " when i = " + NumOfIterations2);
      System.out.println("The value of pi is: " + pi + " when i = " + NumOfIterations3);
	        


You're printing out the same variable three times in a row, and it's not getting modified in between those prints. Are you surprised that you're getting the same value three times in a row?
Was This Post Helpful? 2
  • +
  • -

#3 ThatProgrammerAlex  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 15-October 13

Re: I can't figure out why my code isn't printing correctly

Posted 15 October 2013 - 08:01 PM

What do you mean it's not getting modified in between the prints? And it's not the same variable, that's why I have 1,2,3 after everything. Maybe I'm just not understanding? Can you help me fix it?

This post has been edited by GunnerInc: 15 October 2013 - 09:03 PM
Reason for edit:: Quote be gone

Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7290
  • View blog
  • Posts: 12,090
  • Joined: 19-March 11

Re: I can't figure out why my code isn't printing correctly

Posted 15 October 2013 - 08:07 PM

Quote

What do you mean it's not getting modified in between the prints


Well, look at the code I quoted. Do you see the value of pi being changed?

Quote

And it's not the same variable, that's why I have 1,2,3 after everything

How many variables do you have to represent the value of pi?

And, uh, yeah, that bit with putting numbers after all your variable names? That usually means you want that to be an array.
Was This Post Helpful? 2
  • +
  • -

#5 ThatProgrammerAlex  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 15-October 13

Re: I can't figure out why my code isn't printing correctly

Posted 15 October 2013 - 08:11 PM

Ohhhh, I got it now. Ok so I made it so I have three variables of pi, they're pi, pi2, and pi3. But now my calculations are coming out wrong except for the first one. Now my second statements computes 6.6666 and third computes 2.564? Why did changing the variable name mess with the calculations?

This post has been edited by GunnerInc: 15 October 2013 - 09:03 PM
Reason for edit:: Quote be gone

Was This Post Helpful? 0
  • +
  • -

#6 ThatProgrammerAlex  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 15-October 13

Re: I can't figure out why my code isn't printing correctly

Posted 15 October 2013 - 08:19 PM

I GOT IT!! Awesome! I got the calculations right but now I just need to know how can I make it so it formats only up to 6 decimal places. It's something like %.6f, right?

This post has been edited by GunnerInc: 15 October 2013 - 09:03 PM
Reason for edit:: Quote be gone

Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7290
  • View blog
  • Posts: 12,090
  • Joined: 19-March 11

Re: I can't figure out why my code isn't printing correctly

Posted 15 October 2013 - 08:22 PM

Something like that. System.out has a printf method that might work for you.
Was This Post Helpful? 0
  • +
  • -

#8 ThatProgrammerAlex  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 15-October 13

Re: I can't figure out why my code isn't printing correctly

Posted 15 October 2013 - 08:33 PM

View Postjon.kiparsky, on 15 October 2013 - 08:22 PM, said:

Something like that. System.out has a printf method that might work for you.


System.out.printf("The value of pi is %1$.6f: when i = " ,pi3, + NumOfIterations3);

So that's my statement and it prints out everything except when i = then the NumOfIterations. It just stops printing after the = sign. How come it's not printing the NumOfIterations as well?
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7290
  • View blog
  • Posts: 12,090
  • Joined: 19-March 11

Re: I can't figure out why my code isn't printing correctly

Posted 15 October 2013 - 08:47 PM

What you're doing here is passing it an extra int that it doesn't need, so it ignores it. If you put in an int specifier somewhere in your format string, it'd pick up that int, though. Try that.

System.out.printf("The value of pi is %1$.6f: when i = " ,pi3, + NumOfIterations3);



(note: +NumOfIterations here means just "NumOfIterations, without any change to its sign" - you're passing it as an argument, not concatenating it to a String)
Was This Post Helpful? 0
  • +
  • -

#10 ThatProgrammerAlex  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 15-October 13

Re: I can't figure out why my code isn't printing correctly

Posted 15 October 2013 - 08:57 PM

Ok, so I don't know what an int specifier is and I tried to look it up and that only confused me more. What's an int specifier? I don't think we've gone over those in class or maybe I dozed off during that part of the lecture.


*** EDIT ***
No need to quote the previous post in your replies.

This post has been edited by GunnerInc: 15 October 2013 - 09:04 PM
Reason for edit:: Quote be gone

Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7290
  • View blog
  • Posts: 12,090
  • Joined: 19-March 11

Re: I can't figure out why my code isn't printing correctly

Posted 15 October 2013 - 09:01 PM

The same way you told it to pick up pi3, which is a float, you can tell it to pick up the number of iterations - but you have to use a slightly different form, because now you're looking at an int. Read up on printf.
Was This Post Helpful? 0
  • +
  • -

#12 ThatProgrammerAlex  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 15-October 13

Re: I can't figure out why my code isn't printing correctly

Posted 15 October 2013 - 09:18 PM

View PostThatProgrammerAlex, on 15 October 2013 - 08:57 PM, said:

Ok, so I don't know what an int specifier is and I tried to look it up and that only confused me more. What's an int specifier? I don't think we've gone over those in class or maybe I dozed off during that part of the lecture.


*** EDIT ***
No need to quote the previous post in your replies.


I looked up some stuff on the printf statement and I can see there's a lot of different code for different things, I think for an int it might be %5d or %s? I'm not sure but I tried using both and the first one gave me error with java.io.PrintStream and java.util.Formatter and the %s just makes it do the same thing as before, it stops printing after the = sign.
Was This Post Helpful? 0
  • +
  • -

#13 M0ns7erS0u1  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 22
  • Joined: 13-October 13

Re: I can't figure out why my code isn't printing correctly

Posted 15 October 2013 - 09:21 PM

printf is a neat method, but it was mainly used to convert legacy code.
(recently new to java) you should use formatting objects instead.
Was This Post Helpful? 0
  • +
  • -

#14 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 401
  • View blog
  • Posts: 998
  • Joined: 14-November 12

Re: I can't figure out why my code isn't printing correctly

Posted 16 October 2013 - 01:40 AM

It's still important to understand how the printf method works.

double firstNumber = 3.141592653589793238462643383279502884197d;
int secondNumber = 10000;
System.out.printf("The value of the first number is %1$.6f when the second number is %2$d", firstNumber, secondNumber);


Use the VarArgs to your advantage.

To go back to your original problem, you fixed that by changing pi into 3 variables.. Why not just print pi and the number of iterations at the point of final calculation?

Your original code, edited:
public class Test {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);

		System.out.print("Enter the number of iterations: ");
		int NumOfIterations = input.nextInt();

		input.nextLine(); // clear the scanner

		int odd = 1;
		double pi = 0.0;
		double currentTerm = 0;

		for (int i = 1; i <= NumOfIterations; i++) {
			if (i % 2 == 0) {
				currentTerm = (double) -4 / odd;
			} else {
				currentTerm = (double) 4 / odd;
			}
			odd = odd + 2;
			pi = pi + currentTerm;
			System.out.printf("The value of pi is %1$.6f when i is %2$d", pi, NumOfIterations);
		}
		NumOfIterations = input.nextInt();
		for (int i = 1; i <= NumOfIterations; i++) {
			currentTerm = 0;
			if (i % 2 == 0) {
				currentTerm = (double) -4 / odd;
			} else {
				currentTerm = (double) 4 / odd;
			}
			odd = odd + 2;
			pi = pi + currentTerm;
			System.out.printf("The value of pi is %1$.6f when i is %2$d", pi, NumOfIterations);
		}

		NumOfIterations = input.nextInt();
		for (int i = 1; i <= NumOfIterations; i++) {
			currentTerm = 0;
			if (i % 2 == 0) {
				currentTerm = (double) -4 / odd;
			} else {
				currentTerm = (double) 4 / odd;
			}
			odd = odd + 2;
			pi = pi + currentTerm;
			System.out.printf("The value of pi is %1$.6f when i is %2$d", pi, NumOfIterations);
		}
	}	
}



Now that the variables have been consolidated, you'll notice that your program just repeats itself 3 times. That can be encased in a loop. Now your program will only be ~20 lines long :^:/>

This post has been edited by Flukeshot: 16 October 2013 - 01:42 AM

Was This Post Helpful? 3
  • +
  • -

#15 ThatProgrammerAlex  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 15-October 13

Re: I can't figure out why my code isn't printing correctly

Posted 16 October 2013 - 12:10 PM

Ok so I tried that but I get slightly different output and my professor is very strict about the output that comes out. He likes it to be the exact number as shown in the assignment prompt but yours gets me slightly different numbers. I'll get points deducted for output. Also, this method takes my program much longer to run as before it took no longer than 5 seconds. Is there a way for me to just have it print the correct way with all my output statements at the bottom?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3