13 Replies - 619 Views - Last Post: 04 October 2012 - 05:04 AM Rate Topic: -----

#1 Amadrid064  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-October 12

help with do-while loops.

Posted 03 October 2012 - 02:12 PM

Trying to write a program which will repetitively prompt the user for an integer and then display this odd integer. Every integer can be uniquely written as the product of an odd integer times a power of two. This is what I have so far just need to know what I have wrong.
import java.util.Scanner;

public class Lab4_2
{
  public static void main (String[] args)
  {
    int num, num1;
    
    Scanner scan = new Scanner (System.in);
    
    System.out.print ("Enter an integer: ");
    num1 = scan.nextInt();
    
    do
    {
      num = (num1 % 2) ^2;
      num++;
      System.out.println (num);
      System.out.print("\n");
    }
    while (num <= num1);
  }
}

This post has been edited by jon.kiparsky: 03 October 2012 - 02:23 PM
Reason for edit:: added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: help with do-while loops.

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7629
  • View blog
  • Posts: 12,858
  • Joined: 19-March 11

Re: help with do-while loops.

Posted 03 October 2012 - 02:25 PM

Your while loop looks syntactically sound, but there isn't an operator ^ in java as far as I know.

(maybe it's been added in 7? :) )
Was This Post Helpful? 0
  • +
  • -

#3 Amadrid064  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-October 12

Re: help with do-while loops.

Posted 03 October 2012 - 02:26 PM

So if I wanted to do a power of 2 what should I use instead of ^2?
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7629
  • View blog
  • Posts: 12,858
  • Joined: 19-March 11

Re: help with do-while loops.

Posted 03 October 2012 - 02:32 PM

There's a static method pow(n, p) in java.math which raises n to the p, but I think here you're not raising to a power - at least, not inside the loop. The whole process amounts to raising, but raising to a power is simply repeated multiplication.

That is, 2^8 =

int n=1;
int pow = 8
for (int i = 0; i <pow; i++)
  n *=2;


Was This Post Helpful? 0
  • +
  • -

#5 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: help with do-while loops.

Posted 03 October 2012 - 02:35 PM

You haven't thought through your loop properly. Let's walk through it:

assume num1 = 5.

your loop is
do {
  num = (5 % 2) ^ 2  => num is now  3
  num++              => num is now 4
  (2 printlns)       => you just printed 4
} while (4 <= 5)     => which is always true


since the value of num1 doesn't change in the loop, and num is recalculated from num1 inside the loop, you will never be able to exit the loop.

This post has been edited by jon.kiparsky: 03 October 2012 - 03:44 PM
Reason for edit:: added code tags

Was This Post Helpful? 1
  • +
  • -

#6 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: help with do-while loops.

Posted 03 October 2012 - 02:41 PM

View Postjon.kiparsky, on 03 October 2012 - 02:25 PM, said:

Your while loop looks syntactically sound, but there isn't an operator ^ in java as far as I know.

(maybe it's been added in 7? :) )

Just to clarify, in Java (as well as many other programming languages), the ^ operator is the bitwise xor operator.

The program will compile, you just won't get the expected result.
Was This Post Helpful? 1
  • +
  • -

#7 Amadrid064  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-October 12

Re: help with do-while loops.

Posted 03 October 2012 - 02:43 PM

yeah that is the exact problem I am having. The loop becomes infinite instead of stopping at the smallest answer from the input.
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7629
  • View blog
  • Posts: 12,858
  • Joined: 19-March 11

Re: help with do-while loops.

Posted 03 October 2012 - 03:45 PM

View Postrfs02, on 03 October 2012 - 04:41 PM, said:

Just to clarify, in Java (as well as many other programming languages), the ^ operator is the bitwise xor operator.


That's correct. That ought to teach me about multitasking.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: help with do-while loops.

Posted 03 October 2012 - 08:49 PM

View Postjon.kiparsky, on 03 October 2012 - 05:25 PM, said:

Your while loop looks syntactically sound, but there isn't an operator ^ in java as far as I know.

Yes the XOR operator :) :) :)
Never seen my multiple tutorials on how to swap two int variables without using a temp variable ? (Very usefull in old machines without stack)

   x ^= y;
   y ^= x;
   x ^= y;


if before that code x == 10 and y == 100
after the execution of it x will be == 100 and y will be == 10
:^:
Was This Post Helpful? 0
  • +
  • -

#10 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: help with do-while loops.

Posted 03 October 2012 - 09:22 PM

View Postpbl, on 04 October 2012 - 09:19 AM, said:

View Postjon.kiparsky, on 03 October 2012 - 05:25 PM, said:

Your while loop looks syntactically sound, but there isn't an operator ^ in java as far as I know.

Yes the XOR operator :) :) :)
Never seen my multiple tutorials on how to swap two int variables without using a temp variable ? (Very usefull in old machines without stack)

   x ^= y;
   y ^= x;
   x ^= y;


if before that code x == 10 and y == 100
after the execution of it x will be == 100 and y will be == 10
:^:


What I learned in college to swap two variables without temp was :

a=a+b;
b=a-b;
a=a-b;



Say a=10 and b=5

makes it 1.a=15
2.b=10
3.a=5

The variable values are interchanged without a temp :)

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#11 Amadrid064  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-October 12

Re: help with do-while loops.

Posted 04 October 2012 - 02:47 AM

So I fixed the infinite loop but I am still having trouble finding the odd integers from my inputs. Here is my code
import java.util.Scanner;

public class Lab4_2
{
  public static void main (String[] args)
  {
    int num, num1 = 0;
    
    Scanner scan = new Scanner (System.in);

    do
    {
      System.out.print ("Enter an integer or enter 0 to quit: ");
      num1 = scan.nextInt();
     
      num = (num1 % 2); 
      Math.pow (num,2);
      
      System.out.println (num);
      System.out.print("\n");
    }
    while (num < num1);
    if (num1 == 0)
      System.out.println ("You have quit the program");
  }
}


This post has been edited by smohd: 04 October 2012 - 04:05 AM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#12 Amadrid064  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-October 12

Re: help with do-while loops.

Posted 04 October 2012 - 04:04 AM

sorry for my code not being input right in the thread, trying to figure it out still
Was This Post Helpful? 0
  • +
  • -

#13 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: help with do-while loops.

Posted 04 October 2012 - 04:11 AM

I think you need to check your math again here:
here you will get either 0 or 1:
 num = (num1 % 2);

And here you will get either 0 or 1 because num is either 0 or 1:
Math.pow (num,2);// again you didnt assign the returned value anywhere, so no effect


Now start again to figure out how you will do by hand? What steps will you follow?
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: help with do-while loops.

Posted 04 October 2012 - 05:04 AM

View Postraghav.naganathan, on 04 October 2012 - 12:22 AM, said:

What I learned in college to swap two variables without temp was :

a=a+b;
b=a-b;
a=a-b;


Cute but won't work is a + b > Integer.MAX_VALUE. Don't try it with byte :)
:^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1