File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Troubles with output

 
Tricia Lemay
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys. Been messing with this for days. I'm not receiving any compile errors, but only generating 0's for output. I'm missing something but can't put a finger on it. Any suggestions on where to begin looking for a solution?

code

public class TimeInterval
{

private int mySecondTime;
private int myFirstTime;

public TimeInterval(int time1, int time2)
{

int myFirstTime = time1;
int mySecondTime = time2;
}

public int calculateHours()
{
int firstMin=myFirstTime/100*60+myFirstTime%100;
int secondMin = mySecondTime/100*60+mySecondTime%100;
int diff = secondMin-firstMin;
return diff/60;
}

int calculateMinutes()
{
int firstMin=myFirstTime/100*60+myFirstTime%100;
int secondMin = mySecondTime/100*60+mySecondTime%100;
int diff = secondMin-firstMin;
return diff%60;
}

public int getFirstTime( )
{
return myFirstTime;
}

public int getSecondTime( )
{
return mySecondTime;
}

public String toString( )
{
String message;
return message = getFirstTime() + " hours " + getSecondTime() + " minutes";

}


}

Test


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class TimeIntervalTest {
public static void main(String[] args) throws IOException
{

BufferedReader console = new BufferedReader( new InputStreamReader(System.in));
System.out.print("Please enter the first time: ");
String input = console.readLine();
int time1 = Integer.parseInt(input); // convert string to int

console = new BufferedReader( new InputStreamReader(System.in));
System.out.print("Please enter the second time: ");
input = console.readLine();
int time2 = Integer.parseInt(input); // convert string to int

TimeInterval interval = new TimeInterval(time1,time2);

interval.calculateHours();
interval.calculateMinutes();

System.out.println (interval.toString());

System.exit(0);
}
}

Thanx for taking a look!
[ October 26, 2004: Message edited by: Tricia Lemay ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem is here

public TimeInterval(int time1, int time2)
{
int myFirstTime = time1;//<----------
int mySecondTime = time2;//<----------
}

by including the data type (int), you make both variables local to the
constructor. Later on, the code accessing the class fields of the same name, the values accessed are 0.

so it becomes

public TimeInterval(int time1, int time2)
{
myFirstTime = time1;
mySecondTime = time2;
}
 
Mani Ram
Ranch Hand
Posts: 1140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is with your constructer. It should be


When you say

You are actually creating two new variables instead of initializing the data members in the class.
 
Tricia Lemay
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's still not calculating right, but most importantly I am getting data! Now, I'll go back and see why I'm not getting what I want.

Thanks for your help.
 
Tricia Lemay
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it working, just needed to change around the calculates and the gets!

Thanx again!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic