Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Why my String does not appear ?

  1. #1
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Why my String does not appear ?

    Instruction: 73. Write a Java program to create a new string taking first and last characters from two given strings. If the length of either string is 0 use "#" for missing character.
    Test Data: str1 = "Python"
    str2 = " "
    Sample Output:
    P#

    Link of PB : https://www.w3resource.com/java-exer...asic/index.php

    Here is my program for this pb :
    try (Scanner input = new Scanner(System.in)) {
                // get user String input
                System.out.print("Input Str1 : ");
                String Str1 = input.next();
                System.out.print("Input Str2 : ");
                String Str2 = input.next();
     
                // check String Str1 & Str2 length for diff case
                if (Str1.length() != 0 && Str2.length() != 0) { // case a
                    System.out.println(Str1.substring(0, 1) + Str2.substring(Str2.length() - 1, Str2.length()));
                } else if (Str1.length() != 0 && Str2.length() == 0) { // case b
                    System.out.println(Str1.charAt(0) + "#");
                } else
                    System.out.println("#" + Str2.substring(Str2.length() - 1, Str2.length())); // case c
            }

    The issue is that when I test Case b & c it does return me for example P" (case b) or "P (case c). Why it is not P# or #P as I am expecting with for input Python and ""
    By the way, Case a works with no issue.
    Last edited by siid14; March 11th, 2022 at 03:36 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why my String does not appear ?

    Please copy the program's actual output and paste it here. Add comments to point out what was unexpected.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    siid14 (March 11th, 2022)

  4. #3
    Junior Member BosnianKingdom's Avatar
    Join Date
    Mar 2022
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Why my String does not appear ?

    The name of your variables should be lower case, just fyi. I would use the nextLine method of the Scanner class. Instead of complicating things by dealing with the 2 strings at the same time, I would divide it into 2 separate sections like this:

    try (Scanner input = new Scanner(System.in)) {
    	// get user String input
    	System.out.print("Input Str1 : ");
    	String str1 = input.nextLine();
    	System.out.print("Input Str2 : ");
    	String str2 = input.nextLine();
    	String result;
     
    	if(str1.length() == 0){
    		result = "#";
    	}
    	else{
    		result = String.valueOf(str1.charAt(0));
    	}
     
    	if(str2.length() == 0){
    		result += "#";
    	}
    	else{
    		result += String.valueOf(str2.charAt(str2.length()-1));
    	}
     
    	System.out.println(result);
    }

  5. #4
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Why my String does not appear ?

    I saw your code @BosnianKingdom, it's pretty quick and easy to deal with. Thank you, it would give me another way to handle it. But still, I'm trying to understand why the "#" does not appear in my output. Did something wrong with my code? Here is the output I got :
    Input Str1 : ""
    Input Str2 : ateyaba
    "a


    --- Update ---

    Also, @BosnianKingdom, why did you use String.valueOf? I think it is unnecessary. Your code works well with the followings (below) :
    result += str1.charAt(0);
    result += str2.charAt(str2.length()-1);

    After trying your code, here is my output :
    Input Str1 : ateyaba
    Input Str2 : "
    a"

    I think the issue should come from the compiler.. ? Maybe?

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why my String does not appear ?

    why the "#" does not appear
    What conditions need to be true for that?
    Are those conditions met with your input?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #6
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Why my String does not appear ?

    Here you have a full details about the condition to be meet :

    Instruction: 73. Write a Java program to create a new string taking first and last characters from two given strings. If the length of either string is 0 use "#" for missing character.
    Test Data: str1 = "Python"
    str2 = " "
    Sample Output:
    P#

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why my String does not appear ?

    Sorry, I was asking about the conditions that are coded in your program.

    Print out the lengths of the two Strings to see what is being tested.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Sequences (convert char[] to String) [Replacing String with Character]
    By tpolwort in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 25th, 2013, 02:56 PM
  2. Replies: 6
    Last Post: June 3rd, 2013, 04:57 AM
  3. Replies: 2
    Last Post: March 28th, 2013, 09:54 AM
  4. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  5. Replies: 3
    Last Post: June 14th, 2009, 09:31 PM

Tags for this Thread