aspose file tools*
The moose likes Beginning Java and the fly likes Troubles with output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Troubles with output" Watch "Troubles with output" New topic
Author

Troubles with output

Tricia Lemay
Greenhorn

Joined: Sep 16, 2004
Posts: 15
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

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Mar 11, 2002
Posts: 1140
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.


Mani
Quaerendo Invenietis
Tricia Lemay
Greenhorn

Joined: Sep 16, 2004
Posts: 15
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

Joined: Sep 16, 2004
Posts: 15
Got it working, just needed to change around the calculates and the gets!

Thanx again!
 
Consider Paul's rocket mass heater.
 
subject: Troubles with output