i'm trying to change the last character per word
in the input sentence by the word java..
this program below works...
but there is some major problems with it
//addjava2.java
import javax.swing.JOptionPane;
public class addjava2
{
public static void main(String args[])
{
String sentence,Nsuf="java",tempword=" ",suf="",newsentence=" ",pass="";
int wordLn=0,index=0,nofchar=0;
sentence=JOptionPane.showInputDialog("Enter a sentence:");
nofchar=sentence.length();
JOptionPane.showMessageDialog(null,"YOU ENTERED:\n"+sentence);
while(index<nofchar)
{ while(index<nofchar && sentence.charAt(index)==' ')
{
tempword=" ";
index++;
}
while(index<nofchar && sentence.charAt(index)!=' ')
{
tempword+=sentence.charAt(index);
index++;
}
{
wordLn=tempword.length();
suf=tempword.substring(wordLn-1,wordLn);
pass=tempword.replace(suf,Nsuf);
newsentence+=pass;
}
}
JOptionPane.showMessageDialog(null,"RESULT:\n"+newsentence);
System.exit(0);
}}
for example i enter a sentence of :"Enter JaVa"
my assume output should be : "Entejava JaVjava"
but the OUPUT is:
"Entejava JjavaVjava"
i want to use a replacelLast method so i can only replace the last occurence of letter but the compiler said it doesn't recognize the method..
please help me please..
in the input sentence by the word java..
this program below works...
but there is some major problems with it
//addjava2.java
import javax.swing.JOptionPane;
public class addjava2
{
public static void main(String args[])
{
String sentence,Nsuf="java",tempword=" ",suf="",newsentence=" ",pass="";
int wordLn=0,index=0,nofchar=0;
sentence=JOptionPane.showInputDialog("Enter a sentence:");
nofchar=sentence.length();
JOptionPane.showMessageDialog(null,"YOU ENTERED:\n"+sentence);
while(index<nofchar)
{ while(index<nofchar && sentence.charAt(index)==' ')
{
tempword=" ";
index++;
}
while(index<nofchar && sentence.charAt(index)!=' ')
{
tempword+=sentence.charAt(index);
index++;
}
{
wordLn=tempword.length();
suf=tempword.substring(wordLn-1,wordLn);
pass=tempword.replace(suf,Nsuf);
newsentence+=pass;
}
}
JOptionPane.showMessageDialog(null,"RESULT:\n"+newsentence);
System.exit(0);
}}
for example i enter a sentence of :"Enter JaVa"
my assume output should be : "Entejava JaVjava"
but the OUPUT is:
"Entejava JjavaVjava"
i want to use a replacelLast method so i can only replace the last occurence of letter but the compiler said it doesn't recognize the method..
please help me please..