help me out ?? how can i wrote if condiiton using try catch block .

asked 20 Mar '15, 07:54

Muhammad%20Hassan%20Durrani's gravatar image

Muhammad Has...
4311224
accept rate: 0%


i wrote this code and then chek 1 condition but it is not giving mw the answear . and also how can i find string lenght . please help needed . String string = ""; String string1 = "";

    String m = "";
    try 
    {
        InputStreamReader input = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(input);
        string = reader.readLine();
        string1 = reader.readLine();

if(string==string1) m="Names are identical";

    } 
        catch (NumberFormatException ex) 
        {
            System.out.println("Not a number !");
        }   
        catch (IOException e) 
        {
        e.printStackTrace();

}
System.out.println(m);
link

answered 20 Mar '15, 08:10

Muhammad%20Hassan%20Durrani's gravatar image

Muhammad Has...
4311224
accept rate: 0%

  1. to determine the length of String you need to write variable name(in which the String is stored) then dot(.) length() . For example;

      public static void main(String[] args) 
      {
        String name ="mohammad";
        System.out.println(name.length());
      }
    

it will answer "8" coz name length is 8

link

answered 26 Apr '15, 11:46

rahul's gravatar image

rahul
7916
accept rate: 15%

edited 26 Apr '15, 11:48

I wrote this code . from this code em just getting length answear only . and names identicle conditional is not working . help me out .

    String string = "";
    String string1 = "";
    String m = "";
try 
{
    InputStreamReader input = new InputStreamReader(System.in);
    BufferedReader reader = new BufferedReader(input);
    string = reader.readLine();
    string1 = reader.readLine();
    int n = string.length();
    int l1 = string1.length();

    if(string == string1)
    {
        System.out.println("Names are identical");
    }
    else if (string != string1 && n == l1)
    {
        System.out.println("Name lengths are equal");
    }
} 
    catch (NumberFormatException ex) 
    {
        System.out.println("Not a number !");
    }   
    catch (IOException e) 
    {
    e.printStackTrace();
    }
link

answered 30 Apr '15, 06:24

Muhammad%20Hassan%20Durrani's gravatar image

Muhammad Has...
4311224
accept rate: 0%

please help me out .... em ran out of fuel and for refuel em need to solve remaining task

(30 Apr '15, 09:27) Muhammad Has...

if(string == string1)
the above condition is wrong u have to use following :
if(string.equals( string1))

u have to use equals operator in order to compare 2 Strings since String is class here and as u declared "string" and "string1" are object of class String or u can say that they are refernece variable so in order to compare the value of the refrence variable or object we use equals() method which is defined in class String.

link
This answer is marked "community wiki".

answered 15 May '15, 14:22

raswkp's gravatar image

raswkp
363
accept rate: 20%

edited 15 May '15, 14:23

public static void main(String[] args) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in);); String name1= reader.readLine(); String name2= reader.readLine(); if(name1.equals(name2)) { System.out.println("Names are identical"); } else if (name1.length()==name2.length()) { System.out.println("Name lengths are equal"); }

}
link

answered 05 Feb '16, 08:34

Mohsin%20Choudhary's gravatar image

Mohsin Choud...
11
accept rate: 0%

edited 05 Feb '16, 08:37

public static void main(String[] args)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in);
String name1= reader.readLine();
String name2= reader.readLine();
if(name1.equals(name2))
{
    System.out.println("Names are identical");
}
else if (name1.length()==name2.length())
{
    System.out.println("Name lengths are equal");
}

}
link

answered 05 Feb '16, 08:38

Mohsin%20Choudhary's gravatar image

Mohsin Choud...
11
accept rate: 0%

edited 05 Feb '16, 08:38

There is no need to use try-catch block here.

link

answered 08 Apr '17, 16:51

noelo's gravatar image

noelo
102
accept rate: 0%

import java.io.*;

public class Solution { public static void main(String[] args) throws Exception { BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));

    String a = buff.readLine();
    String b = buff.readLine();

    if(a.equals(b)){
        System.out.println("Names are identical");
    }
    else if(!a.equals(b)){
        if(a.length()==b.length()){
            System.out.println("Name lengths are equal.");
        }
    }

}

}

Doesn't work and i dont know why. Any advice, please?

link

answered 23 Jul '17, 05:51

YourDoom's gravatar image

YourDoom
1811
accept rate: 35%

BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
        String name1=reader.readLine();
        int len3=name1.length();

        String name2=reader.readLine();
        int len2=name2.length();

                if (name1.equals(name2)) {System.out.println("Names are identical");}

                if (len3==len2) {System.out.println("Name lengths are equal");}
why it doesn't work? in Itellije idea it works but doesn't pass test.
link

answered 27 Mar, 09:26

TheHex's gravatar image

TheHex
1
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×67
×40
×35

Asked: 20 Mar '15, 07:54

Seen: 3,201 times

Last updated: 27 Mar, 09:26