Correct the following java program:
import java.util.Scanner;
class AWAY
{
String aw;
public AWAY()
{
aw="";
}
void fnInput()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter string.");
aw=sc.nextLine();
}
void fnShow()
{
char ch;
int i;
int len;
len = aw.length();
for(i=0; i<=len-1; i++)
{
ch = aw.charAt(i);
if(ch!=' ' || Character.isLowerCase(ch)==false)
{
System.out.println("Invalid String.");
}
else
{
System.out.println("Original:" + aw);
System.out.println("Modified:" + aw);
}
}
}
boolean isValid()
{
int len = aw.length();
for (int i = 0; i<=len-1; i++)
{
char ch = aw.charAt(i);
if (ch!=' ' || Character.isLowerCase(ch)==false)
return false;
else
return true;
}
}
void fnModify()
{
char ch1;
char ch2;
String modif="";
int len = aw.length();
for(int i = 0; i<=len-1; i++)
{
ch1=aw.charAt(i);
ch2=aw.charAt(i+1);
if(ch1!=ch2)
{
modif=modif+ch1;
}
}
}
public static void main (String args[])
{
AWAY ob = new AWAY();
ob.fnInput();
ob.fnShow();
ob.fnModify();
}
}
Answers
Fixed it.
import java.util.Scanner;
class AWAY
{
String aw;
public AWAY()
{
aw="";
}
void fnInput()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter string.");
aw=sc.nextLine();
}
void fnShow()
{
System.out.println("The sentence entered is: " + aw);
boolean res = isValid();
if(res == true)
System.out.println("Valid.");
else
System.out.println("Invalid.");
}
boolean isValid()
{
int len = aw.length();
boolean v = true;
for(int i = 0; i<=len-2; i++)
{
char ch = aw.charAt(i);
if(Character.isLowerCase(ch)==true || ch == ' ')
v = true;
else
{
v = false;
break;
}
}
return v;
}
void fnModify()
{
String modif="";
aw = aw+" ";
int len = aw.length();
for(int i = 0; i<=len-2; i++)
{
char ch1=aw.charAt(i);
char ch2=aw.charAt(i+1);
if(ch1!=ch2)
{
modif=modif+ch1;
}
}
System.out.println("Modified Sentence: " + modif);
}
public static void main (String args[])
{
AWAY ob = new AWAY();
ob.fnInput();
ob.fnShow();
ob.fnModify();
}
}