Results 1 to 6 of 6

Thread: tlouvierre

  1. #1
    tlouvierre is offline Member
    Join Date
    Apr 2009
    Location
    I live Stafford Texas right outside of Houston.
    Posts
    78
    Rep Power
    0

    Talking tlouvierre

    [code]
    import java.util.Scanner;

    public class Main
    {

    double rate;
    double hours;
    String Empname;


    // main method begins execution of Java application
    public static void main(String args[])

    {
    boolean stop = false; // controls if loop below is executed
    while (!stop)
    {
    // create Scanner to obtain input from command window
    Scanner input = new Scanner(System.in);

    System.out.print("Enter employee's name or stop to quit: "); // prompt for name
    String EmpName = input.nextLine(); // read employee's name entered

    if (EmpName.equals("stop"))
    {
    System.out.println("This program has ended successfully.");
    stop = true;
    }
    else
    {
    double PayRate; // First number multiplied
    double HoursWorked; // Second number multiplied
    double total; // Total of PayRate * HoursWorked

    System.out.print("Enter the pay rate per hour of the employee: "); // prompt for rate
    PayRate = input.nextDouble(); // read payrate of employee
    while (PayRate <= 0) // Validate for positive number
    {
    System.out.println();
    System.out.println("Pay rate must be a positve number. ");
    System.out.print("Please enter pay rate per hour again: ");
    PayRate = input.nextDouble();
    System.out.println();
    }
    System.out.print("Enter the employee's hours worked this pay week: "); // prompt for Hours
    HoursWorked = input.nextDouble();
    while (HoursWorked <= 0) // validate for positive number
    {
    System.out.println();
    System.out
    .println("Hours worked must be a positive number. ");
    System.out.print("Please enter hours worked this pay week again: ");
    HoursWorked = input.nextDouble(); //read hourly rate of pay
    System.out.println();
    }
    total = (double) PayRate * HoursWorked; // Multiply PayRate by HoursWorked
    System.out.println();
    System.out.println("Employee: " + EmpName); //display name
    System.out.println("Number of hours worked this pay week: " + HoursWorked); //display hours worked
    System.out.printf("Employee's Pay: $%,.2f\n", total); //display pay rate
    System.out.println();
    System.out.println();
    } // end else
    } //end while
    } // end main
    } // end class

    [code]

    In the earlier part of code I use Scanner = new Scanner etc. Is that a constructor because it looks nothing like what the teacher is giving examples of.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    That is how someone uses a constructor, it is not a constructor for your class, if that is what you're asking.

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Scanner != constructor

    No, the Scanner class is not a constructor. Scanner is a class that can parse primitive types and strings using regular expressions

    Scanner (Java Platform SE 6)

    Someexamples how to use the Scanner class:

    Scanner**File**Java Tutorial

    Constructor declarations look like method declarations—except that they use the name of the class and have no return type

    Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Luck,
    CJSL
    Last edited by CJSLMAN; 05-13-2009 at 03:17 PM. Reason: Corrected some serious typos
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    That post would, IMHO, confuse a newbie, since the line referenced contains the use of a contructor. "new Scanner(InputStream)" is a constructor call. A Scanner is not a "method", it is a Class. Some tutorials may refer to a "Scanner method", but they mean "Scanner methodology". They do not mean that Scanner is not a Class.

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default ooppss..

    I'm sorry... yes , you are correct....Scanner is a class, not a method (I haven't had my first cup of coffee yet :-). I've corrected my post to reflect that and apologize for any confusion.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

Similar Threads

  1. tlouvierre
    By tlouvierre in forum New To Java
    Replies: 5
    Last Post: 05-28-2009, 05:03 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
  •