0
import java.io.*;
import java.util.*;
class Mystring
{
String str;
Mystring()
{
str="";
}
void stringread()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("Enter any string");
str=br.readLine();
}catch(Exception e){}
}
void word()
{
String s1;
int temp=0,x,i,j;
StringTokenizer st=new StringTokenizer(str);
x=st.countTokens();
String a[]=new String[x];
x=0;
while(st.hasMoreTokens())
{
s1=st.nextToken();
a[x]=s1;
x++;
}
for(i=0;i<x;i++)
{
for(j=i+1;j<x;j++)
{
if(a[i].compareTo(a[j]>0))
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
void print()
{
for(i=0;i<x;i++)
System.out.println(a[i]);
}
public class Myclass
{
public static void main(String args[])throwsInputStreamReader
{
Mystring ob=new Mystring();
ob.stringread();
ob.word();
ob.print();
}
}
}
2 answers
2
Most of your errors are basic syntactical mistakes like incompatible variables, brackets missing etc. Work on that.
import java.io.*;
import java.util.*;
class Mystring
{
String str;
Mystring()
{
str="";
}
void stringread()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("Enter any string");
str=br.readLine();
}catch(Exception e){}
}
void word()
{
String s1;
int x,i,j;
StringTokenizer st=new StringTokenizer(str);
x=st.countTokens();
String a[]=new String[x];
String temp;
x=0;
while(st.hasMoreTokens())
{
s1=st.nextToken();
a[x]=s1;
x++;
}
for(i=0;i<x;i++)
{
for(j=i+1;j<x;j++)
{
if(a[i].compareTo(a[j]) > 0)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
str="";
for(i=0;i<x;i++)
str+=a[i]+" ";
}
void print()
{
int i;
System.out.println("Modified string: \n"+str);
}
}
public class Myclass
{
public static void main(String args[])
{
Mystring ob=new Mystring();
ob.stringread();
ob.word();
ob.print();
}
}
0
Log in to your account to answer this questions.