Results 1 to 16 of 16
  1. #1
    kornwheat is offline Member
    Join Date
    Oct 2009
    Posts
    1
    Rep Power
    0

    Default Need help writing program to compare 2 strings using a loop

    So I'm trying to write a program that will compare two strings. Basically I want it to see if the first string makes up the beginning of the second string and then tell me yes if it does and no if it doesn't.
    For example,
    If string 1 was test and string 2 was testing then it would say yes.
    If string 1 was stew and string 2 was testing then it would say no.

    This is what I have so far:
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter two strings to test if the first is a prefix of the second");
    String string1 = scan.next();
    String string2 = scan.next();
    String string12 = string1.toLowerCase();
    String string22 = string2.toLowerCase();
    int len1 = string1.length();
    int len2 = string2.length();

    for (int i = 0; i < len2; i++) {
    if (string22.charAt(i) != string12.charAt(i)){
    System.out.println("Is " + string1 + " a prefix of " + string2 + "?");
    System.out.println("No");
    break;
    }
    else if (len1 > len2){
    System.out.println("Is " + string1 + " a prefix of " + string2 + "?");
    System.out.println("No");
    break;
    }
    else if (string22.charAt(i) == string12.charAt(i)) {
    System.out.println("Is " + string1 + " a prefix of " + string2 + "?");
    System.out.println("Yes");
    break;
    }
    }
    }
    }

    But all this is doing is comparing the first letter of each string. I can't seem to get it to actually test the full length of the first string into the second string.

    Oh and I have to use a loop for this. I don't want to use the string class function startsWith()

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You have a break in every branch of your if-elses. That means the loop never completes and is always broken at the first time of entrance.
    Why are you doing the (len1 > len2) check inside the loop? It can be done before the loop because it doesn't need any loop information.

    Then your (string22.charAt(i) == string12.charAt(i)) should not break if the test is true. Read up on the "break" keyword to understand what it does and therefore how you are misusing it.

  3. #3
    anubhavranjan is offline Member
    Join Date
    Sep 2009
    Location
    Bangalore, India
    Posts
    10
    Rep Power
    0

    Smile

    Here I've modified your program, I guess this should work as you wanted...



    import java.io.*;
    import java.util.*;
    public class StringCompare
    {
    public static void main(String args[])
    {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter two strings to test if the first is a prefix of the second");
    String string1 = scan.next();
    String string2 = scan.next();
    String string12 = string1.toLowerCase();
    String string22 = string2.toLowerCase();
    int len1 = string12.length();
    int len2 = string22.length();
    int i=0;
    if(len1>len2)
    {
    System.out.println("No String1 is not the Prefix of String2");
    }
    else
    {
    while(i < len1)
    {
    if(string12.charAt(i)==string22.charAt(i))
    {
    i++;
    }
    else
    {
    break;
    }
    }
    if(i==len1)
    {
    System.out.println("Is " + string1 + " a prefix of " + string2 + "?");
    System.out.println("Yes");
    }
    else
    {
    System.out.println("Is " + string1 + " a prefix of " + string2 + "?");
    System.out.println("No");
    }
    }
    }
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default

    Why?
    Why just hand code over?
    Spoonfeeding is not a Good Thing...

  5. #5
    anubhavranjan is offline Member
    Join Date
    Sep 2009
    Location
    Bangalore, India
    Posts
    10
    Rep Power
    0

    Red face

    no i just did it purposefully cos in the beginning some help is required...if in the beginning they are not helped then it becomes a problem in the later stages.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default

    Sorry for that, possibly a bit quick off the mark, however simply handing over code does not generally help. Guiding people to a solution is the preferable way forward. In other words helping them to come up with the solution themselves.

  7. #7
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default

    Quote Originally Posted by anubhavranjan View Post
    no i just did it purposefully cos in the beginning some help is required...if in the beginning they are not helped then it becomes a problem in the later stages.
    Help is needed, yes. Giving code is not needed. In fact, it just makes it worse. Because what will happen is that people get spoon fed a bunch of little answers, but then when they need to put them together into a bigger answer they have no idea what to do.

    OP: String.subtring could be very very helpful.

    Edit: Also, please use code tags in the future.
    Last edited by senorbum; 10-06-2009 at 11:27 PM.

  8. #8
    SF163 is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default

    I wouldn't say it always makes it worse. I've learned that its often needed because you can at least follow the program once you have the code instead of nothing. Through that you can trace every step back and learn how it was produced

  9. #9
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    I have to agree with SF163 on this one. A good example should invoke new thought processes in one who wants to learn

    Java Code:
    import java.util.Scanner;
    
    public class StringCompare {
      public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter two strings to test if the first is a prefix of the second");
        String string1 = scan.next();
        String string2 = scan.next();
        
        System.out.print("Is \"" + string1 + "\" a prefix of \"" + string2 + "\"? ");
    
        if (string2.toLowerCase().startsWith(string1.toLowerCase())) {
          System.out.println("Yes");
        }
        else {
          System.out.println("No");
        }
      }
    }

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by SF163 View Post
    I wouldn't say it always makes it worse. I've learned that its often needed because you can at least follow the program once you have the code instead of nothing. Through that you can trace every step back and learn how it was produced
    I've found that often the ones that ask here for complete code solutions and who get them, keep coming back and asking for the same, which discourages me. I agree with the folks who recommend "nudging" towards a solution. The more you can make the original poster think for them self, the better off they'll be in the long run.

  11. #11
    SF163 is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default

    yeah that does seem to be a problem, but often enough the hardest part of creating a program is starting it. Usually when you post a problem, people post and reply with what youre supposed to do, but in words. I don't think that people should give the code for whole problem, but giving some and explaining what is going on should not be frowned upon.

  12. #12
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Quote Originally Posted by SF163 View Post
    yeah that does seem to be a problem, but often enough the hardest part of creating a program is starting it. Usually when you post a problem, people post and reply with what youre supposed to do, but in words. I don't think that people should give the code for whole problem, but giving some and explaining what is going on should not be frowned upon.
    I agree that it shouldn't be frowned upon, but it shouldn't be done either. I mean, you rob them the joy and feeling of success. I've been a tutor before, and assist on forums, and I have found that giving answers, while helpful, takes away from the joy of learning.

    Try to remember how you felt after solving a difficult problem. If someone gave you the answer, it would have robbed you of that feeling. If you believe that the other person CAN reach a solution (in time), then give them that time. They will learn much more by making their own mistakes, and they will enjoy the feeling of success much more when they figure it out (since they WILL figure it out).
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  13. #13
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Yep, there's a lot of thought robbing going on on these forums. Those students simply hand it in as theirs and don't even bother to see how it works.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default

    Quote Originally Posted by CodesAway View Post
    Try to remember how you felt after solving a difficult problem. If someone gave you the answer, it would have robbed you of that feeling.
    Robbing someone of their eureka moment...when the little lightbulb comes on, and suddenly it's "Aha!"...:)

  15. #15
    iceagecoming is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by CodesAway View Post
    Try to remember how you felt after solving a difficult problem. If someone gave you the answer, it would have robbed you of that feeling. If you believe that the other person CAN reach a solution (in time), then give them that time. They will learn much more by making their own mistakes, and they will enjoy the feeling of success much more when they figure it out (since they WILL figure it out).
    Did you go to a Montessori school? This is exactly what it's all about.

    I agree, it's a bit shocking to see people providing code solutions. Nudging is definitely better (for the student).

  16. #16
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    I did not, it's my way of thinking, due to my personality type. I'm an ENFJ - The Giver. I could not imagine stealing the joy of learning from another, even under the guise of helping them.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

Similar Threads

  1. Due Tommorow Writing a Program Please Help
    By gallimaufry in forum New To Java
    Replies: 4
    Last Post: 10-23-2008, 03:15 AM
  2. [SOLVED] Cant compare strings sent by client
    By christuart in forum Networking
    Replies: 15
    Last Post: 09-01-2008, 03:24 AM
  3. how begin with writing a new program ?
    By dimitrist in forum New To Java
    Replies: 11
    Last Post: 05-13-2008, 03:49 AM
  4. jsp program for client side printer to print these 2 strings on 3/3
    By for453 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-07-2007, 04:42 PM
  5. how to compare two strings
    By elizabeth in forum New To Java
    Replies: 7
    Last Post: 08-06-2007, 03:57 AM

Posting Permissions

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