So I have been struggling with this prompt for some time. Basically I need to use dialog boxes in the creation of a program to be used in billing for construction companies. The prompt is as follows. First: Read the following data from separate dialog boxes. This needs to be done for the name of the construction firm, name of the customer, number of labor hours for the foreman, number of workers, number of days on the job, and finally the cost of materials. Second: the following must be calculated from the information obtained from the dialog boxes. The calculations are as follows: Foreman labor cost=foreman labor hours times 60, worker labor cost= number of days times 8 hours per day times number of workers times $30, job fee= Forman labor cost+ worker labor cost+ materials, discount= job fee times 10%, the final amount due= job fee- discount. Lastly, all the data must be printed. The output should look something like Construction firm name, customer:xxxxx, number of days =xxxxx, foreman labor cost =xxxxx, worker labor cost =xxxxx, Materials cost =xxxxx, Job Fee =xxxxx, Discount=xxxxx, Final Amount Due =xxxxx. My question is I cannot get the code to run to produce the output I need using the information from dialog boxes? Every time I try and run the program it is terminated so I am a bit confused I am fairly new to Java so any help would be appreciated The code I have so far is as follows:
package bill;
import javax.swing.JOptionPane;
public class ContractorBill
{
public static void main(String[]args) {
}
double forhours,workers,daysonjob,costofmaterials,flcost,wlcost,jobfee,discount,finalamount;
String fname,cname,forstring,wstring,dojstring,comstring,printdata="";{
fname=JOptionPane.showInputDialog(null,"Enter the name of the construction firm","Input Data",JOptionPane.QUESTION_MESSAGE);
cname=JOptionPane.showInputDialog(null,"Enter the customer's name","Input Data",JOptionPane.QUESTION_MESSAGE);
forstring=JOptionPane.showInputDialog(null,"Enter the number of labor hours required for the foreman","Input",JOptionPane.QUESTION_MESSAGE);
wstring=JOptionPane.showInputDialog(null, "Enter the number of workers required","Input",JOptionPane.QUESTION_MESSAGE);
dojstring=JOptionPane.showInputDialog(null,"Enter the number of days required to complete the job","Input",JOptionPane.QUESTION_MESSAGE);
comstring=JOptionPane.showInputDialog(null,"Enter the cost of materials required for the job"+"Input",JOptionPane.QUESTION_MESSAGE);
forhours=Double.parseDouble(forstring);
workers=Double.parseDouble(wstring);
daysonjob=Double.parseDouble(dojstring);
costofmaterials=Double.parseDouble(comstring);
flcost=60*(forhours);
wlcost=8*(daysonjob)+30*(workers);
jobfee=(flcost)+(wlcost)+(costofmaterials);
discount=(jobfee)*0.10;
finalamount=(jobfee)-(discount);
printdata=printdata+"CONSTRUCTION FIRM NAME="+fname+"\n"+
"CUSTOMER:"+cname+"\n"+
"Number of days ="+daysonjob+"\n"+
"Forman labor cost ="+flcost+"\n"+
"Worker Labor Cost ="+wlcost+"\n"+
"Materials cost ="+costofmaterials+"\n"+
"Job Fee ="+jobfee+"\n"+
"Discount ="+discount+"\n"+
"Final Amount Due ="+finalamount+"\n";
JOptionPane.showMessageDialog(null,printdata,"Output:",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}}
The main method is the only one called automatically, and it contains no statements that would cause it not to terminate immediately.
See this altered version, and carefully read the code comments:
import javax.swing.JOptionPane;
public class ContractorBill {
double forhours, workers, daysonjob, costofmaterials, flcost, wlcost, jobfee, discount, finalamount;
String fname, cname, forstring, wstring, dojstring, comstring, printdata = "";
public static void main(String[] args) {
new ContractorBill(); // call the (newly added) constructor
}
// this has now been turned from a code block into a constructor
ContractorBill() {
fname = JOptionPane.showInputDialog(null, "Enter the name of the construction firm", "Input Data", JOptionPane.QUESTION_MESSAGE);
cname = JOptionPane.showInputDialog(null, "Enter the customer's name", "Input Data", JOptionPane.QUESTION_MESSAGE);
forstring = JOptionPane.showInputDialog(null, "Enter the number of labor hours required for the foreman", "Input", JOptionPane.QUESTION_MESSAGE);
wstring = JOptionPane.showInputDialog(null, "Enter the number of workers required", "Input", JOptionPane.QUESTION_MESSAGE);
dojstring = JOptionPane.showInputDialog(null, "Enter the number of days required to complete the job", "Input", JOptionPane.QUESTION_MESSAGE);
comstring = JOptionPane.showInputDialog(null, "Enter the cost of materials required for the job" + "Input", JOptionPane.QUESTION_MESSAGE);
forhours = Double.parseDouble(forstring);
workers = Double.parseDouble(wstring);
daysonjob = Double.parseDouble(dojstring);
costofmaterials = Double.parseDouble(comstring);
flcost = 60 * (forhours);
wlcost = 8 * (daysonjob) + 30 * (workers);
jobfee = (flcost) + (wlcost) + (costofmaterials);
discount = (jobfee) * 0.10;
finalamount = (jobfee) - (discount);
printdata = printdata + "CONSTRUCTION FIRM NAME=" + fname + "\n"
+ "CUSTOMER:" + cname + "\n"
+ "Number of days =" + daysonjob + "\n"
+ "Forman labor cost =" + flcost + "\n"
+ "Worker Labor Cost =" + wlcost + "\n"
+ "Materials cost =" + costofmaterials + "\n"
+ "Job Fee =" + jobfee + "\n"
+ "Discount =" + discount + "\n"
+ "Final Amount Due =" + finalamount + "\n";
JOptionPane.showMessageDialog(null, printdata, "Output:", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
Here's what I'm trying to do:
String output = "If you borrow" + currencyFormatter.format(loanAmount);
+" at an interest rate of" + rate + %;
+"\nfor" + years;
+",you will pay" + totalInterest + "in interest.";
Take out the semicolons before the end of your concatenation.
String output = "If you borrow" + currencyFormatter.format(loanAmount)
+" at an interest rate of" + rate + "%"
+"\nfor" + years
+",you will pay" + totalInterest + "in interest.";
I also recommend that you move the concatenation operator to the end of the line rather than the start of the line. It's a minor stylistic preference...
String output = "If you borrow" + currencyFormatter.format(loanAmount) +
" at an interest rate of" + rate + "%" +
"\nfor" + years +
",you will pay" + totalInterest + "in interest.";
Finally, you may notice that you are missing some white-spaces when you try printing that string. The String.format method helps with that (also see the documentation for Formatter). It's also faster than doing lots of concatenations.
String output = String.format(
"If you borrow %s at an interest rate of %d%%\nfor %d years, you will pay %d in interest.", currencyFormatter.format(loanAmount), rate, years, totalInterest
);
How can I input a decimal or double number as it is? I want if I input .56 it saves in the database as .56 not 1 because its rounding up and I want to ignore the rounding...
This is servlet, well I have beans and its also set to double; I also tried DecimalFormat but still not working or maybe I just don't know how to use it.
neutrophils = rs.getInt("neutrophils");
monocytes = rs.getInt("monocytes");
eosinophils = rs.getInt("eosinophils");
basophils = rs.getInt("basophils");
lymphocytes = rs.getInt("lymphocytes");
total= (neutrophils + monocytes + eosinophils + eosinophils + basophils + lymphocytes);
I made it like this, I changed the value of datatype to VARCHAR but the error is java.lang.NullPointerException; why is that?
neutrophils = Double.parseDouble(rs.getString("neutrophils"));
monocytes = Double.parseDouble(rs.getString("monocytes"));
eosinophils = Double.parseDouble(rs.getString("eosinophils"));
basophils = Double.parseDouble(rs.getString("basophils"));
lymphocytes = Double.parseDouble(rs.getString("lymphocytes"));
bands = (neutrophils + monocytes + eosinophils + eosinophils + basophils + lymphocytes);
If rs is ResultSet you can simply use rs.getDouble. If for some reason you don't want to use it, get the result as a String and then convert
Double.parseDouble(rs.getString(ColumnLabel));
I'm taking a Java programming class at school and we've been working on a project in class - dealing with console applications. For the coming week, we're moving on to working on creating GUI applications with JAVA and I had an idea of what to do with one of my projects.
I wanted to redirect the console output to a text area inside the GUI. But I don't know if this is possible, or how to do it. Is it possible, if so, can somebody help me. I'm trying to design my form so that it looks like a cash register (with the receipt on one side of the register). In this case, the receipt will be the redirected console output.
Any help would be greatly appreciated.
Here's my source code:
package autoshop_invoice;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.NumberFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
public class AutoShop_Invoice
{
public static void total_sales() throws IOException
{
String text = "";
String part_number = "";
int num_items = 0;
double price = 0.0;
double tax = 0.0;
double total_sale = 0.0;
//Prompt user for part number and store value in variable part_number.
System.out.println("Enter Part Number then hit enter.");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try
{
part_number = in.readLine();
} catch (IOException ex)
{
Logger.getLogger(AutoShop_Invoice.class.getName()).log(Level.SEVERE, null, ex);
}
//Prompt user to enter number of items sold and store value in variable num_items.
System.out.println("Enter Number of Items sold then hit enter.");
in = new BufferedReader(new InputStreamReader(System.in));
text = in.readLine();
num_items = Integer.parseInt(text);
//Prompt user to enter Price per Item and store value in variable num_items.
System.out.println("Enter Price per Item then hit enter.");
in = new BufferedReader(new InputStreamReader(System.in));
text = in.readLine();
price = Double.parseDouble(text);
//Display the total sale WITH tax calculated.
total_sale = num_items * price;
tax = total_sale * .06;
total_sale = total_sale + tax;
//DecimalFormat df = new DecimalFormat("#.##");
NumberFormat nf = NumberFormat.getCurrencyInstance(); //Get the current system locale currency
System.out.println();
System.out.println("***********************************");
System.out.print("Part Number: " + part_number + " "); //Display the Part Number being sold
System.out.println("QTY: " + num_items); //Display the quantity of items sold
System.out.println("Sub-Total is: " + nf.format(total_sale)); //Display sub-total rounded to 2 decimal points
System.out.println("MD Sales Tax due (6%): " + nf.format(tax)); //Display the calculated sales tax
//Display grand-total rounded to 2 decimal points and formatted with the locale currency
//System.out.println("Grand-Total sales is: " + df.format(nf.format(total_sale + tax)));
System.out.println("Grand-Total sales is: " + nf.format(total_sale + tax));
System.out.println("***********************************");
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws IOException
{
// TODO code application logic here
total_sales();
}
}
Write your own OutputStream implementation that adds any bytes written to the text area. Then wrap it in a PrintStream and pass it to System.setOut():
System.setOut(new PrintStream(myTextAreaOutputStream));
You can use Java Swing to easily create a form and implement its functionality.
Take a look at this tutorial: http://docs.oracle.com/javase/tutorial/uiswing/start/
A walk through on a basic project starts here using Netbeans http://docs.oracle.com/javase/tutorial/uiswing/learn/settingup.html
It's pretty straight forward. Lay out the controls, and then implement the functionality. I think their Celsius converter example will get you very close to finishing your project with just a few modifications.
I seem to be having some trouble getting my code to run properly here. What this is supposed to do is it is supposed to read from a text file and find the name, quantity, and price of an item on each line then format the results. The tricky bit here is that the items have names that consist of two words, so these strings have to be differentiated from the quantity integer and price double. While I was able to get this working, the problem that I am having is with a singe space that is at the very end of the text file, right after the last item's price. This is giving me a java.util.NoSuchElement Exception: null, and I cannot seem to move past it. Can someone help me to work out a solution? The error is on thename = thename + " " + in.next();
while (in.hasNextLine())
{
String thename = "";
while (!in.hasNextInt())
{
thename = thename + " " + in.next();
thename = thename.trim();
}
name = thename;
quantity = in.nextInt();
price = in.nextDouble();
}
You need to make sure the Name quantity price string is properly formatted. There might not be enough tokens in the string. To check that there are enough tokens for the name:
while (!in.hasNextInt())
{
thename = thename + " ";
if (!in.hasNext())
throw new SomeKindOfError();
thename += in.next();
thename = thename.trim();
}
You don't have to throw an error, but you should have some kind of code to handle this issue properly according to your needs.
The problem is in the logic of your inner while loop:
while (!in.hasNextInt()) {
thename = thename + " " + in.next();
}
In English, this says "while there's not an int available, read the next token". The test does nothing to help check if the next action will succeed.
You aren't checking if there is a next token there to read.
Consider changing your test to one that makes the action safe:
while (in.hasNext()) {
thename = thename + " " + in.next();
thename = thename.trim();
name = thename;
quantity = in.nextInt();
price = in.nextDouble();
}