Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-21-2007, 10:20 PM
Member
 
Join Date: Nov 2007
Posts: 4
Rep Power: 0
nhlfan is on a distinguished road
Default Display program
Hey,
I am trying to have the user input an integer, a float, and a double. For example:

Input the Integer: 7
Input float: 7
Input the double: 66.4

And then display the following output:

THe integer is: 7 The float is: 7.0 The double is: 66.4

My code runs fine, however it only produces the output for an integer, and leaves the float / double as 0.0 :

THe integer is: 7 The float is: 0.0 The double is: 0.0

I have 2 separate files "Show.java" and "UtilShow.java". The code for both of them is below:

Show.java file
Code:
public class Show
{
private int Integer;
private float Float;
private double Double;
 
public Show (int inTeger ) 
{
Integer = inTeger;
}
 
public Show (float fLoat)
{
Float = fLoat;
}
 
public Show (double douBle)
{
 Double = douBle;	
}
 
public void Display()
{
System.out.println("THe integer is: " +Integer + " The float is: " +Float + " The double is: " +Double);
} 
 
}

UtilShow.java file
Code:
import java.io.*;
public class UtilShow{
 
public static void main(String[] args)
{
try {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.print("Input the Integer: ");
Show intone = new Show(Integer.parseInt(br.readLine()));
System.out.print("Input float: ");
Show fone = new Show(Float.parseFloat(br.readLine()));
System.out.print("Input the double: ");
Show done = new Show(Double.parseDouble(br.readLine()));
 
intone.Display();
 
}
catch(Exception e) {}
}
}

Can anybody please help me out? Thanks alot!
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-22-2007, 12:25 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
Rep Power: 0
staykovmarin is on a distinguished road
Default
Right now you are creating a new instance of the class Show for each variable.
Code:
Show intone = new Show(Integer.parseInt(br.readLine()));
intone.Display();
So you are only displaying intone.

Code:
public Show (int inTeger, double dblVal, float  fVal) {..}
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-22-2007, 01:00 AM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
Code:
import java.io.*;

public class UtilShowRx {
    public static void main(String[] args)
    {
        try {
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            System.out.print("Input the Integer: ");
            // Create an instance of class Show and save a reference
            // to it. We'll use this reference, this instance variable,
            // to call methods in the class.
            Show intone = new Show();
            // Use this instance variable to set the Integer field
            // for/in this instance of the Show class.
            intone.Show(Integer.parseInt(br.readLine()));
            System.out.print("Input float: ");
//            Show fone = new Show(Float.parseFloat(br.readLine()));
            // Set the Float field for this instance.
            intone.Show(Float.parseFloat(br.readLine()));
            System.out.print("Input the double: ");
//            Show done = new Show(Double.parseDouble(br.readLine()));
            // Set the Double field for this instance of Show.
            intone.Show(Double.parseDouble(br.readLine()));
 
            intone.Display();
            br.close();
        } catch(Exception e) {}
    }
}

class Show
{
    private int Integer;
    private float Float;
    private double Double;

    // Methods must have a return type in their signature.
    // This method does not return a value so its return
    // type is "void".
    public void Show (int inTeger ) 
    {
        Integer = inTeger;
    }
 
    public void Show (float fLoat)
    {
        Float = fLoat;
    }
 
    public void Show (double douBle)
    {
        Double = douBle;	
    }
 
    public void Display()
    {
        System.out.println("THe integer is: " + Integer +
                           " The float is: " + Float +
                           " The double is: " + Double);
    } 
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing a program within a program gibsonrocker800 New To Java 5 05-12-2008 09:24 AM
How to execute an External Program through Java program Java Tip java.io 0 04-04-2008 03:40 PM
Swing program to display JVM information satya007 AWT / Swing 3 11-13-2007 10:59 AM
My program doesnt display anything Bojevnik AWT / Swing 2 10-19-2007 03:50 PM
How to execute an External Program through Java program JavaBean Java Tips 0 10-04-2007 10:33 PM


All times are GMT +2. The time now is 04:52 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org