• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

can anyone explain the flow of the code

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;

public class P4
{
public static void main (String [] args)
{
short num;
Scanner keyboard=new Scanner (System.in);

System.out.println("Number?");
quad1=keyboard.nextShort();

String bi;
while (num>=1)
{
if(num%2==0)
bi="0";
if(num%2==1)
bi="1";
num/=2;
}
System.out.println(bi);
}
}
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mr Bala krish.
Usually for accepting input from keyboard we use Streams(InputStream , OutputStream).
The procedure of accepting input which i know is

//First attach keyboard to InputStreamReader
InputStreamReader ios=new InputStreamReader(System.in);

//attach ios to bufferedReader
BufferedReader br=new BufferedReader(ios);

//accept input using br

System.out.println("Number?");
String str=br.readLine();

If you know this procedure neglect this reply.
Thank you
 
Marshal
Posts: 79969
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bala Kris is quite correct to use Scanner, which was introduced in Java 5 and (I think) already incorporates the stream and buffered readers.
What output do you get? Can you work it out from reading the code?
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by bala kris:
import java.util.*;

short num;

//...
while (num>=1)



This code doesn't even compile.. You cannot use a local variabile if you have not initialized it yet.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes , I think that this code will not compile because the local variable
"num" is used before it is initialized .
Please advice if there is any other flaws / findings other than this in the code .

Regards,
Prathibha.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//initialize num variable as it is a local variable
short num = 0;
// used for inputting data through keyboard.Alternative to Streams which is newly introduced in jdk1.5.Import java.util.Scanner inorder to use this
Scanner keyboard=new Scanner (System.in);
System.out.println("Number?");
// Assigning num variable whatever number you have entered through keyboard
// nextShort() of Scanner class Scans the next token of the input as a short.
num=keyboard.nextShort();
//initialize String variable bi as it is declared locally
String bi = "";
while(num>=1)
{
if(num%2==0)
bi="0";
if(num%2==1)
bi="1";
num/=2;
}
System.out.println(bi);

The output will always be "1" as the statement "num/=2" will make the num an odd value even if you have entered an even value
 
Campbell Ritchie
Marshal
Posts: 79969
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right about local variables not initialised yet.
You want to move the "print" statement inside the "while" loop. Then it is quite possible to get several "0"s.

Once you have compiled it, you need to run it and see what the output is. Then work out what it is doing.
 
Francesco Bianchi
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Little hint: declare variable as late as possible, better if in the same moment when you initialize/use them the first time.

Originally posted by shazar kv:

short num = 0;
Scanner keyboard=new Scanner (System.in);
System.out.println("Number?");
num=keyboard.nextShort();



would be better written this way:




In the following case, always taken from you code, it's ok to declare a variable a bit before is it used because of a problem of scopes

Originally posted by shazar kv:

String bi = "";
while(num>=1)
{
if(num%2==0)
bi="0";
if(num%2==1)
bi="1";
num/=2;
}
System.out.println(bi);





And now..a question: what you are tryng to do with this program is printing the binary representation of the user-digited number?
 
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic