This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes reading data from keyboard Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reading data from keyboard" Watch "reading data from keyboard" New topic

reading data from keyboard

krish reddy

Joined: Sep 09, 2005
Posts: 10
this is a program which im trying for sum of a harmonic series, the value of n should be given by the confused
class harmonic
public static void main(String args[])
DataInputStream in=new DataInputStream(;
int n=0;
int i;
float sum=1;
System.out.println("Enter an interger: ");
catch(Exception e){}
System.out.println("sum of harmonic series ="+sum);
the answer i always get is 2
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24168


Welcome to JavaRanch! The result of dividing an int by an int is an int; after the first one, your division always comes out to zero, as fractional parts are truncated.

Instead do something like

By making one of the operands a float, the result becomes a float.

[Jess in Action][AskingGoodQuestions]
krish reddy

Joined: Sep 09, 2005
Posts: 10
thank u very much, the program is working now, i have been preparing for scjp and this has been a good start.
wood burning stoves
subject: reading data from keyboard
Similar Threads
while loops and all that fun stuff, need assignment help
deprecated API
using rational class for summation series
Evaluating values
simple question, drawing a blank