Add A Zero For Decimal Part

Apr 19, 2015

Add the following method to the BankAccount class: public String toString()Your method should return a string that contains the account's name and balance separated by a comma and space. For example, if an account object named benben has the name "Benson" and a balance of 17.25, the call of benben.toString() should return:

Benson, $17.25

There are some special cases you should handle. If the balance is negative, put the - sign before the dollar sign. Also, always display the cents as a two-digit number. For example, if the same object had a balance of -17.5, your method should return:

Benson, -$17.50

Here is my code:

public String toString() {
String result = name + ", ";
if (balance < 0) {
result += "-";
}
return result += "$" + Math.abs(balance);
}

My code only works in case there are full two numbers for the cents part, not for the case when there's only one number. So I wonder how I can add an extra zero when needed.I can get only the decimal part and add a zero if it's less than 10, but I don't know how I can extract just the decimal part from the number. (The balance is just a double and it doesn't have any separate field for dollars and cents).

View Replies


ADVERTISEMENT

Not Getting Decimal For Surely Decimal Answer?

Jan 1, 2015

I'm using eclipse. I'm going to get straight to the point and give all the info I can, if the values in the first code box are used, shouldn't these values be left after all in the second box is done:

remainder=23, arr[0]=100, div=23/10=2.3, whole=2, and decimal=3?

When I use this code, div comes out to be just (2.0).

Java Code:

int leng=10;
arr[0]=123; //int
arr[1]=100; //int mh_sh_highlight_all('java'); Java Code: if (arr[0]!=arr[1]){
int remainder=arr[0]-arr[1];
arr[0]=arr[0]-remainder;
double div=remainder/leng; //double div=Double.valueOf(remainder/leng);
int whole=(int) Math.floor(div);
int decimal=(int) ((div-whole)*leng); mh_sh_highlight_all('java');

I'm not sure were I'm going wrong in how div is being calculated, but I ultimately need div to be 2.3.

I've also used the second option commented out which still gives (2.0).

View Replies View Related

Round Decimal Number Into 2 Decimal

Apr 2, 2014

Here is my code and i want to convert number into 2 decimal but this code not give me result how to check my code.

import javax.swing.JOptionPane;
public class showtime{
public static void main(String[] args){
double total_mili=System.currentTimeMillis();
JOptionPane.showMessageDialog(null,total_mili, "Total milisecond",JOptionPane.INFORMATION_MESSAGE);
double seconds=total_mili/60;
double sec=(double)(seconds * 100) / 100.000;
JOptionPane.showMessageDialog(null,seconds, "Total Second",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,sec, "Total Second",JOptionPane.ERROR_MESSAGE);
}
}

View Replies View Related

Extracting Part Of A String?

Oct 13, 2014

Okay So I am creating an application but I'm not sure how to get certain parts of the string. I have read In a file as such:

*tp*|21394398437984|163600
*2*|AAA|1234567894561236|STOP|20140527|Success||Automated|DSPRN1234567
*2*|AAA|1234567894561237|STOP|20140527|Success||Automated|DPSRN1234568
*3*|2

I need to read the lines beggining with *2* so I done:

s = new Scanner(new BufferedReader(new FileReader("example.dat")));
while (s.hasNext()) {
String str1 = s.nextLine ();
if(str1.startsWith("*2*")){
System.out.print(str1);
}
}

So this will read the whole line I'm fine with that, Now my issue is I need to extract the 2nd line beggining with numbers the 4th with numbers the 5th with success and the 7th(DPSRN). I was thinking about using a String dilemeter with | how to extract them once i've used the dilemeter.

View Replies View Related

Inventory Part 2 Portion And Getting Errors

Apr 30, 2014

for my assignment I am to only add the sort and total inventory methods in the inventory class. And not create an array in the inventory class at all. My inventory class should contain all the variables needed to use with the two new methods: sort and calculate total inventory.By not creating any array in the inventory class and not touching the variables at all.For the inventoryTest class, I only need to declare an array of the inventory class by making an instance of the inventory.

