Java Programming Ch 8 ex 5 a Write an application that accep
Java Programming: Ch 8 ex 5
a. Write an application that accepts up to 10 Strings, or fewer if the user enters
 a terminating value. Divide the entered Strings into two lists—one for short
 Strings that are 10 characters or fewer and the other for long Strings. After
 data entry is complete, prompt the user to enter which type of String to display,
 and then output the correct list. For this exercise, you can assume that if the
 user does not request the list of short strings, the user wants the list of long
 strings. If there are no Strings in a requested list, output an appropriate
 message. Prompt the user continuously until a sentinel value is entered. Save
 the file as CategorizeStrings.java.
 b. Modify the CategorizeStrings application to divide the entered Strings into
 those that contain no spaces, one space, or more. After data entry is complete,
 continuously prompt the user to enter the type of String to display. If the user
 does not enter one of the three valid choices, display all of the Strings. Save the
 file as CategorizeStrings2.java.
Solution
1 . )
import java.util.Scanner; //scanner class to read data
 public class CategorizeStrings               //create class Sequence
 {
     public static void main(String []args)           //define main method
      {
    Scanner sc=new Scanner(System.in);
    String myStrings[]=new String[10];               //Array Declaration
    String shortlist[]=new String[10];               //Array Declaration
    String longlist[]=new String[10];               //Array Declaration
   
    int i=0,sl=0,ll=0;
    while(i<10)                                       //loop for prompting & reading 10 strings
    {
        System.out.println(\"1 - Enter String \\t 2- Exit \ \");
        int choice =sc.nextInt();                   //read choice 1 for enter string and 2 for exit
        if(choice==1)
        {
        System.out.println(\"Enter String\" +(i+1));
        myStrings[i]=sc.next();                       //read string
            if(myStrings[i].length()<=10)           //if string length is lessthan 10 it stored in shortlist otherwise it is in longlist  
            {
                shortlist[sl]=myStrings[i];
                sl++;
            }
            else
            {
                longlist[ll]=myStrings[i];
                ll++;
            }
        //System.out.println(myStrings[i]);
        }
        else
        {
            break;
        }
        i++;
           
    }
   
        //loop to dispaly output 1-dispalys shot list and 2- for long list ,3- for exit
        while(true)
        {
            System.out.println(\"1 - List Out Short Strings \\t 2 - List Out Long Strings \\t 3- Exit\" );
            int val=sc.nextInt();   //read choice value
            //if else ladder for display short or long list based on choice
            if(val==1)
            {
                for(int x=0;x<sl;x++)
                    System.out.println(shortlist[x]);       //display shortlist
           }
            else if(val==2)
            {
                for(int x=0;x<ll;x++)
                    System.out.println(longlist[x]);       //dispaly longlist
           }
            else if (val==3)
            {
                break;
            }
            else
            {
           }
        }
           
      }
 }
 Output :
1 - Enter String 2- Exit
1
 Enter String0
 adsfsdfsdgfgfdgfdgfd
 
 1 - Enter String         2- Exit
1
 Enter String1
 dasd
 
 1 - Enter String         2- Exit
1
 Enter String2
 sdfdsfds
 
 1 - Enter String         2- Exit
1
 Enter String3
 sdfsdddddddddddddddddddddddddddddddddd
 
 1 - Enter String         2- Exit
1
 Enter String4
 dsf
 
 1 - List Out Short Strings      2 - List Out Long Strings      3- Exit
 1
 dasd
 sdfdsfds
 dsf
 1 - List Out Short Strings      2 - List Out Long Strings      3- Exit
 2
 adsfsdfsdgfgfdgfdgfd
 sdfsdddddddddddddddddddddddddddddddddd
 1 - List Out Short Strings      2 - List Out Long Strings      3- Exit
 3
2 )
//Java Programme CategorizeStrings2.java.
import java.util.Scanner; //scanner class to read data
 public class CategorizeStrings2               //create class Sequence
 {
   
    //Method to return number of white spaces
    static int spacecount(String s)
     {
        int c=0;
         for(int i=0;i<s.length();i++)
         {
             char ch=s.charAt(i);
             if(ch==\' \')
             c++;
         }
         return c;
     }
     public static void main(String []args)           //define main method
      {
        CategorizeStrings2 ob=new CategorizeStrings2();
    Scanner sc=new Scanner(System.in);
    String myStrings[]=new String[10];               //Array Declaration
    String zerolist[]=new String[10];               //Array Declaration
    String onelist[]=new String[10];               //Array Declaration
    String morelist[]=new String[10];               //Array Declaration
   
    int i=0,zl=0,ol=0,ml=0;
    while(i<10)                                       //loop for prompting & reading 10 strings
    {
        System.out.println(\"1 - Enter String \\t 2- Exit \ \");
        int choice =sc.nextInt();                   //read choice 1 for enter string and 2 for exit
        if(choice==1)
        {
        System.out.println(\"Enter String\" +i);
        myStrings[i]=sc.next();                       //read string
        int res=ob.spacecount(myStrings[i]);
            if(res==0)           //if string length is lessthan 10 it stored in shortlist otherwise it is in longlist  
            {
                zerolist[zl]=myStrings[i];
                zl++;
            }
            else if(res==1)
            {
                onelist[ol]=myStrings[i];
                ol++;
            }
            else
            {
                morelist[ml]=myStrings[i];
                ml++;
            }
        //System.out.println(myStrings[i]);
        }
        else
        {
            break;
        }
        i++;
           
    }
   
        //loop to dispaly output 1-dispalys shot list and 2- for long list ,3- for exit
        while(true)
        {
            System.out.println(\"1 - List Out Zero Space Strings \\t 2 - List Out One space Strings \\t 3 - List Out More spaces Strings\\t 4- Exit\" );
            int val=sc.nextInt();   //read choice value
            //if else ladder for display short or long list based on choice
            if(val==1)
            {
                for(int x=0;x<zl;x++)
                    System.out.println(zerolist[x]);       //display zerolist
           }
            else if(val==2)
            {
                for(int x=0;x<ol;x++)
                    System.out.println(onelist[x]);       //dispaly onelist
           }
            else if (val==3)
            {
                for(int x=0;x<ml;x++)
                    System.out.println(morelist[x]);       //dispaly morelist
            }
            else if(val==4)
            {
                break;
            }
            else
            {}
        }
           
      }
 }




