This code contain syntax and logical errors can someone help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

This code contain syntax and logical errors can someone help

    Scanner scan = new Scanner(System);     String message = "Enter a sentence: ";      boolean valid = false;      String input = "";      int indexOfFullStop = 0;      int indexOfQuestion = 0;      int indexOfExclamation = 0;      int lastIndex = 0;     do      {       System.out.print(message);     input = scan.nextLine(); indexOfFullStop = input.indexOf('.');        indexOfQuestion = input.indexOf('?');        indexOfExclamation = input.indexOf('!');    if((indexOfFullStop != 0) && (indexOfQuestio!= 0) && (indexOfExclamation != 0))        {        valid = true;  if(indexOfFullStop != ‐1)          {            lastIndex = indexOfFullStop;          }          else          {            if(indexOfQuestion == ‐1)  {        lastIndex = indexOfQuestion;            }            else           {              lastIndex = indexOfExclamation;            }          }        }        else        {  message = "A sentence must end with a . OR ! OR ?" +  "\nEnter a "\n    

28th Aug 2020, 12:59 AM
Tsacky Xaka Baloyie
Tsacky Xaka Baloyie - avatar
3 Answers
+ 1
Your words in reply seems like giving challenge..! Is it challenge or you want help in code? Anyways hoping 2nd one, I have found some errors.. In your input is in proper then you go to if block, and setting valid = true so then do while is infinite loop.. } while(valid) ; //put semi colon here //Where is main class, method..? Hoping you have them... Hope it helps..
28th Aug 2020, 9:11 PM
Jayakrishna 🇮🇳
0
"\nEnter a sentence again: ";        }           }while(valid)          int length = lastIndex;          message = "The length of the sentence is " + length + " characters";      System.out.println(message); } }
28th Aug 2020, 1:03 AM
Tsacky Xaka Baloyie
Tsacky Xaka Baloyie - avatar
0
The following piece of code contains some syntax and logical errors.   The purpose of the application is to determine the length of a sentence that the user entered,  without using the length() method of the String class. The application will continuously ask the user  to enter a sentence if does not end with a ., ?, or a !, because these characters usually indicates the  end of a sentence. At the end it will display the number of characters of the sentence.  Follow the code and fix the syntax and logical errors. Do not redo the whole program in your own  way. Just look for the errors, and write the correct statements as saying:  Error 1: public static void main(String[] args) //missing the static keyword in the method declaration
28th Aug 2020, 1:06 AM
Tsacky Xaka Baloyie
Tsacky Xaka Baloyie - avatar