JAVA problem 1 Assume that word is a variable of type String

JAVA problem

1.

Assume that word is a variable of type  String that has been assigned a value . Assume furthermore that this value always contains the letters \"dr\" followed by at least two other letters. For example: \"undramatic\", \"dreck\", \"android\", \"no-drip\".

Assume that there is another variable  declared , drWord, also of type  String . Write the statements needed so that the 4-character substring word of the value of word starting with \"dr\" is assigned to drWord. So, if the value of word were \"George slew the dragon\" your code would assign the value \"drag\" to drWord.

2.

Assume that sentence is a variable of type  String  that has been assigned a value . Assume furthermore that this value is a String consisting of words separated by single space characters with a period at the end. For example: \"This is a possible value of sentence.\"

Assume that there is another variable  declared , firstWord, also of type  String . Write the statements needed so that the first word of the value of sentence is assigned to firstWord. So, if the value of sentence were \"Broccoli is delicious.\" your code would assign the value \"Broccoli\" to firstWord.

Solution

2)solution

package com.prt.test;

import java.util.Arrays;
import java.util.Scanner;

public class StringSeparation {
  
   public static void main(String args[])
  
   {
      
       String firstword=\"\";
  
       Scanner scanner=new Scanner(System.in);
       System.out.println(\"enter the string\");
       String sentence=scanner.nextLine();
       String[] words=sentence.split(\" \");
      
       for (String word:words)
       {
           System.out.println(word);
       }
       System.out.println(\"enter the string to separate first word\");
      
       String sentence1=scanner.nextLine();
      
       String[] firstwords=sentence1.split(\" \");
      
   int n=firstwords.length;
     
   for(int i=0;i<n;i++)
   {
       firstword=firstwords[i];
       break;
   }
   System.out.println(\"the firstword is\"+ firstword);
      
      
   }

}

output

enter the string
this is a possible value of sentence.
this
is
a
possible
value
of
sentence.
enter the string to separate first word
Broccoli is delicious.
the firstword isBroccoli

1)solution

package com.prt.test;

output

package com.prt.test;

import java.util.Scanner;


public class AssignValue {
  
   public static void main(String[] args) {
  
      
       Scanner scanner=new Scanner(System.in);
      
       System.out.println(\"enter the multiple words and any word contains startingwith \'dr\'\");
       String sentence=scanner.nextLine();
  
       boolean matchword=sentence.contains(\"dr\");
       if(matchword==true)
       {
          
           String drword=sentence.substring(sentence.indexOf(\"dr\"), sentence.length());
int len=drword.length();
if(len>0&&len>=4)
           {
   String drword1 = drword.substring(0, Math.min(drword.length(), 4));

       System.out.println(\"the drword is\\t\"+drword1);
      
      
               }
       else{
           System.out.println(\"unable to add atleast two characters after finding dr\");
       }
     
          
   }
       }

          
}

enter the multiple words and any word contains startingwith \'dr\'
george slew the dragon
dragon
the drword is   drag

enter the multiple words and any word contains startingwith \'dr\'
dragon is slewed by george
the drword is   drag

JAVA problem 1. Assume that word is a variable of type String that has been assigned a value . Assume furthermore that this value always contains the letters \
JAVA problem 1. Assume that word is a variable of type String that has been assigned a value . Assume furthermore that this value always contains the letters \
JAVA problem 1. Assume that word is a variable of type String that has been assigned a value . Assume furthermore that this value always contains the letters \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site