Results 1 to 8 of 8

Thread: Java: Need advice on String manipulation program

  1. #1
    Join Date
    Mar 2008
    Location
    Ireland
    Beans
    781
    Distro
    Ubuntu 15.04 Vivid Vervet

    Java: Need advice on String manipulation program

    I wrote this simple program a while back but it doesn't quite work properly.

    For some reason the surname is repeated. Any ideas?

    PHP Code:
    class StringTest
    {
      public static 
    void main(String[] args)
      {
        
    String str1 = new String();
        
    String str2 = new String();
        
    char initial=' 'firstletsn;
        
    int i=0start=0end=0endstr;
      
        
    System.out.print("Enter your firstname followed by your surname ");
        
    str1 EasyIn.getString();
        
    endstr str1.length();
      
        while (
    endstr)
        {
            
    System.out.println(str1.charAt(i)+ " " i);
            
    i++;
        }
        
        
    i=0;
        
            while(
    str1.charAt(i)== ' ')
            
    i++;
            
    start=i;
            
            
    initial str1.charAt(start);
            
    str1 str1.toLowerCase();
             
            while ((
    str1.charAt(i)>='a')&&(str1.charAt(i)<='z'))
            
    i++;
            
            
            while(
    endstr)
            {
                
    System.out.println(str1.charAt (i) + " " i);
                
    i++;
            }
            
    end i;
        
    str2 str1.substring(start,end);
        
    firstletsn str2.charAt(start);
        
    str2 str2.toUpperCase();
        
    firstletsn str2.charAt(0);
        
    str2 str2.substring(1,str2.length());
        
    str2 str2.toLowerCase();
        
        
    System.out.println(firstletsn+str2 " " initial ".");
        
      }

    I was gonna study this for an upcoming test but since its faulty then there is no point in studying something wrong!
    Projects: PhotoFiltre LX | Xwii | URT-2D | TTM

  2. #2
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,294
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Java: Need advice on String manipulation program

    What's EasyIn?
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Mar 2008
    Location
    Ireland
    Beans
    781
    Distro
    Ubuntu 15.04 Vivid Vervet

    Re: Java: Need advice on String manipulation program

    It is just a class that takes input.. no need to worry about that.

    Works the same as scanner.
    Projects: PhotoFiltre LX | Xwii | URT-2D | TTM

  4. #4
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,294
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Java: Need advice on String manipulation program

    You'll have to tell me what kinf of input your program expects, ten. just the surname or first name + surname ?
    「明後日の夕方には帰ってるからね。」


  5. #5
    Join Date
    Mar 2008
    Location
    Ireland
    Beans
    781
    Distro
    Ubuntu 15.04 Vivid Vervet

    Re: Java: Need advice on String manipulation program

    The program expects the first name followed by the surname. Like this:

    firstname surname
    The letters are outputed one by one... except the surname gets repeated a second time.. I need to know why this happens.

    It organizes the name in the correct way at the end. The end output is correct.
    Projects: PhotoFiltre LX | Xwii | URT-2D | TTM

  6. #6
    Join Date
    Apr 2005
    Location
    Hampshire, UK
    Beans
    1,274

    Re: Java: Need advice on String manipulation program

    Quote Originally Posted by fallenshadow View Post
    The letters are outputed one by one... except the surname gets repeated a second time.. I need to know why this happens.
    Well ... if I'm understanding you correctly, it's because you print it out twice (There are two "while" loops with

    Code:
    System.out.println(str1.charAt (i) + " " + i)
    in them; comment out the second).


    Quote Originally Posted by fallenshadow View Post
    It organizes the name in the correct way at the end. The end output is correct.
    Is the output for

    Code:
    Jebediah Bootlace
    really supposed to be

    Code:
    Jebediah bootlace J.
    ?
    Last edited by GeneralZod; August 22nd, 2011 at 08:39 PM.

  7. #7
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,160
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Java: Need advice on String manipulation program

    In the real word, this code would be written using a regular expression...

    It is also some code which won't work outside of English-speaking countries (and even then...), and I'm not talking about the futility of the "initial" in ideographic languages, but of the test of the a-z range.

  8. #8
    Join Date
    Mar 2008
    Location
    Ireland
    Beans
    781
    Distro
    Ubuntu 15.04 Vivid Vervet

    Re: Java: Need advice on String manipulation program

    Oh silly me... I was tired when I posted this.

    Please forgive me for posting up such a stupid question.

    Thanks General Zod! Your reply was very helpful!
    Projects: PhotoFiltre LX | Xwii | URT-2D | TTM

Tags for this Thread

Bookmarks

Posting Permissions

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