Decimal Point - Remove Leading Zeros
Jan 23, 2014
If I have 0123.45 string and trying to get 12345 as result. I am trying regex like below.
String s="0123.45";
s = s.replaceAll("^0*([0-9]+).*", "$1");
It gave result 123 as leading zeros removed that is one of the things that I want. How do I achieve 12345?
View Replies
ADVERTISEMENT
Nov 18, 2014
why I need to populate an array with leading zeros. It sounds like it wants me to populate the entire array for one number (string). When I use next(), it separates the numbers so why do I need to go the leading zeros route?
This assignment will give you practice with external input files and arrays. You are going to write a program that adds together large integers. The built-in type int has a maximum value of 2,147,483,647. Anything larger will cause what is known as overflow. Java also has a type called long that has a larger range, but even values of type long can be at most 9,223,372,036,854,775,807.The approach you are to implement is to store each integer in an array of digits, with one digit per array element. We will be using arrays of length 50, so we will be able to store integers up to 50 digits long. We have to be careful in how we store these digits. Consider, for example, storing the numbers 38423 and 27. If we store these at the front of the array with the leading digit of each number in index 0 of the array, then when we go to add these numbers together, we're likely to add them like this:
38423
27
To simulate this right-shifting of values, we will store each value as a sequence of exactly 50 digits, but we'll allow the number to have leading 0's. For example, the problem above is converted into:
0000000000000000000038423
0000000000000000000000027
Now the columns line up properly and we have plenty of space at the front in case we have even longer numbers to add to these.The data for your program will be stored in a file called sum.txt. Each line of the input file will have a different addition problem for you to solve. Each line will have one or more integers to be added together. Take a look at the input file at the end of this write-up and the output you are supposed to produce. Notice that you produce a line of output for each input line showing the addition problem you are solving and its answer. Your output should also indicate at the end how many lines of input were processed. You must exactly reproduce this output.
You should use the techniques described in chapter 6 to open a file, to read it line by line, and to process the contents of each line. In reading these numbers, you wont be able to read them as ints or longs because many of them are too large to be stored in an int or long. So youll have to read them as String values using calls on the method next(). Your first task, then, will be to convert a String of digits into an array of 50 digits. As described above, youll want to shift the number to the right and include leading 0s in front.
The String method charAt and the method Character.getNumericValue will be useful for solving this part of the problem.You are to add up each line of numbers, which means that youll have to write some code that allows you to add together two of these numbers or to add one of them to another. This is something you learned in Elementary School to add starting from the right, keeping track of whether there is a digit to carry from one column to the next. Your challenge here is to take a process that you are familiar with and to write code that performs the corresponding task.
Your program also must write out these numbers. In doing so, it should not print any leading 0s. Even though it is convenient to store the number internally with leading 0s, a person reading your output would rather see these numbers without any leading 0s.You can assume that the input file has numbers that have 50 or fewer digits and that the answer is always 50 digits or fewer. Notice, however, that you have to deal with the possibility that an individual number might be 0 or the answer might be 0. There will be no negative integers in the input file.You should solve this problem using arrays that are exactly 50 digits long. Certain bugs can be solved by stretching the array to something like 51 digits, but it shouldnt be necessary to do that and you would lose style points if your arrays require more than 50 digits.The choice of 50 for the number of digits is arbitrary (a magic number), so you should introduce a class constant that you use throughout that would make it easy to modify your code to operate with a different number of digits.
Consider the input file as an example of the kind of problems your program must solve. We might use a more complex input file for actual grading. The Java class libraries include classes called BigInteger and BigDecimal that use a strategy similar to what we are asking you to implement in this program. You are not allowed to solve this problem using BigInteger or BigDecimal. You must solve it using arrays of digits.Your program should be stored in a file called Sum.java.
Input file sum.txt
82384
204 435
22 31 12
999 483
28350 28345 39823 95689 234856 3482 55328 934803
7849323789 22398496 8940 32489 859320
729348690234239 542890432323 534322343298
3948692348692348693486235 5834938349234856234863423
999999999999999999999999 432432 58903 34
82934 49802390432 8554389 4789432789 0 48372934287
0
0 0 0
7482343 0 4879023 0 8943242
3333333333 4723 3333333333 6642 3333333333
Output that should be produced
82384 = 82384
204 + 435 = 639
22 + 31 + 12 = 65
999 + 483 = 1482
28350 + 28345 + 39823 + 95689 + 234856 + 3482 + 55328 + 934803 = 1420676
7849323789 + 22398496 + 8940 + 32489 + 859320 = 7872623034
729348690234239 + 542890432323 + 534322343298 = 730425903009860
[code]....
View Replies
View Related
Nov 15, 2014
I am writing a program that adds together large integers. I have to store each integer in an array of digits, with one digit per array element. Array length is 50 so integer is 50 digits long. I have to store numbers in right-shifting format with leading zeros. For example,
0000000000000000000038423
0000000000000000000000027
Sum.txt contains numbers to be added. There could be one or more numbers per line.each line must be read as string with next() since it's assumed to be a very long number. String of digits needs to be converted into an array of 50 digits. Method CharAt and Character.getNumericValue will be useful. All numbers in each line are to be added. There are no negative numbers and individual number might be 0 or answer might be 0. Answer is always 50 digits or fewer.
BigDecimal or BigInteger are not allowed.
I'm lost where it says to put number with leading zeros in a 50 room array. How do I add numbers after formatting numbers with leading zeros?
View Replies
View Related
Feb 4, 2015
I have an array made that represents digits and I am trying to make a method so that if there are zeros in front of the first significant digit I want to trim them, I understand you can't re size arrays so I have created a new array, but my code doesn't seem to run correctly? Here is my code I can't figure out what is wrong I've tried everything: (I put stars around the error**)
package music;
import java.util.Random;
public class Music {
private int length; // length of the array
private int numOfDigits; // number of actual digits in the array
int[] musicArray;
[Code] .......
View Replies
View Related
Jun 10, 2014
I got situation where i have postal code as 0009 in database and the use is entering 0009 but somehow in my java code it only reading 9 from the xml file
This is how i define getter and setter
When i debug the code i get
passed postalcode 9
Java Code:
public String Postalcode="";
public void setPostalcode(String Postalcode) {
this.Postalcode = Postalcode;
}
public String getPostalcode() {
return Postalcode;
} mh_sh_highlight_all('java');
What if 0009 is passed i what it to remain 0009
View Replies
View Related
Jan 21, 2014
Any way to place leading zeros onto numbers without having to convert them to strings? Is such a thing possible?
View Replies
View Related
Jun 10, 2014
I got situation where i have postal code as 0009 in database and the use is entering 0009 but somehow in my java code it only reading 9 from the xml file
This is how i define getter and setter :
When I debug the code i get this :
passed postalcode 9
if a user entered 0009 I what it to remain 0009
Java Code:
public String Postalcode="";
public void setPostalcode(String Postalcode) { this.Postalcode = Postalcode; }
public String getPostalcode() { return Postalcode; }
View Replies
View Related
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
Apr 18, 2015
Right now it displays a number like this "$25.0" and I want it to display it like this "$25.00" and don't know how to do it.
View Replies
View Related
Jul 21, 2014
Given a Numbers instance, whose fields are arrays of all the built-in Java numeric types (int, long, float, double, big-decimal, etc), write a method to sort all the numbers into a master list, and then print out the numbers where the number of digits past the decimal point is equal to the index of the number in the master list.
Is there a function in Java that will give me just the numbers after the decimal? I tried Decimalformat but couldn't get it to work. Here is what I have so far; however, I think I might be on the wrong track.
public class Numbers
{
public static void main(String[] args) {
Byte bNum = new Byte((byte) -50);
Integer iNum = new Integer(168);
Long lNum = new Long(100000L);
Short sNum = new Short((short) 10000);
Float fNum = new Float(12.19f);
Double dNum = new Double(23.123);
BigDecimal bd = new BigDecimal("3.14159265358979323846");
[code]....
View Replies
View Related
Jan 27, 2015
Consider this small program and output.
public class Floating{
public static void main( String[] args ){
System.out.println( 2.80 - 1.75 );
}
}
The output is 1.0499999999999998. Why not 1.05?
View Replies
View Related
Jun 5, 2014
I was reading the oracle java tutorial under: URL....Here's the code for the Point class:
public class Point {
public int x = 0;
public int y = 0;
//constructor
public Point(int a, int b) {
x = a;
y = b;
}
}
and in the Rectangle class you have the following constructor:
public Rectangle(Point p, int w, int h) {
origin = p;
width = w;
height = h;
If we create a new Point object like this:
Point originOne = new Point(23, 94);
and then a new Rectangle object like this:
Rectangle rectOne = new Rectangle(originOne, 100, 200);
Will that set originOne to point to the object Point at (23, 94). just want to make that this is the meaning of this statement: Point(Point p)Constructs and initializes a point with the same location as the specified Point object.
View Replies
View Related
Mar 4, 2014
How do i flush off the leading 0s from my results? I want to start with 1.
import java.util.Scanner;
public class Hex2Bin {
public static void main(String[] args) {
String hexStr;
int hexStrLen;
[code]....
View Replies
View Related
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
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
Jun 20, 2014
why this code only displays four zeros ?????
public class VargjetUshtrimi2 {
public static void main (String a []) {
int r[] = new int[11];
for (int i = 1 ;i < 10; i++)
{System.out.println( r[i] );}
}
View Replies
View Related
Nov 16, 2014
Ok, so let's say I am having a user input scores and at the end I want to print out the results so I do something like:
int[] Scores = new int[1000];
//code to ask user for input and store into array //
The user only inputs lets say 5 scores out of possible 1000. I then try to print it out by doing something like this:
for(int counter = 0; counter < Scores.length; ++counter) {
System.out.println(Scores[counter])
}
How would I go about printing out only the index's that were input, because right now it prints out the scores and then 995 0's after.
View Replies
View Related
Apr 3, 2015
I am writing a code that requires a user to input a number, then output the individual digits and then add the sum of the digits. I have the entire program written, but I cannot figure out how to make zeros output as individual digits. If I input 400, it only shows 4 and not 4 0 0. Here is the code:
import java.util.*;
public class week4program {
public static void main(String[] args) {
[Code].....
View Replies
View Related
Mar 31, 2014
How do i change the outcome to below? at the moment it is all 0? How I can clear some clouds in this area.
Competitor 1: 11111
===============================================
Competitor 2: 22222
===============================================
Competitor 3: 33333
===============================================
Competitor 4: 44444
===============================================
Competitor 5: 55555
===============================================
Here's the code:
case 2: System.out.println("Menu item 2");
for(int i=1; i<MAX_COMPS;i++){
System.out.print("Competitor " + i + ": ");
for (int j=1;j<MAX_SCORES;j++)
System.out.print(scores[i][j] + " ");
System.out.println("");
}
View Replies
View Related
Dec 29, 2014
ex
Sample input:2340980
Sample Output:23498
this is the program i have tried but is dosent work
import java.util.*;
class zero
{
public static void main(String args[])
{
[Code]....
View Replies
View Related
Jun 8, 2014
I made a blackjack code in java and I need to find a way to replace the place where I added a system.exit with a way to ask the user if they'd like to play again and restart the loop, keep in mind that I don't need the program to restart as I'd like to keep the value of their chips considering if they've won or lost.
Secondly, because it is a blackjack code, when it deals the cards, I would like for it to also print out K, Q or J but still consider it the number 11. Can I make an Ace count as 1 and 11?
One last question, is there anyway to add the suits of the cards such as (clubs, spades etc.) but the actual signs and if the signs aren't possible at all then the letter ('C', 'S', 'H', 'J') will have to do I guess.
import java.util.Scanner;
class Blackjack {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
Scanner num = new Scanner (System.in);
System.out.println("Welcome to Blackjack!");
[code]....
View Replies
View Related
Jan 31, 2014
how the data is stored in float. It seems like the range would be greater because it stores scientific notation rather than plain value, whilst integer arithmetic performance is better. float should be used to store bigger values and integer should be used for speed when values are smaller. As an example, I want to have cubic volumes ranging from about a handful to cargo ship. So float would be necessary for that.
View Replies
View Related
May 8, 2014
What are the x- and y-coordinates of the Points referred to as p1, p2, and p3 after the following code executes? Give your answer as an x-y pair such as (0, 0). Recall that Points and other objects use reference semantics.
PHP Code:
Point p1 = new Point();
p1.x = 17;
p1.y = 9;
Point p2 = new Point();
p2.x = 4;
p2.y = -1;
Point p3 = p2;
p1.translate(3, 1);
p2.x = 50;
p3.translate(-4, 5); mh_sh_highlight_all('php');
P1:
P2:
P3:
My guesses were: P1:(20,10) //Correct
P2:(50,-1) //Incorrect
P3:(0,4) //Incorrect
View Replies
View Related
Jan 7, 2015
the output of this:
int x = (int) 24.6;
View Replies
View Related
Sep 29, 2014
I need to fill up 1/10th of the board with mines but i am only able to successfully put 1! also my goal is not showing up on the game even though i put it as an up arrow.
package Homework4;
import java.util.Random;
import java.util.Scanner;
public class HW4 {
//Creates a new type to be used to create the board
[Code] ......
View Replies
View Related
Aug 13, 2014
I wanna write a program that take an Hexadecimal and convert it to floating-point . what should I do ?
View Replies
View Related