File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes can anyone explain the flow of the code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can anyone explain the flow of the code" Watch "can anyone explain the flow of the code" New topic
Author

can anyone explain the flow of the code

bala kris
Greenhorn

Joined: Jul 10, 2007
Posts: 5
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

Joined: Jul 12, 2007
Posts: 48
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

Joined: Oct 13, 2005
Posts: 38007
    
  22
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

Joined: Jun 22, 2007
Posts: 62
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.


SCJP 5 & 6, SCWCD 5, SCBCD 5
prathibha ananthapadmanabha
Greenhorn

Joined: Jul 12, 2007
Posts: 16
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

Joined: Mar 16, 2007
Posts: 7
//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

Joined: Oct 13, 2005
Posts: 38007
    
  22
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

Joined: Jun 22, 2007
Posts: 62
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?
 
wood burning stoves
 
subject: can anyone explain the flow of the code
 
Similar Threads
Array in reverse order......?
how to calculate the N to the power of N
when i run my virtual machine just keeps going, but no output.
diamond pattern without parse.int (Someone help me T.T) :cry:
Recursive method to print prime factorization of a number