The main imperial transformation task

Coding calculation tool for my Android. At the entrance is the distance in feet and inches.

I have two inputs (input3 and input4) for legs and inches respectively. In my calculation, I am trying to convert these two inputs to a decimal number, which will be used in the rest of the equation. Here is 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. 4 reaches a penalty, but 6 inches by default is 0 when it is divisible by 12. Thus, my result is 4.0. I tried to reduce the calculation to JUST division operations, and the result was 0.0

What am I doing wrong? (fyi: this is my first time using Java)

+4
source share
3 answers

Your types are wrong. You parse them as ints when they really should be doubled.

Try:

 double imperial1 = Double.parseDouble(strValue3) + (Double.parseDouble(strValue4) / 12.0); 
+8
source

... what Eli said.

I just wanted to ask why your variables are called strValue3 and strValue4. I suppose this is generated, but you should get used to naming things. I could go with โ€œlegsโ€ and โ€œinchesโ€ :)

+1
source

Eli's answer will work fine. I just send the answer to your question (comment) to Eli's answer.

 Can you explain what a try-catch block is? 

You must first understand what an exception is. An exception is an event that occurs during the execution of your program, which violates the normal flow of program instructions.

There are 3 types of exceptions in Java:

  • Runtime exceptions : An exception is called a runtime exception if its data type is java.lang.RuntimeException or its subclass.

  • Excluded exceptions: An exception is called a checked exception if its data type is a child of java.lang.Exception , but not a child of RuntimeException .

  • Errors: An exception is called an error if its data type is a child of the java.lang.Error class. The error is related to problems that occur outside your application and usually does not try to repair the errors.

For a more detailed description of exceptions, read this .

To catch and handle these exceptions, you use try-catch blocks. For example, you use the Double.parseDouble function. If the parameter in this function is not a valid number, for example, if the user set the string "NotANumber", you will try to convert it to double, then NumberFormatException will be Double.parseDouble . If you cannot handle this error, your program will exit unexpectedly.

So you should write something like the following (including the positive number function 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. } 
+1
source

Source: https://habr.com/ru/post/1299260/


All Articles