Convert Java Code To Display Using Printf Statement With Two Decimal Places To Right
Feb 26, 2015
How i would convert this java code to display using the printf statement, with two decimal places to the right...here is the source code so far, but it has a few errors and needs to be reformated for printf
import java.util.Scanner; // scanner class
public class PROB3_CHAL15
{
public static void main(String[] args)
{
double checks =0,
totalfee =0,
fee = 10,
fee1 =.1,
fee2 = .08,
fee3 = .06,
fee4 = .04,
checkFee;
String input;
Scanner keyboard = new Scanner(System.in);
I need to print to 2 decimal places and I'm trying to use the printf method, but for some reason I keep getting an error. I've looked through my code, but unless I'm missing a small detail, it looks okay to me.
I'm attempting to format my doubles to two decimal places within my return statement. I have tried to use DecimalFormat but it gives me an error because my method needs to return a double and that results in a string being returned. I have also tried using the *100.00/100.00 method and that doesn't work when the number already ends in 0.
If I pass -150.00 it gives me -150.0 when I need two decimal places.
I am working with a program I wrote for class. I got it to compile and do what I want, But I was wondering how I can format my methods to to display a decimal with 1-2 decimal places. Would I create a method in my NumberAnalysis class to do it for me? Or would I declare an instance of the DecimalFormat class in my main method?
import java.util.Scanner; //Needed for Scanner Class import java.io.*; //Need for File and IOException import java.text.DecimalFormat; public class Ex8_11 { public static void main(String[] args) throws IOException{ { DecimalFormat decformatter = new DecimalFormat("#0.00");
[Code] .....
This is my output:
Lowest Number: 1.09 Highest Number: 82.76 Total Number: 367.89000000000004 Total Average Number: 30.657500000000002
Think I just solved the answer to my own question, I did it by declaring double variables in my main method and called the methods and instantiated the variables into the methods... Is there a better way to do this?
package lesson4.skowronek; import java.util.Scanner; //Needed for Scanner Class import java.io.*; //Need for File and IOException import java.text.DecimalFormat; public class Ex8_11 { public static void main(String[] args) throws IOException{
import javax.swing.JOptionPane; import java.text.DecimalFormat; public class Sample3 { public static void main(String args[]){ double amount,iRate,monPay,totalPay; int years; String amountStr;
I've been scanning forums for answers to this problem, but most deal with simple programming that you might find in a classroom (i.e. "System.out.printf") which will not work in the GUI I'm attempting to complete. Here's the tale of the tape:
The GUI is a price calculator I'm developing for my company that takes input from drop-down menus and several Jtextfields and calculates the answer based on the values contained within each. It's completely done (and functional), so I'd rather not change too much if at all possible. Because I'm dealing with decimal values then I'm getting 9 decimal places in the output JLabel, though. In order to display the answer, I'm using a series of "totalPrice.setText(..." declarations.
Because there is a fair amount of text and the values in the calculation are constantly changing, is there a way to 'simply' format the output JLabel to display only 2 decimals? Or is there an alternative solution that would work--say with a JTextfield instead--without having to completely re-code the calculator?
I am writing a simple learning program that does basic math. Everything is working as needed except for when it comes to division. I need to know if there is a way to format the users input to two decimal places and that when the program checks the users answer against the division, it gives it a yay or nay. Right now, it wants the answer out to 12 decimal places.
My program is supposed to include The three sides of the triangle formatted to two decimal places The perimeter formatted to one decimal place The area formatted to one decimal place The unformatted area. It does run, but it is not decimal formatted. I have read my book to try and figure out how to do this, but it doesn't make since to me.
I'm using this try statement to check for errors in input in my Tic Tac Toe game. The problem I have having is I don't understand why this error is happening. It's preventing my code from compiling. I've tried inserting an else statement and multiple brackets at various places with no success.
Java Code: public void play(){ Scanner input = new Scanner(System.in); int row, col; char currPlayer = 'X';
The code here I have works fine if I just want to ask the user to enter four digits: //java application that asks user to input binary numbers(1 or 0) and convert them to decimal numbers import java.util.Scanner; //program uses class scanner public class binarynumber{
//main method that executes the java application public static void main(String args[]){ //declares variables
int digit; int base=2; int degree; double decimal; int binary_zero=0; int binary_one=1; //create scanner for object input
[code]....
The thing is, I want the java application to input more than four digits for the user and I want it to loop it manytimes f until the user ask it to stop.
I am having an issue with my program. It is supposed to convert from a binary number to a decimal number such as bin 101 = dec 5. my first and foremost issue is that when I use System.out.println(parseBin("101")); it returns 5 as it should. However when I change 101 to 1013 it returns 13??? Why is this happening and why are my exceptions not catching the issue?
//import java.util.Scanner; public class BinaryFormatException { public static void main(String[] args) throws BinFormatException{ System.out.println(parseBin("1013")); //Scanner input = new Scanner(System.in); //System.out.println("Please enter a binary number using 1's and 0's: "); //String binString = input.nextLine();
I'm working on creating the Binary to Decimal program . hat is wrong with this part of my code. Why does it not take you into the loop.
import java.util.Scanner; public class question5 { public static void main(String[] args) { System.out.println("Enter a Binary number. "); // collect Scanner keyb = new Scanner(System.in); //
I am new to java programming and using bluej for programming and i have tried this question what i have have given in title ... How to do the following question without using strings.
Write a java program to input a binary value and convert it into decimal and Vice Versa. Without using String. Please make it for blue j format.
So I have to write a java program that converts hexadecimals to decimals without using the whole "integer.parseInt(AB1, 16)" method. I tried looking up how to do this but every forum/site I went to used this same method.
I written java code in JSP to display data. I know it is bad practice. Actually my Business-service communicates with DB and returning data in the form of MAP. I don't have beans Because the fields in DB is dynamically changing. So, for now I'm retrieving Data by writing Java code in JSP. Is there any other way to display data without writing java code in JSP?
I'm learning about the printf command, and when I have it, it is not letting me ad an input. Here is my quick little program:
import java.util.Scanner; public class TestingPrintF { public static void main(String[] args) { // Create a Scanner Scanner input = new Scanner(System.in);
[Code] ....
The first section works good, but when i move down to the second part it just automatically finishes without letting me enter a phrase.
I am currently trying to solve a programming problem on this site, and the problem includes working on a 100 digits of decimal places of a certain irrational number. I tried using BigDecimal class but it displays, correctly me if I am wrong, just 20+ decimal digits. Is there other way to do this?
There's a main menu choosing binary,octa,hexa,and decimal if i choose decimal it will display decimal menu and then i will input the numbers of binary and then it automatically display the converting process for octa hexa and decimal. and if i choose octa . It display the octa menu and i will input the numbers in octa and then it automatically display the converting process for binary hexa and decimal.. and so on.. but in binary if i input 2 it will display invalid .. the same as octa hexa and decimal... in my code the only problem is the converting process. our instructor said no one will use a converter for our project. we will code it manually ..so for a moment .. this is my code..
import java.util.*; import java.lang.*; public class mainmenu { public static void main (String[]args) { Scanner in = new Scanner(System.in); char B, O, H, D, Q, L, N , Y; char bin , pk, pick ;
I am trying to figure out how to convert a string of ASCII code into char.I know that you can use (char) to convert it, but the issue is you cannot really just it for Strings.