How do I fix the code in Java. Error FinalExamAnswers.java:3: error: class FinalExamAnswer is public, should be declared in a file named FinalExamAnswer.java
How do I fix the code in Java.
Error
FinalExamAnswers.java:3: error: class FinalExamAnswer is public, should be declared in a file named FinalExamAnswer.java
public class FinalExamAnswer{
^
Code:
import java.util.Scanner;
public class FinalExamAnswer{
public static void main(String [] args)
{
manipulateString(); //calls function
}
//your code here
public static void manipulateString()
{
Scanner sc=new Scanner(System.in); //create Scanner instance
System.out.print("Enter a sentence: ");
String sentence=sc.nextLine(); //input a sentence
String[] words=sentence.split(" "); //split the sentence at space and store it in array
for(int i=0;i<words.length;i++) //i from 0 to last index
{
if(i%2==0) //if even index
words[i]=words[i].toUpperCase(); //converted to upper case
else //if odd index
words[i]=words[i].toLowerCase(); //converted to lower case
}
for(int i=words.length-1;i>=0;i--) //i from last index to 0
{
System.out.print(words[i]+" "); //print word at index i and space
}
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps