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

Ans: a

package CategorizeStrings;
import java.lang.*;
import java.util.Scanner;

public class First {
  
   public static void main(String args[]){
       int t;
       String[] StringArray = new String[10];
       String[] ShortStringArray = new String[10];
       String[] LongStringArray = new String[10];
       Scanner scan = new Scanner(System.in);
       System.out.println(\"Enter string for Input in Array\");
       for(int i=0;i<StringArray.length;i++)
       {
          
           StringArray[i] = scan.next();
          
       }
       System.out.println(\"Entered string Array\");//Print entered array
       for(int i=0;i<StringArray.length;i++)
       {
           System.out.println(StringArray[i]);
          
       }
       for(int i=0;i<StringArray.length;i++)
       {
           if(StringArray[i].length()<10)
           {
               ShortStringArray[i]=StringArray[i];
           }
           else
           {
               LongStringArray[i]=   StringArray[i];
           }
       }
      
       do
       {
   System.out.println(\"Enter your choice : 1. Long String list , 2 .for short string List\");
   t = scan.nextInt();
       if(t==1){
           for(int i=0;i<LongStringArray.length;i++)
           {
               if(LongStringArray[i]!=null)
               {
                   System.out.println(LongStringArray[i]);
               }
           }
          
       }
       else if(t==2)
       {
           for(int i=0;i<ShortStringArray.length;i++)
           {
               if(ShortStringArray[i]!=null)
               {
                   System.out.println(ShortStringArray[i]);
               }
           }
       }
       else{
           System.out.println(\"your choices is not correct..Please try again\");
       }
       }while(t!=1 || t!=2);
       }
      
}

Ans:b

package CategorizeStrings2;
import java.lang.*;
import java.lang.Character;
import java.util.Scanner;

public class First {
  
   public static void main(String args[]){
       int t;
       int count=0;
       String[] StringArray = new String[10];
       String[] NoSpaceStringArray = new String[10];
       String[] OneSpaceStringArray = new String[10];
       String[] MoreSpaceStringArray = new String[10];
       Scanner scan = new Scanner(System.in);
       System.out.println(\"Enter string for Input in Array\");
       for(int i=0;i<StringArray.length;i++)
       {
          
           StringArray[i] = scan.nextLine();
          
       }
       for(int i=0;i<StringArray.length;i++)
       {
           String[] words = StringArray[i].split(\"\\\\s+\");
           count = words.length;
           count--;
           if(count==0){
              
               NoSpaceStringArray[i]= StringArray[i];
           }
           else if(count==1){
               OneSpaceStringArray[i]= StringArray[i];
              
           }
           else
           {
              
               MoreSpaceStringArray[i]= StringArray[i];
           }
       }
      
      
   System.out.println(\"Enter your choice : 1.No space String list , 2 One Space String List, 3. More space string List\");
   t = scan.nextInt();
       if(t==1){
           for(int i=0;i<NoSpaceStringArray.length;i++)
           {
               if(NoSpaceStringArray[i]!=null)
               {
                   System.out.println(NoSpaceStringArray[i]);
               }
           }
          
       }
       else if(t==2)
       {
           for(int i=0;i<OneSpaceStringArray.length;i++)
           {
               if(OneSpaceStringArray[i]!=null)
               {
                   System.out.println(OneSpaceStringArray[i]);
               }
           }
       }
       else if(t==3){
           for(int i=0;i<MoreSpaceStringArray.length;i++)
           {
               if(MoreSpaceStringArray[i]!=null)
               {
                   System.out.println(MoreSpaceStringArray[i]);
               }
           }
       }
       else
       {
           System.out.println(\"You have wrong choices : Here is complete list\");
      
           for(int i=0;i<StringArray.length;i++)
           {
               System.out.println(StringArray[i]);
              
           }
          
       }
       }
      
       }

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 i
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 i
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 i

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site