Basic imperial conversion problem - java

Coding a calculation tool for my android. On of the inputs is distance in Feet and Inches.
I have two inputs (input3 and input4) for feet and inches, respectively. In my calculation I am trying to convert these two inputs to a decimal number to be used in the rest of the equation. Here's the part of my code that does this:
private void doCalculation() {
// Get entered input value
String strValue3 = input3.getText().toString();
String strValue4 = input4.getText().toString();
// Perform a hard-coded calculation
double imperial1 = (Integer.parseInt(strValue3) + (Integer.parseInt(strValue4) / 12));
// Update the UI with the result to test if calc worked
output2.setText("Test: "+ imperial1);
}
My test values are 4 feet, 6 inches. The 4 comes across fine, but the 6 inches defaults to 0 when it is divided by 12. So my result is 4.0 I tried cutting the calculation down to JUST the division operation, and the result was 0.0
What am I doing wrong? (fyi: this is my first time using Java)

Your types are wrong. You're parsing them as ints when they really should be doubles.
Try:
double imperial1 = Double.parseDouble(strValue3) +
(Double.parseDouble(strValue4) / 12.0);

... what Eli said.
I just wanted to ask why your variables are called strValue3 and strValue4. I'm guessing that it's generated, but you should get into the habit of naming things well. I might go with "feet" and "inches" :)

Eli's answer will work fine. I'm just posting an answer to your question (comment) on Eli's answer.
Can you explain what a try-catch block is?
First you have to understand what an exception is. An exception is an event, which occurs during the execution of your program, that disrupts the normal flow of the program's instructions.
There are 3 kinds of exceptions in Java:
Runtime exceptions: An exception is referred to as a runtime exception if its data type is java.lang.RuntimeException or a subclass of it.
Checked exceptions: An exception is referred to as a checked exception if its data type is a child class of java.lang.Exception, but not a child class of RuntimeException.
Errors: An exception is referred to as an error if its data type is a child class of java.lang.Error. An error is associated with problems that arise outside of your application and typically do not attempt to recover from errors.
For a more detailed description on exceptions, read this.
In order to catch and handle these exceptions, you use try-catch blocks. For example, you use Double.parseDouble function. If the parameter in this function is not a valid number, for example if the user supply the string "NotANumber" you try to convert it to double, then a NumberFormatException will be thrown by Double.parseDouble. If you don't handle this error, your program will terminate unexpectedly.
So, you should write something like the following (including the positive numbers feature you want):
double imperial1 = 0.0;
try {
double firstNumber = Double.parseDouble(strValue3);
double secondNumber = Double.parseDouble(strValue4);
if(firstNumber < 0 || secondNumber < 0)
throw new NumberFormatException("numbers must be positive.");
imperial1 = firstNumber + secondNumber / 12.0;
} catch(NumberFormatException ex) {
// Handle the exception maybe by printing a message to the user that his inputs
// weren't valid numbers.
}

Related

Java error : "Exception in thread "main" java.util.InputMismatchException"

