Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can anyone explain the flow of the code

 
bala kris
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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);
}
}
 
Shrikanth Nekkanti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • 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?
 
Francesco Bianchi
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • 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.
 
prathibha ananthapadmanabha
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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.
 
shazar kv
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic