Converting String Input to Numbers Example

/*************************************************
Name: Payroll.java
Output: Prints out total salary to the console using JOptionPane and System.out methods
Date: November 23, 2008
Author: Mr. P
*************************************************/

import javax.swing.*;
public class Payroll{ public static void main(String [] args)
{
int numOfHours;
String numOfHours_str;

double hourlyWage, totalSalary;
String hourlyWage_str;

numOfHours_str = JOptionPane.showInputDialog ("How many hours did you work this week?");
numOfHours = Integer.parseInt (numOfHours_str);

hourlyWage_str = JOptionPane.showInputDialog ("What is your hourly wage?");
hourlyWage = Double.parseDouble (hourlyWage_str);

totalSalary = numOfHours * hourlyWage;

JOptionPane.showMessageDialog (null,"Your total salary for this week is: $" +totalSalary);
System.out.println("Your total salary for this week is: $" +totalSalary);
}
}

1 comment:

gillberk said...

The input string can contain one or more numbers separated by spaces, commas, or semicolons, such as '5', '10,11,12', or '5,10;15,20'. In addition to numerical values and delimiters, the input string can also include a decimal point, leading + or - signs, the letter e or d preceding a power of 10 scale factor, or the letter i or j indicating a complex or imaginary number.
---------------
Gillberk

NEW WAY TO ADVERTISE