Results 1 to 10 of 10

Thread: Strings

  1. #1
    zoala001 is offline Member
    Join Date
    Jan 2011
    Posts
    15
    Rep Power
    0

    Unhappy Strings

    Hi guys.

    i am stuck in this code. please help me:((

    my code is right but my teacher told me i have to change the code to get the full pointD

    the change i have to do is to print out the last 3 letters when i give any name to the code to count the letters.

    for ex. when we write Angelina Julie, lie shoul be prited out.

    the code can print out the first letter. but not the last 3.


    import java.util.*;

    public class Strängpraktik {

    public static void main (String[] arg){


    Scanner scan = new Scanner(System.in);

    String name, name1;

    int length;

    char firstLetter;


    System.out.println ("Please write your full name: ");

    name = scan.nextLine();

    name = name.trim();

    name1 = name.replaceAll(" ","");

    length = name1.length();


    System.out.println ("your name have " + length + " letters");


    firstLetter = name.charAt(0);


    if ((firstLetter >= 'a' && firstLetter <= 'm') || (firstLetter >= 'A' && firstLetter <= 'M'))
    {

    temp = name.toUpperCase();
    }

    else {

    temp = name.toLowerCase();
    }


    System.out.println(temp);


    name1 = name.replaceAll("a","o");

    name1 = name1.replaceAll("e","y");

    name1 = name1.replaceAll("i","å");

    System.out.println (name1);


    System.out.print(name.charAt(0));

    }

    }
    Last edited by zoala001; 01-01-2011 at 03:59 PM.

  2. #2
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    if you are allowed to use substring, then here's a demo
    Java Code:
    public class PrintLastThree {
        public static void main(String[] args){
            String myString = "Angelina Julie";
            System.out.println("Last 3 letters are: " + myString.trim().substring( myString.length() - 3 ) );
        }
    }

  3. #3
    zoala001 is offline Member
    Join Date
    Jan 2011
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by JavaHater View Post
    if you are allowed to use substring, then here's a demo
    Java Code:
    public class PrintLastThree {
        public static void main(String[] args){
            String myString = "Angelina Julie";
            System.out.println("Last 3 letters are: " + myString.trim().substring( myString.length() - 3 ) );
        }
    }
    thanx alot. i only used the last line of your code and it worked. i can print out the last 3 letters when i give any name to the code.
    but a question comes up: why did u use substrings? or please explain the last line cuz i have to explain every single thing to my teacher:((
    Last edited by zoala001; 01-01-2011 at 04:13 PM.

  4. #4
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by zoala001 View Post
    thanx alot. i only used the last line of your code and it worked. i can print out the last 3 letters when i give any name to the code.
    but a question comes up: why did u use substrings? or please explain the last line.
    because taking out part of a string from a larger string is a very common task that all of us do? That's why there's a method for it. So use it.
    You can certainly go the longer way, like using a for loop to iterate through the strings back ward, as in
    Java Code:
    for (int i = string.length; i>= string.length-3 ;i--){
      ......
    }
    or
    Java Code:
    for ( int i = string.length-3 ; i<=string.length; i++){
      ....
    }
    but that's just long winded.

  5. #5
    zoala001 is offline Member
    Join Date
    Jan 2011
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by JavaHater View Post
    if you are allowed to use substring, then here's a demo
    Java Code:
    public class PrintLastThree {
        public static void main(String[] args){
            String myString = "Angelina Julie";
            System.out.println("Last 3 letters are: " + myString.trim().substring( myString.length() - 3 ) );
        }
    }
    Quote Originally Posted by JavaHater View Post
    because taking out part of a string from a larger string is a very common task that all of us do? That's why there's a method for it. So use it.
    You can certainly go the longer way, like using a for loop to iterate through the strings back ward, as in
    Java Code:
    for (int i = string.length; i>= string.length-3 ;i--){
      ......
    }
    or
    Java Code:
    for ( int i = string.length-3 ; i<=string.length; i++){
      ....
    }
    but that's just long winded.
    Thanxxxxxx alot.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by zoala001 View Post
    Thanxxxxxx alot.
    Here's the example again:

    Java Code:
    public class PrintLastThree {
        public static void main(String[] args){
            String myString = "  Angelina Julie   ";
            System.out.println("Last 3 letters are: " + myString.trim().substring( myString.length() - 3 ) );
        }
    }
    Note that there are a few leading and trailing spaces in the String; run it and see for yourself what happens. The example isn't correct, be careful with, don't just simply copy and paste it and assume it's correct.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    zoala001 is offline Member
    Join Date
    Jan 2011
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Here's the example again:

    Java Code:
    public class PrintLastThree {
        public static void main(String[] args){
            String myString = "  Angelina Julie   ";
            System.out.println("Last 3 letters are: " + myString.trim().substring( myString.length() - 3 ) );
        }
    }
    Note that there are a few leading and trailing spaces in the String; run it and see for yourself what happens. The example isn't correct, be careful with, don't just simply copy and paste it and assume it's correct.

    kind regards,

    Jos
    thanx alot. is that correct to just copy the last line of the code you have written and add it to the prog.? cuz i did it and it worked. whan i run the prog. asks me to give the name. after doing some change and counting the letters, prints out the last 3 letters.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by zoala001 View Post
    thanx alot. is that correct to just copy the last line of the code you have written and add it to the prog.? cuz i did it and it worked. whan i run the prog. asks me to give the name. after doing some change and counting the letters, prints out the last 3 letters.
    No it isn't correct to copy that line varbatim; as you could've seen it goofs when there are leading and/or trailing spaces in the String. Split the line and check if it has at least three characters in it:

    Java Code:
    myString= myString.trim(); // assuming myString isn't null
    if (myString.length() >= 3)
       System.out.println("Last 3 letters are: " + myString.substring( myString.length() - 3 ) );
    else
       // what to do if it contains less than three chars?
    If someone dumps boilerplate code at you don't just blindly copy and paste it because the code might not be valid and the corner circumstances may even be trivial.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by zoala001 View Post
    thanx alot. is that correct to just copy the last line of the code you have written and add it to the prog.? cuz i did it and it worked. whan i run the prog. asks me to give the name. after doing some change and counting the letters, prints out the last 3 letters.
    you can do the trim() first, if you don't want the leading/trailing spaces
    Java Code:
    public class PrintLastThree {
        public static void main(String[] args){
            String myString = "   Angelina Julie   ".trim();
            System.out.println("Last 3 letters are: " + myString.substring( myString.length() - 3 ) );
        }
    }

  10. #10
    zoala001 is offline Member
    Join Date
    Jan 2011
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by JavaHater View Post
    you can do the trim() first, if you don't want the leading/trailing spaces
    Java Code:
    public class PrintLastThree {
        public static void main(String[] args){
            String myString = "   Angelina Julie   ".trim();
            System.out.println("Last 3 letters are: " + myString.substring( myString.length() - 3 ) );
        }
    }

    i need a code to add to my own code and when i write any name prints out the last 3 letters. i dont need a code to print out the last 3 letters of Angelina Julie's name.her name was just an example.

    by the way where shoul i add this code to my own code.
    i need a code to just copy and paste in my original code.

Similar Threads

  1. Binary Strings
    By Zack in forum New To Java
    Replies: 0
    Last Post: 06-25-2010, 07:15 AM
  2. It is possible in Strings..?
    By mlibot in forum New To Java
    Replies: 1
    Last Post: 03-12-2010, 05:30 AM
  3. The java Strings
    By greenteachacha in forum Jobs Discussion
    Replies: 8
    Last Post: 04-16-2009, 05:08 AM
  4. Subtracting Strings
    By lazygun247 in forum New To Java
    Replies: 6
    Last Post: 03-29-2009, 10:05 PM
  5. comparing strings
    By diggitydoggz in forum New To Java
    Replies: 7
    Last Post: 12-23-2008, 04:40 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
  •