-1

I am trying to print out an int whenever a an int is inputted for my double. e.g. user input 123456, outputs 123456; user inputs 1.0, outputs 1.0. As of the moment my code prints a double regardless. This is for my fullCellText method.

My code:

package textExcel;

public class ValueCell extends RealCell {
    private boolean isInt;
    public ValueCell(String cell) { 
        super(cell);
        // TODO Auto-generated constructor stub
    }
    public ValueCell(String cell, boolean isInt) { 
        super(cell);
        this.isInt = isInt;
        // TODO Auto-generated constructor stub
    }
    public String fullCellText() {
        return "" + cell;
    }

}
insanity_serum
  • 65
  • 1
  • 1
  • 8
  • 1
    Possible duplicate of [Checking if a number is an Integer in Java](https://stackoverflow.com/questions/5502548/checking-if-a-number-is-an-integer-in-java) – Samuel Philipp Mar 17 '19 at 20:53

2 Answers2

0

Not sure if I understood your question correctly but I think you are trying to print a double with no decimal values.

You can convert the double value into a int value by doing something like: int x = (int) y here y is double. Now if you print x then you won't get any decimal places.

Note: int typecasting is not a good idea as your double may go beyond the range.

Coding Bad
  • 252
  • 1
  • 4
  • 12
0

I can suggest to dot check in input String

if (yourString.contains("."))

Its not best way to solve this, but it works.

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String buff = scanner.nextLine();
        if (buff.contains(".")){
            double tempDouble = Double.parseDouble(buff);
            System.out.println(tempDouble);
        } else {
            int integer = Integer.parseInt(buff);
            System.out.println(integer);
        }
    }