Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • December 07, 2016, 12:22:34 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Java code help  (Read 2444 times)

jabborwoc

  • Participant
  • Joined in 2011
  • *
  • Posts: 5
    • View Profile
    • Donate to Member
Java code help
« on: April 17, 2011, 01:34:46 PM »
I am probably way off
// A programs contains the following method:
//public static void display(int arg1, double arg2, char arg3)
//{
// System.out.println("The values are " + arg1 + ", " +
//arg2 + ", and " + arg3);
//}
//Write a statement that calls this method and passes the following varibles as arguements:
//char initial = 'T';
//int age = 25;
//double income = 50000.00

public class PassArg4
{
   public static void main(String[] args)
   {
       
     System.out.println("I am passing values to displayValue.");
           
      System.out.println("Now I am back in main.");
   
  }
       
   public static void display(int arg1, double arg2, char arg3)
   
   {
    int arg1 = 25;
    double arg2 = 50000.00;
    char arg3 = 'T';
 
    display(arg1, arg2, arg3);
    System.out.println("the values are " + arg1 + ", " +
    arg2 + ", and " + arg3);
   
   System.out.println("The values are  " + arg1 + ", " +
    arg2 + ", and " + arg3);
   }
}

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,780
    • View Profile
    • Donate to Member
Re: Java code help
« Reply #1 on: April 17, 2011, 02:36:59 PM »
Something like:
public static void main(String[] args) {
    char initial = 'T';
    int age = 25;
    double income = 50000.00;
    display(age, income, initial);
}

Plain and quite simple, IMHO.

jabborwoc

  • Participant
  • Joined in 2011
  • *
  • Posts: 5
    • View Profile
    • Donate to Member
Re: Java code help
« Reply #2 on: April 17, 2011, 03:19:42 PM »
ok its working now butr it dsiplays 50000.0   <-- thats odd why wont is show 50000.00   ???
// A programs contains the following method:
//public static void display(int arg1, double arg2, char arg3)
//{
// System.out.println("The values are " + arg1 + ", " +
//arg2 + ", and " + arg3);
//}
//Write a statement that calls this method and passes the following varibles as arguements:
//char initial = 'T';
//int age = 25;
//double income = 50000.00


public class PassArg4
{
   public static void main(String[] args)
   {
    
  char initial = 'T';
     int age = 25;
     double income=50000.00;
     display(age, income, initial);

    
     }


// A programs contains the following method:
public static void display(int arg1, double arg2, char arg3)
{
System.out.println("The values are " + arg1 + ", " +
arg2 + " ,  and " + arg3);
}
// Write a statement that calls this method and passes the following varibles as arguments:
// char initial = 'T';
// int age = 25;
// double income = 50000.00
}
Why does it just show 50000.0       <------?
instead of 50000.00    <-- everything else is right
« Last Edit: April 17, 2011, 03:38:26 PM by jabborwoc »

jabborwoc

  • Participant
  • Joined in 2011
  • *
  • Posts: 5
    • View Profile
    • Donate to Member
Re: Java code help
« Reply #3 on: April 17, 2011, 03:39:36 PM »
---------- Capture Output ----------
> "C:\Program Files\Java\jdk1.6.0_23\bin\java.exe" PassArg4
The values are 25, 50000.0 ,  and T

> Terminated with exit code 0.

see is should have 50000.00  , why doesn't it???

ewemoa

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 2,845
    • View Profile
    • Donate to Member
Re: Java code help
« Reply #4 on: April 17, 2011, 06:30:04 PM »
I think the single 0 may be normal behavior for the values in question.  If you want two 0s, may be using something like DecimalFormat will work.

I pasted the following into beanshell (w/ Capture System in/out/err chosen via the File menu):

Quote
import java.text.*;

double arg2 = 50000.00;
String pattern = "###.00";
DecimalFormat df = new DecimalFormat(pattern);
System.out.println(arg2 + " " + pattern + " " + df.format(arg2));

The result was:

Quote
50000.0 ###.00 50000.00

How does that seem?

References:
  http://www.beanshell.org/
  http://www.pp.rhul.ac.uk/~george/PH2150/html/node29.html
  http://download.oracle.com/javase/tutorial/i18n/format/decimalFormat.html

« Last Edit: April 17, 2011, 06:31:40 PM by ewemoa »

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,780
    • View Profile
    • Donate to Member
Re: Java code help
« Reply #5 on: April 18, 2011, 01:59:01 AM »
Or the more generic String.Format() (that uses different format strings) could be used instead of DecimalFormat(), but that's a minor detail.

ewemoa

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 2,845
    • View Profile
    • Donate to Member
Re: Java code help
« Reply #6 on: April 18, 2011, 06:03:52 AM »
FWIW, to use things like System.out.printf, String.format, and friends, I had more luck with beanshell2 than with ordinary beanshell:

http://code.google.com/p/beanshell2/

System.out.printf("num is %.2f\n", Math.PI);
System.out.print(String.format("%.2f", Math.PI));

May be it makes more sense to learn the printf format strings as they are used in other programming languages too...

Reference:
  http://www.java2s.com/Code/JavaAPI/java.lang/System.out.printf.htm
« Last Edit: April 18, 2011, 06:13:01 AM by ewemoa »