String Manipulation & Console Looping

  • tj
  • Newbie
  • Newbie
  • tj
  • Posts: 6

Post 3+ Months Ago

Hi,

A string variable s1, takes in value from the user through the console.
It should only contain alphabetic characters. All other characters such as spaces, punctuations, numbers, symbols, etc are considered invalid.
But these characters though accepted should be ignored.
In the end, the new string should contain only 26 alphabets, with no repetition of any character.

I managed to do a bit of the above part as below:
Code: [ Select ]

String s1,s2;
// s1 is input from the user's side
s1=s1.toUpperCase();
StringBuffer sb1 = new StringBuffer(s1);
StringBuffer sb2 = new StringBuffer();

for (int i=0; i<s1.length(); i++)
{
if(sb1.charAt(i)>='A' && sb1.charAt(i)<='Z') // invalids are ignored
sb2.append(sb1.charAt(i));
}

s2=sb2.toString();

if (s2.length()==25)
// perfect with the count of characters!, what about repetition??
// but if its less than 25, how would I get the program ask to renter
// the string, it should continue until the user's string is converted to
// s2 which will hold a perfect 26 character string of only CAPITAL
// Alphabtes

else
System.out.println("The links variable should have 26 characters of the alphabet set");
  1. String s1,s2;
  2. // s1 is input from the user's side
  3. s1=s1.toUpperCase();
  4. StringBuffer sb1 = new StringBuffer(s1);
  5. StringBuffer sb2 = new StringBuffer();
  6. for (int i=0; i<s1.length(); i++)
  7. {
  8. if(sb1.charAt(i)>='A' && sb1.charAt(i)<='Z') // invalids are ignored
  9. sb2.append(sb1.charAt(i));
  10. }
  11. s2=sb2.toString();
  12. if (s2.length()==25)
  13. // perfect with the count of characters!, what about repetition??
  14. // but if its less than 25, how would I get the program ask to renter
  15. // the string, it should continue until the user's string is converted to
  16. // s2 which will hold a perfect 26 character string of only CAPITAL
  17. // Alphabtes
  18. else
  19. System.out.println("The links variable should have 26 characters of the alphabet set");

This part of the code is in a constructor StrManip(s1) of the class StrManip

The console operation is in the main() of a driver class TestStrManip.
Its from here, where s1 is passed to the StrManip().

Please Help!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

Ok, the only way I can work with something like this is to make a little class of my own, so you may want to pick and choose what you want from this class and I don't recommend you use this method of keyboard input as I just threw it together because I couldn't find my old console input class. Substitue whatever method you have been using already.

I used Strings instead of StringBuffers because String has an indexOf method that will return -1 if a char isn't in the string. I passed a String to the constructor and then called a validate method to check to see if it passed the test.

Besides adding the indexOf method I didn't need to change your test - except you should be testing for ==26 not 25 (since strings and arrays are indexed to 0 the last element will have an index of 25, but the string will contain 26 elements 0-25).

Each time the validate method returns false the user will be prompted to enter more characters, but the destination string will hold any chars that have already passed the test, so if they entered 25 correct chars the first time the next string they enter will only need to have the one missing char and the validate method should filter out the rest.

Keep in mind that I threw this together rather quickly so I can't guarantee that it doesn't have a problem, but it seems to be working ok for me, and the basic concepts are probably ones that you will want to modify for your class anyway, so just test it thoroughly.

Code: [ Select ]
public class alpha
{
  String s2 = "", s1;
  
  public alpha(String inputString)
  {
   s1 = inputString;
   
   while(!validate())
   {
     s1 = getStringFromKeyboard("The links variable should have 26 characters of the alphabet set");
   }
   System.out.println("valid: " + s2);
  }
  
  private boolean validate()
  {
   s1=s1.toUpperCase();
   
   for (int i=0; i<s1.length(); i++)
   {
     char c = s1.charAt(i);
     
     if(c >= 'A' && c <= 'Z' && s2.indexOf(c) == -1)
     {
      s2 += s1.charAt(i);
     }
   }
   
   if (s2.length()==26)
   {
     return true;
   }
   
   return false;
  }
  
  private String getStringFromKeyboard(String prompt)
  {
   String retVal=null;
   System.out.println(prompt);
   java.io.BufferedReader br;
   br = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
   try
   {
     retVal = br.readLine();
   }
   catch(java.io.IOException e){System.out.println("oops");}
   
   return retVal;
  }

  public static void main(String[] args)
  {
   new alpha("abcdefghijklmnopqrstuvwxyz");
  }
}
     
  1. public class alpha
  2. {
  3.   String s2 = "", s1;
  4.   
  5.   public alpha(String inputString)
  6.   {
  7.    s1 = inputString;
  8.    
  9.    while(!validate())
  10.    {
  11.      s1 = getStringFromKeyboard("The links variable should have 26 characters of the alphabet set");
  12.    }
  13.    System.out.println("valid: " + s2);
  14.   }
  15.   
  16.   private boolean validate()
  17.   {
  18.    s1=s1.toUpperCase();
  19.    
  20.    for (int i=0; i<s1.length(); i++)
  21.    {
  22.      char c = s1.charAt(i);
  23.      
  24.      if(c >= 'A' && c <= 'Z' && s2.indexOf(c) == -1)
  25.      {
  26.       s2 += s1.charAt(i);
  27.      }
  28.    }
  29.    
  30.    if (s2.length()==26)
  31.    {
  32.      return true;
  33.    }
  34.    
  35.    return false;
  36.   }
  37.   
  38.   private String getStringFromKeyboard(String prompt)
  39.   {
  40.    String retVal=null;
  41.    System.out.println(prompt);
  42.    java.io.BufferedReader br;
  43.    br = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
  44.    try
  45.    {
  46.      retVal = br.readLine();
  47.    }
  48.    catch(java.io.IOException e){System.out.println("oops");}
  49.    
  50.    return retVal;
  51.   }
  52.   public static void main(String[] args)
  53.   {
  54.    new alpha("abcdefghijklmnopqrstuvwxyz");
  55.   }
  56. }
  57.      

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 80 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.