I am a newbie with Java and I'm making a very simple Java program like this:
package exercise7;
import java.util.Scanner;
public class Exercise7 {
public static void main(String[] args) {
// TODO code application logic here
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a number between 0.0 and 1.0.");
keyboard.nextDouble();
}
}
My problem is, when I enter 0.1 for example, they said to me that I had this problem:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at exercise7.Exercise7.main(Exercise7.java:30)C:\Users\Anh Bui\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1BUILD FAILED (total time: 7 seconds)
I am very confusing, because I thought that nextDouble could mean 0.1 or something like that? Could you please help me? Thank you very much.
The reason between this is just between using 1.1 or 1,1 in different zones.
The way the Scanner reads the nextDouble is related to your Locale or Zone setting (which, if not overrided, are loaded from system).
For example, I'm in Poland and my delimiter for decimal or floating point numbers is , and regardless the Java standard syntax is to use dot(.), if I input some number with . I'll also get InputMismatchException, but when I use ,, e.g. 0,6 it finishes flawlessly.
In the same time, regardless of the Zone or Locale, the valueOf or parseDouble methods from Double class are using . as floating point separator.
Scanner class methods starting with next... are based on the built-in patterns for discovering the proper values. The discovery pattern is build with the common/regular way of writing numbers/values (of the specified type) in your country/zone.
This is very similar to the Data types, which differs even more on their zone of use.
One of the ways of handling InputMismatchException here is to catch this exception and require the user to provide the number again, similarly to this question.
Your case is a bit different from the input of natural number (because 1.1 and 1,1 should indicate the floating point number.
The simplest way of handling those two separators for decimal numbers would be to read the input as String, replace the wrong separator and parse to double, like:
static double getDouble(String doubleS) {
doubleS = doubleS.replace(',', '.');
try {
return Double.parseDouble(doubleS);
} catch (NumberFormatException e) {
e.printStackTrace();
throw e;
}
}
Thanks to #Andreas comment, the most appropiate way of handling this would to require the user to provide a decimal number with . separator, because the printed message (before input) has the separator in its numbers.
This could be done with setting the type of Scanner object locale before number input like:
Scanner keyboard = new Scanner(System.in);
keyboard.useLocale(Locale.US); //this sets the locale which has below separator
System.out.println("Enter a number between 0.0 and 1.0.");

Having issue with a very simple java program, not displaying proper result

here is my code that isn't working:
Scanner hello = new Scanner (System.in);
double a = 10;
double c;
System.out.print("Enter the value: ");
c = hello.nextDouble();
double f = a + c;
System.out.printf("The sum of 10 plus user entry is : ", a+c);
No syntax error whatsoever, no error displayed, this is the result :
Enter the value: 100
The sum of 10 plus user entry is :
So there is no result in the second line,,, for the command ( a+c ) as in program. But if i use a ' %.2f ' before ( a+c ) command, it works fine,,
like :
System.out.printf("The sum of 10 plus user entry is : %.2f", a+c);
I tried to search about the '%.2f' but got to know it is used just to ascertain that the following number is to be displayed as a number with two decimal places. (kinda round off thing, i guess)..
I'm totally a rookie at Java. Started studying it at college right now. Was just curious to know about this concept and reason behind why this program worked only with the '%.2f' typed in it, and not without it, although it showed no error. Will be great if someone can answer it. thanks :-)
Java's System.out.printf() method doesn't append information; it substitutes it. The '%.2f' means: "Replace this with the next argument, and convert it to a floating-point number 2 places precise." Removing the '%.2f' would mean that a+c would have nowhere to go, and printf() would discard it.
Since Java's System.out.printf() method is actually based on the printf() from C/C++, you might want to check out this guide.
You are using the wrong function.
You should be using
System.out.println(myString)
Or
System.out.print(myString)
You would format your code as
System.out.println(myExplinationString + a+c)
System.out is an instance of java.io.PrintStream class that is provided as a static field of the System class. printf(String format, Object... args) is one of the methods of the PrintStream class, check this Oracle tutorial on formatting numbers. In brief, the first argument is a format string that may contain plain text and format specifiers, e.g. %.2f, that are applied to the next argument(s). All format specifiers are explained in the description of the java.util.Formatter class. Note, that double value is autoboxed to Double.

Changing Doubles To Ints

Im working on a homework assignment for my intro to computer science class and we are are inputting basic commands to get the percentage of people who drink a certain kind of energy drink. We used JOptionPane to make text boxes and you can input the amount of people and the computer has a set percentage to get the output. My problem is i set up my variables as doubles and my answers are very long decimals. I want to convert the answers to Ints so i can get whole numbers. I have tried to do this through casting but i keep getting the error message" EnergyDrink.java:14: error: variable citrusEnergyDrinkers might not have been initialized". What can i do?
This can't be solved without code. The error is not due to any problem with the conversion, but simply as the compiler-error says:
variable citrusEnergyDrinkers might not have been initialized
This means that the variable might not hold a value at the time you attempt to convert it, which results in undefined behaviour, which java-designers didn't allow for a reason.
The problem is as the error-message tells: citrusEnergyDrinkers gets its value inside some try-catch-block or a block that is only run under certain conditions, like if. One way to work around this would be to simply initialize citrusEnergyDrinkers as 0:
double citrusEnergyDrinkers = 0;.
Note though that this might produce incorrect results depending upon what happens when the value isn't set in case the above mentioned block of code isn't entered/breaks off before setting a value.
For the conversion:
Math.round(citrusEnergyDrinkers) is most likely preferable to a simple cast to int, since double most of the time has some imprecision due to the way it's stored in memory and round will actually round the value, while a cast will simply remove the frictional part. For example:
(int) 0.75 //produces 0
Math.round(0.75) //produces 1
You could multiply the double by 100 and then cast to an int:
double d = .77583495;
int perc = (int) Math.round( d );
I prefer to not cast like that, but it works.
Good luck.

unable to produce random number?

Each object in my warehouseList contains a end stock, safety stock, and a required stock.
My aim to change the end stock of an object if lesser that 10% of the safety stock, and put it in range of -%10 to %5 of the safety stock. To do this im using a randomizer code:
Random random=new Random();
for(Warehouse obj:warehouseList){
double diff=obj.getEndStock()-obj.getSafetyStock();
if(((diff/obj.getSafetyStock())*100)<(-5)){
diff=Math.abs(diff)+(1.05*obj.getSafetyStock())-obj.getSafetyStock();
if(diff<0)
Logging.log(diff,"\n");
int randomNum=0;
double reqStock=0;
double end=obj.getEndStock();
while(((end-obj.getSafetyStock())*100)<(-5)){
randomNum=random.nextInt((int)diff);
reqStock+=randomNum;
end+=randomNum;
}
obj.setRequiredStock(reqStock);
obj.setEndStock(end);
}
}
}
Now to the problem: I'm checking if the diff variable even becomes negative as it doesnt as nothing prints on the console, However whenever i reach the the line randomNum=random.nextInt((int)diff);
the program throws the following error:
Exception in thread "main" java.lang.IllegalArgumentException: n must be positive
at java.util.Random.nextInt(Unknown Source)
at managing.Final.randomWay(Final.java:163)
at managing.Final.main(Final.java:252)
CAN ANYONE HELP?
This is rather simple. Look at these two lines:
double diff=obj.getEndStock()-obj.getSafetyStock();
...
randomNum=random.nextInt((int)diff);
Your diff contains a difference between end stock and safety stock. If your end stock is below safety stock, then diff will contain a negative number.
nextInt method requires a positive integer as a parameter. If your diff is negative, you'll get this error.
EDIT: In response to the comment I re-read the question carefully again and noticed the line I missed. With this line:
diff=Math.abs(diff)+(1.05*obj.getSafetyStock())-obj.getSafetyStock();
You actually can get a 0. (I am assuming that your getSafetyStock would always return a positive number - if it doesn't, then this may be your problem.)
In your code, you are checking whether diff is 0 - but are not doing anything about it, other than printing a log line.
On the whole, it may make sense to print the value of diff regardless of what it is before using it as a parameter for nextInt - for debugging purposes.
I see no check for negative value. Here:
if(diff==0)
Logging.log(diff,"\n");
You check for zero-equality, which is weird for double-typed variable anyway.
It seems I get what's wrong. nextInt method requires it's argument to be positive, but you are checking if it is non-negative. That's what causes the error.
This code prooves my point:
import java.util.Random;
public class RandomMain {
public static void main(String[] args) {
Random random = new Random();
System.out.println(random.nextInt(0));
}
}
it generates an exception:
Exception in thread "main" java.lang.IllegalArgumentException: n must be positive
at java.util.Random.nextInt(Random.java:300)
at RandomMain.main(RandomMain.java:6)

If or If-Else Statement Error

I am trying to write an If or If-Else Statement that protects the program when an integer value is not entered when prompted. I keep getting the error message: '.class' expected.
System.out.println("Type the name of your destination: ");
destination = keyboard.nextLine();
System.out.print(destination + " is how many miles away?");
miles = keyboard.nextInt();
if (miles != int);
{
System.out.println(miles + " is not valid. I will use 100 for the number of miles.");
}
This can not compile:
if (miles != int);
You are comparing a variable with int,which is a type. The compiler suggest you to add .class, so you can obtain the class object, but it is not correct either.
You want to know if an integer value is not entered, but in that case what would happen is that nexInt() would throw a InputMismatchException. What you should do is to check with hasNextInt() if the user is inputing an integer.
int is a keyword and cannot participate in conditional expressions.
There is also a spurious semicolon after your if statement, which acts as the controlled statement, so the following block is unconditional.
The syntax error that you are getting is because int is a keyword. You cannot use it as an identifier.
If you would like to check if a Scanner has an int ready for reading, use keyboard.hasNextInt() in your condition:
int miles;
while (!keyboard.hasNextInt()) {
System.out.println(keyboard.nextLine() + " is not valid. Please enter a different number.");
}
int miles = keyboard.nextInt();
you have a ; after the if statement.
You also have it checking what the user inputs to an int which is not a number. might want to fix that with a number and it should work... from what i can remember from class.
if (miles != int);
You can't test if a number is an int like that. You also have a semi colon at the end of this line (which should be removed). However, there's actually no reason to have that line at all. Because you wrote:
miles = keyboard.nextInt();
miles has to be an int. Otherwise an exception will be thrown by Scanner.
I'm going to start this by saying that you need to do a Java Tutorial. Your code is full of basic mistakes.
(Assuming that keyboard is an instance of Scanner ...)
Mistake 1 - You cannot use a type name or keyword (e.g. int) as a value.
Mistake 2 - You can only use == to compare integer values (like the value of miles) with other numbers.
Mistake 3 - You shouldn't put a semicolon after if (condition). What you have written will compile, but it actually means "if the condition is true, then execute the empty statement". Then you've followed that with a block ... that will be executed unconditionally.
Mistake 4 - In fact, if keyboard.nextInt() encounters something that isn't a valid integer, it will throw an exception; e.g. InputMismatchException. If you want to print out an error message, you need to catch that exception.
Mistake 5 - Obviously you didn't check the javadoc for the nextInt() method ...
To recap ... you are making so many mistakes because you haven't done the tutorial ... or you've been skipping lectures or something.

Categories

Resources