public class inventory
{

private int prodNumber; // product number
private String prodName; // product name
private int unitsTotal; // total units in stock
private double unitPrice; // price per unit
private double totalInventory; // amount of total inventory

[code]...

View Replies View Related

Swing/AWT/SWT :: Extend And Retract Part Of GUI

Jul 18, 2014

I have a GUI that I've been working on for a while now. What I am trying to figure out, is a way to have the user push a button, and when they do have the GUI extend out to the right with another table to use for Filter Data. Once They are done, I want them to push that button again, and the panel with the query data retracts and is hidden again, all without changing the size of the current GUI in question. I've tried a bunch of different things with setting preferred and Minimal values to my GUI, and I've played around with different Layouts (Border Layout.East, etc), but I can't seem to find a good working solution.

Ideally it would be slick if I could make my panel SLIDE out and SLIDE back in, which would look really cool, but I'd settle for something that just worked.

View Replies View Related

JSF :: Printing Only Part Of Prime-faces Page?

Jan 15, 2014

I have a print button as below

<p:commandButton id="printit" value="Print" type="button" icon="icn-print">
<p:printer target=":topForm:data" />
</p:commandButton>

when i click print button only top half of the page is getting printed and bottom half is pushed to next page. How do i fix this issue.

View Replies View Related

How To Remove Object From Jframe As Part Of ActionPerformed

Mar 11, 2014

I currently have a program written that outputs a canvas object and adds a picture of 5 taxis to it.I have now added a jtextfield so that a user can add an interger. Ideally if the user was to enter the number 8, there would be 8 taxis added to the canvas.I am having trouble with the final part i mentioned. how to delete the old canvas and output a new one with the amount that the user wrote in the jtextfield.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;

[code]....

View Replies View Related

How To Extract Specific Part Of A String In Java

Mar 1, 2014

consider this statement from a jsp file(there are many more statements like this in jsp file..) Statement -

<h:dataGrid something styleclass="styleclass1" something1
onClick="event" something2
<% this is a scriplet tag %>
something3
style="style1">

<h:output text>hello i am text</h:output text>
</h:dataGrid>

What I want is to extract(and store it somewhere) the part from "<" to ">" where:

< - is the one in "<h:dataGrid"
> - is the one in "style1>" and not the('>') one that appears in the end
of "</h:dataGrid>" or "<h:output text>" or "</h:output text>"

Problem is the text b/w && is in multi-line...&& there are scriplet tags in between them.. so i don't know how to extract this particular string.. i tried using using some regular expressions but couldn't find the exact one..

(this was just an example && instead of this "" tag it can be anything like again in this line :

<h:output text>hello i am text</h:output text>

I want to extract the string from "<" till ">" where :

< - is the one in starting of "<h:output text>"
> - is the one in ending of "<h:output text>" and not the one in "</h:output text>"

However the difference b/w this example and the above mentioned one is that this one is not multi-line and doesn't contains any scriptlet tags)....

View Replies View Related

JSF :: Loading Part Of Page Using AJAX - How To Handle CommandButtons

Jun 25, 2014

I have a page which lists items using a ui:repeat. The repeat is surrounded by my h:form tag.

Now I have made it so that when click an item, then I load some item details - render them in their own xhtml file and inject the result into the dom tree.

This is causing some problems.

* My injected content has commandButtons and commandLinks which do not work because I don't have a form in my injected page - since this would cause nested forms :-(
* tried to replace commandButtons and commandLinks and instead create unique url's that I can call to get my work done - but how to I then re-render a panelGroup on then page? tried using jsf.ajax.request but I'm not able to get part of the page (a shoppingcart) to update

Basic outline

<h:form>
<table>
<ui:repeat ...>
<tr><td onclick="...">Click here to load item details</td></tr><tr><td id="itemDetailPlaceholder"></td></tr>
</ui:repeat>
</table>
</h:form>

View Replies View Related

Java API - Truncate Float Part Of Double Variable

Jan 13, 2015

Is there a method in Java API that truncate the float part of a double variable after a given number of digits?

Example: if the number of digits to be truncated is 2, so the method truncate (6.9872) to (6.98) ,

I tried to write a method that do that but it was very difficult.

View Replies View Related

Part Of Panel Missing - No Code Error (tried Changing Size)

Apr 20, 2014

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.*;

public class aaa extends JFrame
{
private JPanel combinationp, np, ap, dp, surgeryp, medicationp, infop; // input = name, address, days
private JLabel namelabel, addresslabel, dayslabel, infolabel;
private JTextField nametf, addresstf, daystf, selectedsurgerytf;
private JButton calcButton;

[Code] .....

View Replies View Related

Decimal To Hex - Not Getting Value 0 When Input Is 0

Mar 4, 2014

Why do i not get a value 0 when my input is 0.

import java.util.Scanner;
public class Dec2Hex {
public static void main(String[] args){
int dec;
String hexStr = "";
int radix = 16;

[Code] .....

View Replies View Related

Binary To Decimal Using Array

Jan 28, 2015

I do not know how to convert a reversed array to decimal.

The output should be:

How many digits to convert?
(user will input) ex. 3
Please input digits:
(user will input 3 digits)
ex. 0
1
1
The binary digits are: 110 (<-reversed)
The decimal value is: 6

Here is my code:

package numbersystemconversion;
import java.util.Scanner;
public class BinarytoDecimal {
static {
int digit=0;
Scanner input = new Scanner(System.in);

[Code] ....

View Replies View Related

Numbers After Decimal Won't Add Together For Total

Sep 26, 2014

The code is below and the problem I am having is the numbers after the decimal won't add together for the total I am teaching myself java through liang's 10th edition book and I took one of the sample programs further than what was shown. The one in the book only shows the tax amount I simply wanted to make it so that the purchase amount and the tax amount would add together

package javalearning;
import java.util.Scanner;
public class FindingTax {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter Purchase amount: ");

[code]....

View Replies View Related

Display Decimal Numbers Only

Jul 6, 2014

What code to use to display decimal numbers only. for example 125.50. the program will only display .50

View Replies View Related

Converting Octal To Decimal

Sep 26, 2014

I'm trying to complete a code to convert decimal to octal. however i can't figure out how to print the output correctly. it should be.. Your integer number 160000 is 0470400 in octal. (160000 is the number you input). My code is...

Scanner input = new Scanner(System.in);

int value;
int a;
int b;
int c;
int d;
int e;
int f;
int g;
String result;

[Code] .....

Lastly, my output prints everything correctly, but the input number always is 0.

Ex error: input= 160000

Your integer number 0 is 0470400 in octal.

it should be.. Your integer number 160000 is 0470400 in octal.

View Replies View Related

Program Cannot See DECIMAL FORMAT

May 12, 2015

I am using NetBeans IDE 8.0.2 to program Java code. I am a beginner in Java.

Instead of getting two decimal places after the point, I am getting three despite using the code format for two ("##.##"). Actually, this happens even if I remove the format code between the quotes. It is as if the program cannot see the format code. Why this happens ?

Here is the relevant program code:

private void convertButtonActionPerformed(java.awt.event.Action Event evt) {
double inputNumber = 0;
// sets the decimal format

[Code]....

View Replies View Related

Decimal Point On Jtextfield

Apr 26, 2014

designing a program which allows you to buy stuff from the jframe with 10x items each with different pricing, so once you click the image button the cost of that is displayed in the textfield, ive been trying to set the jtextfield to a decimal point but have not been able to so far,

if (source == jBSofa) {
System.out.println("Sofa");
{
dTotal = dTotal + 599.99;

jTotal.setText(Double.toString(dTotal));
DecimalFormat myFormatter = new DecimalFormat("#####.#");
String output = myFormatter.format(dTotal);
a visual aspect of it.

View Replies View Related

Convert Binary To Decimal Such As Bin

Sep 20, 2014

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();

[Code] ....

View Replies View Related

Print Out GPA In Decimal Format

Oct 25, 2014

I need this program to print out the gpa down to 2 decimal places and I can't figure out how to do it. It keeps saying it can't find decimal format and I'm not sure how to define it.

Java Code:

import java.util.Scanner;
public class GPACalculator {
public static void main(String[] args) {
double creditHours = 0;
double gradePoints = 0;

[Code] ....

View Replies View Related

Decimal To Binary - Not Getting Any Input

Dec 13, 2014

import java.io.*;
class Binary_Displaying
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int d,r,bi=0,i=0,dup;

[Code] ....

I am not getting any output.........

View Replies View Related

How To Add Decimal Format Up To Two Places

Jul 25, 2014

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;

[Code] ....

View Replies View Related

Octal To Decimal Program

Oct 19, 2014

I'm trying to convert an octal number into a decimal number. I keep getting a big error that says java:63: error: class, interface, or enum expected. i need to use a public static int convert ( int octalNumber ) along with a public static void main ( String args[] ).

Here is my code. />

[public static void main ( String args[] )
{
int foo;
int valid = 0;
Scanner sc = new Scanner (System.in);
System.out.print ("Enter up to an 8-digit octal number and I'll convert it: "); foo = sc.nextInt();

[code].....

View Replies View Related

Decimal Values - JOptionPane

Jul 1, 2014

I am a beginner in Java and I am currently learning about JOption pane.Using Strings I am accepting an input from the user and by using the Interer.ParseInt(variable) option I am able to multiply this two strings using the code below.

String Length;
Length = JOptionPane.showInputDialog("Enter the Length");
String Breadth;
Breadth = JOptionPane.showInputDialog("Enter the Breadth");
System.out.println(" Area is " + (Integer.parseInt(Breadth) * Integer.parseInt(Length)));
System.exit(0);

How Do I make my code accept Decimal values. E.g. My Code should accept 10.02 as Length and 20.42 as Breadth and give the product a Decimal.

View Replies View Related

Program To Print In Decimal Format

Oct 24, 2014

so I need my program to print in decimal format and I keep getting an error saying that it cant find symbol "decimalFormat". here's what I have so far.

import java.util.Scanner;
public class GPACalculator {
public static void main(String[] args) {
int creditHours = 0;
int gradePoints = 0;

[Code] .....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved