Results 1 to 6 of 6
  1. #1
    Sailendra is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Need help for a program in Core Java

    Can anyone help me in writing the code in core java for the following-
    Input three numbers through Scanner class in variables a,b c.
    If a is inputted 1, b inputted null(or blank) and c inputted 3 then output should show a=1 & b=3.
    If a is null(or blank), b is 1,c is 4, then output- b=1 & c=4.
    If a is 1, b is 2, c is 3, then output- a=1,b=2 & c=3.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Need help for a program in Core Java

    No, it doesn't work this way. You write the code and ask specific questions when you get stuck. Nobody will do your homework for you.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Sailendra is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Re: Need help for a program in Core Java

    Quote Originally Posted by PhHein View Post
    No, it doesn't work this way. You write the code and ask specific questions when you get stuck. Nobody will do your homework for you.
    Okay, then how can i get an "&" before the last variable... I need to know the code for that part only...
    I know following code will work but it's not efficient...

    if(!a.equals("")&&!b.equals("")&&!c.equals("")){
    System.out.println("a is "+a+", b is "+b+" & c is "+c);
    }
    if(a.equals("")&&!b.equals("")&&!c.equals("")){
    System.out.println("b is "+b+" & c is "+c);
    }
    if(!a.equals("")&&b.equals("")&&!c.equals("")){
    System.out.println("a is "+a+" & c is "+c);
    }
    if(!a.equals("")&&!b.equals("")&&c.equals("")){
    System.out.println("a is "+a+" & b is "+b);
    }
    if(a.equals("")&&b.equals("")&&!c.equals("")){
    System.out.println(" c is "+c);
    }
    if(!a.equals("")&&b.equals("")&&c.equals("")){
    System.out.println("a is "+a);
    }
    if(a.equals("")&&!b.equals("")&&c.equals("")){
    System.out.println("b is "+b);
    }

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Need help for a program in Core Java

    Hi Sailendra,

    Instead of checking for every possible condition and printing out the associated comment you could get clever with the code.
    You could set a variable to count the number of varaibles which are not null then based on this do something like the following:

    • Print out the first letter then depending on the counter print either a ',' or '&'.
    • Print the second letter then assuming all three letters were on null print out the '&'.
    • If the third letter was not null then print this out.

    Regards.
    Last edited by Ronin; 03-05-2013 at 01:56 PM. Reason: Formatting

  5. #5
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: Need help for a program in Core Java

    Quote Originally Posted by Sailendra View Post
    Can anyone help me in writing the code in core java for the following-
    Input three numbers through Scanner class in variables a,b c.
    If a is inputted 1, b inputted null(or blank) and c inputted 3 then output should show a=1 & b=3.
    If a is null(or blank), b is 1,c is 4, then output- b=1 & c=4.
    If a is 1, b is 2, c is 3, then output- a=1,b=2 & c=3.
    I think you will use if... else condition and print the output inside the condition what you need exactly...

    Like..

    Java Code:
    if(a.equals("1") && b.equals("")&& c.equals(3)){
    System.out.println("a=1 & b=3");
    }
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Need help for a program in Core Java

    Quote Originally Posted by tamilarasi View Post
    I think you will use if... else condition and print the output inside the condition what you need exactly...
    That's going to be verbose and inefficient. Better to build a String (using a StringBuilder), append components conditionally, then print that with a single println statement.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Core Java
    By murali23krishna in forum New To Java
    Replies: 7
    Last Post: 02-25-2011, 05:56 AM
  2. After getting knowledge core java...
    By spk2712 in forum New To Java
    Replies: 2
    Last Post: 01-23-2011, 11:14 AM
  3. a free program that show exactly when and on which core each process
    By simorgh in forum Threads and Synchronization
    Replies: 3
    Last Post: 05-01-2010, 12:01 PM
  4. query on core java
    By venkatallu in forum New To Java
    Replies: 1
    Last Post: 09-01-2008, 02:23 PM
  5. Core Java Vs. J2EE
    By evanp in forum New To Java
    Replies: 2
    Last Post: 07-23-2008, 01:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •