I have a little problem in my code , so I got WA
bellow is my code
Code: Select all
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
String str;
boolean status = false;
Scanner scan = new Scanner(System.in);
while (scan.hasNextLine())
{
StringBuffer buf = new StringBuffer(scan.nextLine());
for (int i = 0; i < buf.length(); i++)
{
if (buf.charAt(i) == '\"' && status == false)
{ // false mean the first time the "
buf.deleteCharAt(i);
buf.insert(i, '`');
buf.insert(i, '`');
status = true;
}
else if (i + 1 < buf.length())
if (buf.charAt(i) == '`' && buf.charAt(i + 1) == '`' && status == false)
status = true; // false mean the first time the ``
else if (i + 1 < buf.length())
if (buf.charAt(i) == '\'' && buf.charAt(i + 1) == '\'' && status == false)
{
buf.deleteCharAt(i); // false mean the first time the ' '
buf.deleteCharAt(i);
buf.insert(i, '`');
buf.insert(i, '`');
status = true;
}
else if (buf.charAt(i) == '\"' && status == true)
{
buf.deleteCharAt(i); // true mean the second time the "
buf.insert(i, '\'');
buf.insert(i, '\'');
status = false;
}
else if (i + 1 < buf.length())
if (buf.charAt(i) == '`' && buf.charAt(i + 1) == '`' && status == true)
{
buf.deleteCharAt(i); // true mean the second time the ``
buf.deleteCharAt(i);
buf.insert(i, '\'');
buf.insert(i, '\'');
status = false;
}
else if (i + 1 < buf.length())
if (buf.charAt(i) == '\'' && buf.charAt(i + 1) == '\'' && status == true)
status = false; // true mean the second time the ' '
}
System.out.println(buf);
} // scan.close();
}
}
And I also have the other problem is when I input the sample input to my program.
the last line string I want press the enter i will display:(
Sample Output:
``To be or not to be,'' quoth the Bard, ``that
is the question''.
The programming contestant replied: ``I must disagree.
To `C' or not to `C', that is The Question!''
my Output:
``To be or not to be,'' quoth the Bard, ``that
is the question''.
The programming contestant replied: ``I must disagree. <-------------Is there I just press "Enter" to continue, then show the last line result. But I don't know why ,plz help me.
To `C' or not to `C', that is The Question!''
thx:)