I\'ve tried some ways to detect EOF in my code, but it still not working. I\'ve tried using BufferedReader, Scanner, and using char u001a to flag the EOF, but still not make
You can use try and catch.
Scanner n=new Scanner(System.in);
String input;
int counter=0;
input=n.nextLine();
try{
while(input!=null)
{
char[] charInput=input.toCharArray();
for (int i = 0; i < input.length(); i++) {
if(charInput[i]=='"')
{
if(counter%2==0)
{
System.out.print("``");
}
else
{
System.out.print("''");
}
counter++;
}
else
{
System.out.print(charInput[i]);
}
}
System.out.print("\n");
input=n.nextLine();
}
}catch(Exception e){
}
Here, when you will give Ctrl+z, Scanner.nextLine()
will give you NoSuchElementException
. Use this exception as a condition of EOF. To handle this exception use try and catch.