Question
This is related to my previous question
I'm solving UVA's Edit Step Ladders and trying to make the online judge comply with my answer.
I have used the method ReadLn() to adapt my textfile-reading program to this:
import java.io.*;
import java.util.*;
class LevenshteinParaElJuez implements Runnable{
static String ReadLn(int maxLength){ // utility function to read from stdin,
// Provided by Programming-challenges, edit for style only
byte line[] = new byte [maxLength];
int length = 0;
int input = -1;
try{
while (length < maxLength){//Read untill maxlength
input = System.in.read();
if ((input < 0) || (input == '\n')) break; //or untill end of line ninput
line [length++] += input;
}
if ((input < 0) && (length == 0)) return null; // eof
return new String(line, 0, length);
}catch (IOException e){
return null;
}
}
public static void main(String args[]) // entry point from OS
{
LevenshteinParaElJuez myWork = new LevenshteinParaElJuez(); // Construct the bootloader
myWork.run(); // execute
}
public void run() {
new myStuff().run();
}
}
class myStuff implements Runnable{
public void run(){
ArrayList<String> theWords = new ArrayList<String>();
try
{
/// PLACE YOUR JAVA CODE HERE
String leido=LevenshteinParaElJuez.ReadLn(100);
//System.out.println("lo leido fue "+leido);
while (!leido.equals(" ")){
theWords.add(leido);
leido=LevenshteinParaElJuez.ReadLn(100);
}
}catch(Exception e){
System.out.println("El programa genero una excepcion");
}
int maxEdit=0;
int actualEdit=0;
int wordsIndex1 =0, wordsIndex2=0;
while (wordsIndex1<= theWords.size())
{
while (wordsIndex2<= theWords.size()-1){
actualEdit=Levenshtein.computeLevenshteinDistance(theWords.get(wordsIndex1),theWords.get(wordsIndex2));
if (actualEdit>maxEdit){maxEdit=actualEdit;}
wordsIndex2++;
}
wordsIndex1++;
}
System.out.println(maxEdit+1);
}
}
class Levenshtein {
private static int minimum(int a, int b, int c) {
if(a<=b && a<=c)
return a;
if(b<=a && b<=c)
return b;
return c;
}
public static int computeLevenshteinDistance(String str1, String str2) {
return computeLevenshteinDistance(str1.toCharArray(),
str2.toCharArray());
}
private static int computeLevenshteinDistance(char [] str1, char [] str2) {
int [][]distance = new int[str1.length+1][str2.length+1];
for(int i=0;i<=str1.length;i++)
distance[i][0]=i;
for(int j=0;j<=str2.length;j++)
distance[0][j]=j;
for(int i=1;i<=str1.length;i++)
for(int j=1;j<=str2.length;j++)
distance[i][j]= minimum(distance[i-1][j]+1,
distance[i][j-1]+1,
distance[i-1][j-1]+
((str1[i-1]==str2[j-1])?0:1));
return distance[str1.length][str2.length];
}
}
I'm supposed to read the whole of the online judge's input as it were written via keyboard, but when I run the above program I can't make it stop reading. It goes something like:
abc
cba
aba
cca
without being able to stop the console reading. How do I get around this?? I suspect the problem is in my loop's conditional:
String leido=LevenshteinParaElJuez.ReadLn(100);
//System.out.println("lo leido fue "+leido);
while (!leido.equals(" ")){
theWords.add(leido);
leido=LevenshteinParaElJuez.ReadLn(100);
}
I have also used:
while (!leido.equals(null)){
theWords.add(leido);
leido=LevenshteinParaElJuez.ReadLn(100);
}
and gotten stuck as well.
Edit: the declaration was actually:
while (leido != null)){
theWords.add(leido);
leido=LevenshteinParaElJuez.ReadLn(100);
}
I don't get why it's failing. I want the reading of input via keyboard to stop when the first blank line is entered.
Edit: thanks to rodion's answer, the ReadLn method is now changed to:
if ((input < 0) || (length == 0)) return null; // eof
instead of:
if ((input < 0) && (length == 0)) return null; // eof
Now, it's reading two blank space before producing the integer output. How can I change it just to read one?
OTHER TIPS
I am not really sure if I understand your problem correctly. If you merely want your program to stop reading from the console, you can "close" the console by hitting Ctrl+D (Linux) or Ctrl+Z (Windows). This causes System.in.read() to return -1, so that ReadLn method would return null.
Check for null as Nathaniel suggested.
Edit: (based on your comment)
The problem is with this condition in the ReadLn method
if ((input < 0) && (length == 0)) return null;
If you enter a blank line, length will be 0, input will be > 0 though (whatever your system uses to denote a newline). If you change it to
if ((input < 0) || (length == 0)) return null;
the method will return null if either the input stream is closed or a blank line is entered.strong text
Instead of !leido.equals(" ")
, simply use !leido.equals("")
. The former terminates when a line containing only a space is entered, the latter when a blank line is entered.
I dont understand your problem much, but just a suggest from my side will be that use your condition in this way
while(!" ".equals(leido)){
//instead of
while (!leido.equals(" ")){
why ?, because your code will throw a Exception if your variable contains null, and the condition which i mentioned will never do that :P