Results 1 to 3 of 3
  1. #1
    bettyhagan is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default adding printf to system.in file

    Please help,

    I have a scanner input that works up until I disply the the donation amount. How can I word the program to get the system to show $25.

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package addressbook;
    //import scanner
    import java.util.Scanner;
    //
    //
    // @author Betty
    //create class
    public class addressBook {

    //
    //
    //Method execution
    public static void main(String[] args) {
    //set scanner input
    Scanner in = new Scanner(System.in);

    //declare variables
    String firstName= new String();
    String lastName= new String();
    String streetAddress= new String();
    String cityName= new String();
    String stateName= new String();
    String zipCode= new String();
    String donationNumber = new String();
    String donationAmount= new String();

    //set constructor

    System.out.println("Please Enter Your First Name:\n");
    firstName = in.nextLine();
    System.out.println("Enter Your Last Name:\n");
    lastName=in.nextLine();
    System.out.print("Enter Street Address:\n");
    streetAddress=in.nextLine();
    System.out.print("Enter City:\n");
    cityName=in.nextLine();
    System.out.print("Enter State:\n");
    stateName=in.nextLine();
    System.out.print("Enter Zip Code:\n");
    zipCode=in.nextLine();
    System.out.print("Enter Number of Donations:\n");
    donationNumber=in.nextLine();
    System.out.print("Enter Amount of Donation:\n");
    donationAmount=in.nextLine();


    //Method to display address information

    System.out.println(firstName + "");
    System.out.println(lastName + "");
    System.out.println(streetAddress +"");
    System.out.println(cityName + "");
    System.out.println(stateName + "");
    System.out.println(donationNumber + "");
    System.out.printf(donationAmount + "$%d%n");





    }

    }

  2. #2
    Syahrul is offline Member
    Join Date
    Nov 2009
    Posts
    12
    Rep Power
    0

    Default

    System.out.printf("$%s",donationAmount);

    since u declare donationAmount as String, use %s instead of %d

  3. #3
    bettyhagan is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default thanks

    I kept getting errors when I tried making the donationAmount as a double.
    Thanks this worked.

Similar Threads

  1. Replies: 0
    Last Post: 11-20-2009, 05:16 AM
  2. Printf error
    By ks1615 in forum New To Java
    Replies: 4
    Last Post: 03-10-2009, 06:59 AM
  3. Adding a new class to a jar file
    By Raul Menendez in forum New To Java
    Replies: 2
    Last Post: 08-21-2008, 04:52 PM
  4. Adding file names(Help!!)
    By Gambit17 in forum New To Java
    Replies: 4
    Last Post: 11-09-2007, 07:26 AM
  5. printf
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 04:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •