How To Work With Doubles In Arrays
Sep 13, 2014
import java.util.*;
public class OneDimenArray {
public static void main(String[] args) {
double[] decimals = new double[12];
double nums = 0.0;
double a = 1.0;
[Code] ....
These are the erroe codes I'm getting;
OneDimenArray.java:13: error: ']' expected
double[] decimals = double[Scanner.nextDouble()];
^
OneDimenArray.java:13: error: ';' expected
double[] decimals = double[Scanner.nextDouble()];
^
2 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
View Replies
ADVERTISEMENT
Jan 20, 2014
I'm allowing the user to choose certain items to buy that is moved to an array.Now I'm trying to add those thing in the array use a different class. how I can call the array from my driver class to my checkout class that adds them together.
View Replies
View Related
Jan 25, 2014
I had to use scanner to receive input from the user for a formula and everything compiled and worked correctly except when I entered a double into the Scanner, it only accepts ints and gives me this error every time I enter a double:
java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at TestA1Q2.main(TestA1Q2.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
I know that you usually get that error when the Scanner expects something else that is different from what you type and I have looked over my code time and time again and cannot find anything. I created a test program to see if it was just that problem and not something else in the code and I still got the same error but when I tried compiling and running it here browxy.com everything worked and I got the right answer for the formula. Here's the test I used:
import java.util.Scanner;
public class TestA1Q2 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a double");
double velocity = keyboard.nextDouble();
}
}
At this point I'm thinking it might be a problem with my install of DrJava and I've tried re-installing but nothing changed.
View Replies
View Related
Apr 13, 2014
I have a text file that looks like this:
JacobAndrews8159RoseCtMill CityOR97322555212787964.00
RyanPerkins7546PrinceAveLas VegasNV87126555345198913.00
JoshuaGilbert9278MonroeDrCorvallisCA97330555283265695.00
MilesCrain4578ChesterDrCorvallisOR9733155523456781544.00
ButchCassidy5498SuttonPlGresham AZ9738054165657971798.56
PerryWinkle8185ShaverAveLas VegasNV871265553812346195.66
everything is separated by tabs.
My while loop is like this:
while(fileScan.hasNextLine()) {
String currentLine = fileScan.nextLine();
String[]dataSet = currentLine.split(" ");
String zip = dataSet[7];
String phone = dataSet[8];
String donation = dataSet[9];
int ZIP = Integer.parseInt(zip);
Double Donation = Double.parseDouble(donation);
As you can see I made the donation read as a double, my question is how do I get the donation total? I need to write the code to add up and get the total donations but cannot figure it out.
View Replies
View Related
Mar 8, 2014
Just done a quick test to try and figure out the difference between floats and doubles.I made a quick program which outputs the result of 3.3 * 2 as both a float and a double.
I assumed since a float is the larger and more precise of the two data types that there would be more numbers after the decimal point, however this was not the case, it was in fact the double which had more numbers after the decimal point. Result was as follows:
Float: 29.699999
Double: 29.7
BTW for the above code I simply had a few text fields and a button with the following code:
Java Code:
textBox3.setText("" + Float.parseFloat(textBox1.getText()) * Float.parseFloat(textBox2.getText()));
textBox6.setText("" + Double.parseDouble(textBox1.getText()) * Double.parseDouble(textBox2.getText())); mh_sh_highlight_all('java');
Also, as these are limited to a certain amount of numbers im thinking there must be a more precise way for currency, if so what would I use for that? I suppose what im trying to figure out is what data type to use in different scenarios? When to use integer, float, double and long.
View Replies
View Related
Dec 15, 2014
I'm working with doubles I'm trying to figure out how to set this up so it works?
I'm just stuck on how to set and Assign a double FedTaxWitholding that gets the Fed Tax Withholding = Gross Pay * Fed
Tax Withholding Rate.
Do I need a string for Gross pay?
View Replies
View Related
Oct 14, 2014
I am new to Java and I am having trouble figuring out why input.nextDouble(); is not working for the multiple doubles.
import java.util.Scanner;
public class SimpleMenu
{
public static void main(String [] args)
{
int userInput;
int input = 0;
double circleArea = 0;
double triangleArea = 0;
double rectangleArea = 0;
[Code] ....
View Replies
View Related
Jun 28, 2014
I mainly would like to know is a int[4][4] or a int [4*4] is more efficient to use. Which takes more storage? Which requires more to process? that kind of stuff.
View Replies
View Related
Jan 26, 2015
Write a Java method that returns the largest value of an array of doubles passed to the method as an argument.
Back into java wasn't sure how to do it for doubles did one in the main for integers and then added a method changed from int to double and now i'm lost as go why its not working.
package kickstarter9;
public class Kickstarter9 {
public static void main(String[] args){
double myList;
double[] myList = {6.0, 4.1, 2.4, 6.8, 1.9, 9.4, 2.8, 4.6, 9.3};
// find the largest value in the list
[Code]...
View Replies
View Related
Feb 7, 2014
I am having a problem with infix and prefix ... Here is the code
import java.util.StringTokenizer;
import static java.lang.Math.pow;
import java.util.Stack;
public class Calculator {
static int precedence(char op) {
switch(op) {
[Code] ....
it says in line 72
possible loss of precision
required: int
found: double
the pow only takes int values?not doubles?how do i fix it?
View Replies
View Related
Oct 1, 2014
This code will let a user type 10 double's, and return the smallest of those 10 double's. But I don't really understand what happens in de last for-loop.
package hsleiden.webcat.exercise06_09;
import java.util.Scanner;
public class Exercise06_09
{
public static void main (String[] args){
double[] allNumbers = new double [10];
[Code] ....
View Replies
View Related
Aug 15, 2014
I've been noticing some of my programs have been a little buggy recently, and think it's down to confusion over doubles and positioning. Lets say I have a label called banner that I want to scroll across the screen. Now I need to know the label's width in order to position it, but the width depends on the amount of text, so i use this code:
double bannerWidth = banner.getWidth();
Which forces me into using a double if I want to be accurate.
But the problem is that I'm trying to use a condition that compares the label's horizontal position (currentX) to the left edge of the screen (LEFT_EDGE), minus the width of the label (bannerWidth). In other words when the label is off the screen, it should go back to its starting position.
I assume that means that any variables I use to track the label's position (in this case currentX), or constants that I use to check equality (LEFT_EDGE), have to be doubles as well?
My difficulty is that I iterate currentX. But currentX--; won't work because doubles don't iterate as I'd expect.
So casting becomes an option..... but if I cast to an integer I effectively lose width on the label. And that is magnified each loop, resulting in the label's starting position moving further and further to the left.
View Replies
View Related
Sep 29, 2014
I am looking to find a way to display the number of decimal places without using any type of formatting string.
Only to make it xxx.x instead of xxx.xxxxxx.
View Replies
View Related
Jan 25, 2015
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.
How can I go about doing this?
View Replies
View Related
Oct 26, 2014
"Create a project called RainFall and a class nameD RainFall. Write a program that stores the total rainfall for each of 12 months into an array of doubles. The program should display total rainfall for the year, the average monthly rainfall, the month with the most rain and the month with the least rain. When outputting the month with the most and least rain, output the name of the month. This is what I have so far.
Scanner keyboard = new Scanner(System.in);
double averageMonthly;
double mostRain;
double leastRain;
int months;
double monthlyRain = 0;
double totalRain = 0;
[Code] ....
View Replies
View Related
Jun 18, 2014
The problem is as follows: I've been running java script using git bash and at one point it tells me that I need to increase heap size because apparently it takes more than 1gb to run. However although I manually increase it via control panel>programs>java nothing changes. I've been told that I need to ' figure out how to use 64bit java (and a VM that supports 64bit) in order to increase the maximum memory allocation (heap size) to something north of 2g.'
My laptop has 8GB worth of RAM (Windows 7)so it shouldn't technically have problems running anything above 1G. But there is apparently a 1GB wall But increasing the heap size to above 2G results in an error stating that VM doesn't initialize within git bash, I thought that my java is 32 bit so I downloaded a 64 bit java. Now how and if it is even possible to allocate a program to run under 64 bit java and if I should delete the other java version.
View Replies
View Related
Dec 13, 2014
how does recursion works, I don't understand why it prints al numbers going up again?This is the code
void print2(int n){
if (n<0){
out.printf(" %d",-1);
return;
}
out.printf(" %d", n);
print2(n-1)
out.printf(" %d", n);
}
this should be the output if n is 6: 6 5 4 3 2 1 0 -1 0 1 2 3 4 5 6.
View Replies
View Related
Mar 6, 2015
I need to create a chart for work and comes across this sample from mbostock's chord diagram (bl.ocks.org/mbostock/4062006).
I only need to revise the tick label (names, display positions) but I don't know how to do it as I have 0 experience with Java.
My sample data for the var matrix is
[0,100,100,100]
[0,0,99,98]
[0,0,92,84]
[0,99,0,0]
which shows flows between country A, B, C and D.
I would like to revise the tick labels, so that:
1) Instead of showing 0K 5K 10K, I would like to show the country names (A - D), with no number or tick. I figured out this one by just commenting out all the codes relating to ticks.
2) Place the country names in the center of the arch. I don't know how to do this one.
View Replies
View Related
Mar 5, 2014
a buffer reader , how does it work and what is the code for it ?
View Replies
View Related
Oct 9, 2014
I've got a panel which an arbitrary number of text fields may be added to at run time. I've got it set up so that the window's height, and the height of the panel in which the fields appear will never exceed the height of the screen.What I don't have is a way to make the panel scroll to access the fields that are being visually truncated. I'm setting the autoscrolls on the panel to true, but when I run the program, the fields are simply truncated.
pnlDeclensions.setAutoscrolls(true);
View Replies
View Related
Jun 10, 2014
how inheritence and exception work together ?? what are the rules ??
View Replies
View Related
Oct 4, 2014
So I'm working on a project and noticed that my toString() method won't work. This is just an example of the type of code that I have in my real project. THIS IS MY MAIN CLASS
XML Code:
package trialanderror;
import java.util.Scanner;
public class TrialAndError {
public static void main(String[] args) {
Scanner keys = new Scanner(System.in);
String name;
String phonenumber;
[code]....
View Replies
View Related
Nov 17, 2014
I want to create a dropdown menu with contents from a database. You can see my Code in the attachment. It does not work, but why? I have a dropdown-symbol, but no contents.
View Replies
View Related
Apr 8, 2014
I use Chrome rather than IE. Recently I got an error message when I tried to deposit a check online.
It said that I needed up get a newer version of Java than 6.10, so I downloaded 7u51.
When I tried to deposit the check again, Chrome asked if I wanted to run the app, and I said yes, but that took me back to the download link again.
I spent almost an hour with my bank experts, but they were unable to solve the problem.
When we tried it on Internet Explorer, it worked fine.
Later, I discovered that on the Java Control Panel under the Default Browser for Java, it has only IE and Mozilla, with the IE being checked and greyed out.
I'm assuming that's why the problems on Chrome, even though up until a few days ago, I was able to deposit checks using Chrome-Java.
View Replies
View Related
Jan 12, 2015
All I am trying to do is to make a section of code execute if two strings are equal. The two strings are userId and "A001062". When I use the debugger in Eclipse, I can see the value of userId as "A001062" but whatever string comparison I try never evaluates to true. I have tried
userId=="A001602"
userId.equals("A001602")
"A001602.equals(userId)
Assigning A001062 to a string called AAA and comparing userId to AAA
My code is as follows. I have also attached a screen shot from the Eclipse Debugger which makes me think the string comparison should succeed. I never see the debugger execute the print line nor do I see the print line on the JBOSS console.
String userId = StringUtils.trim(nextLine[HR_USER_ID]);
String AAA="A001062";
if (userId.intern().equals(AAA.intern())) {System.out.print("MKP1: " + userId+"-"+managerId);}
if (userId.compareTo("DTS0428")==0) {System.out.print("MKP2: " + userId+"-"+managerId);}
Attached image(s)
View Replies
View Related
Dec 14, 2014
Okay so I have this part done and I cannot get all the JButtons to work. This is my first GUI!
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintStream;
import java.util.Arrays;
import javax.swing.BoxLayout;
[Code] ....
BTW the error is on 310, 311, and 312 and the error is cannot find symbol ....
View Replies
View Related