-1

I want to check whether a length of an input text is divisible by 2.

so it will be like

if my text length is 3, the result will be 1.5 and it will display not divisible by 2 and if my text length is 6, the result will be 3.0 and it will display divisible by 2

but my codes will display the output "not divisible by 2" regardless what is the text length. what have I done wrong?

import java.util.Scanner;

public class Test1 {
    public static void main (String[]args)
    {

        String a  =null;
        int l = 0;
        double result = 0.0;

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter your string\n");
        a = scan.nextLine();

        l = a.length();

        result = (double)l/2.0;

        System.out.println(result);

        if((double)result % 2 != .0) {
            System.out.println("not divisiable by 2");
        }
        else {
             System.out.println("divisiable by 2");
        } 

    }
}
  • possible duplicate of [How do I check if an integer is even or odd?](http://stackoverflow.com/questions/160930/how-do-i-check-if-an-integer-is-even-or-odd) – StackFlowed Oct 12 '14 at 03:41
  • You are storing the length in `l`, then dividing `l` by 2 and storing that in `result` (as a double), and then checking if `result / 2` is divisible by 2... You are dividing by 2 twice! – Barranka Oct 12 '14 at 03:46

6 Answers6

3

The mod operation is your friend but only with integers...

if (integer % divisibleBy == 0) { do stuff; }

Edit: I also found this page that does a really good job outlining the various uses mod the mod operator and explains why your double mod doesn't work like you expect.

Edit: Also more review of your code; it looks like you divide by 2 and then do mod by 2. So 6/2 = 3 and 3 is not even. Wonder if your code would work if you used 8 -> 8/2 = 4 and 4%2 = 0.

Andrew White
  • 52,720
  • 19
  • 113
  • 137
1

Check the length of the text, then do a modulo to it.

mcr619619
  • 435
  • 1
  • 4
  • 13
1

Modulo , an operation which checks if a number will have a remainder if its divided by another number

yourNumber%5 == 0

where yourNumber is the lenght of your String. Take it from here.

Barranka
  • 20,547
  • 13
  • 65
  • 83
shepard23
  • 148
  • 2
  • 13
0

First, length() returns an int, and it's simpler to work with integers, so why are you casting the length to double? The % (modulo) operator is meant to work with integers, not doubles; remember, double numbers are floating-point numbers, so there's always room for numerical error if you use them.

Second, you don't need to use so many variables; keep it simple:

a = scan.nextLine();
if(a.length() % 2 == 0)
    System.out.println("Length of string is divisible by 2");
else
    System.out.println("Length of string is not divisible by 2");

Easier to read (and write), don't you think?

Barranka
  • 20,547
  • 13
  • 65
  • 83
0
import java.util.Scanner;

public class Test1 {
    public static void main (String[]args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter your string\n");
        String str = scan.nextLine();
        int length = str.length();
        if(length % 2 !=0) {
            System.out.println("not divisible by 2");
        }
        else {
             System.out.println("divisible by 2");
        } 

    }
}

There were a lot of problems with your code:

  1. You are using a double to store the result for some reason, when it should've been an int
  2. Your variable names are not descriptive of what they are for.
  3. There is no need to initialize variables to a default value only to overwrite them with new values.
dramzy
  • 1,379
  • 1
  • 11
  • 25
0

haha i hate these little mistakes. i do them all the time. Just switch both to %

String a  =null;
    int l = 0;
    double result = 0.0;

    Scanner scan = new Scanner(System.in);
    System.out.println("Enter your string\n");
    a = scan.nextLine();

    l = a.length();
        //this is your problem
    result = (double)l%2.0;
        //this is your problem

    System.out.println(result);

    if((double)result % 2 != .0) {
        System.out.println("not divisiable by 2");
    }
    else {
         System.out.println("divisiable by 2");
    }