Home > OS >  If-else One line statement (with string and integer) [closed]
If-else One line statement (with string and integer) [closed]

Time:09-17

I'm creating a program. This code below is the Java program. Checking if the value of B and H, input by the user is equal to zero. If it is true, then a math formula will execute which is p = b*h then if it is false, a string will be shown on the console. But the thing is, it keeps on giving me an error that string cannot be converted to int or int cannot be converted to string.

Here is the code:

import java.util.Scanner;

public class StaticProject{
    
    static {
        Scanner input = new Scanner (System.in);
        int B = input.nextInt();
        int H = input.nextInt();
        int P;
        
        
        int solution = B>=0 && H>=0 ?  P = B*H : "java.lang.Exception: Breadth and height must be positive";
    } 

How can I improve my coding when it comes in doing this oneline bi-conditionals statement? Any tips?

CodePudding user response:

your solution variable Type is int, and your else statement returns an String, this is the cause of error.

you can change your code to this:

Scanner input = new Scanner (System.in);
    int B = input.nextInt();
    int H = input.nextInt();
    int P;

    if (B >= 0 && H >= 0) {
        P = B*H;
    } else {
        throw new Exception("Breadth and height must be positive");
    }

    System.out.println(P);
}

CodePudding user response:

You are trying to assign string to an int variable, if your statement is evaluated as false. You need to use if/else condition, which both sides have the same type. In your case, it's not possible. another possibility is use System.out.println() command, to print the result of whatever resulted by the condition.

CodePudding user response:

There is no virtue in writing code in as few lines as possible. By all means, don't write unnecessary code, but just don't think that concise code is objectively "better".

The best type of code to write is simple code. Maybe it's a little more verbose; but if you write code that people can understand at a glance, they know what it does straight away.

Here is how I would write it:

if (B < 0 || H < 0) {
  // Actually, non-negative, rather than positive.
  throw new Exception("Breadth and height must be positive");

  // Or, maybe, don't even use exceptions at all:
  // System.out.println("The message");
  // return;
}

// No need for `solution` variable.
P = B * H;
System.out.println(P);

Of course, you might argue this isn't code everybody will understand at a glance. Sure. Readability is subjective; but I assert that anybody with just a little bit of experience in Java can work it out.


You can write a conditional ?: expression which results in an exception being thrown in the case of the condition being false.

// Define a method which does the throwing.
static int justThrow(String message) throws Exception {
  throw new Exception(message);
}

// In the main method:
int solution = B>=0 && H>=0 ?  P = B*H : justThrow("Breadth and height must be positive");

I emphasize that you can, but you shouldn't do this. It's obscure. It's trying to be "a bit too clever". It will have people reading your code and scratching their heads, because they will wonder why you wrote a method for something so simple; what is the solution variable for, given that it will have the same value as P etc.

  • Related