Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

while loop reading in a BufferedReader input stream

807601Jun 26 2008 — edited Jun 26 2008
import java.io.*;
Help!! The below program works for the first line of input from a file. But the while loop only executes that once. I have looked up a lot of information and I can't seem to find where the issue is with the loop. Also, does the file have to be formatted any specific way for it to be read in? I read in using readLine() so I just have each string that needs to be read in on a seperate line in a text file on notepad. The first string reads in and goes through the program correctly. Any help is greatly appreciated!



public class ExpConversion
{

char[] expArray = new char[100];
int length = 0;
String infix;
String postfix;
String prefix;
char symb;
int prec;

public void FileReader()
{
//BufferedReader inputStream = null;
//PrintWriter outputStream = null;
try
{
String inputFileName = "ExprInput.txt";
String outputFileName = "ExprOutput.txt";
FileReader inputFileReader = new FileReader( inputFileName );
FileWriter outputFileWriter = new FileWriter( outputFileName );
BufferedReader inputStream = new BufferedReader( inputFileReader );
PrintWriter outputStream = new PrintWriter( outputFileWriter, true );
String inLine;

while ((inLine = inputStream.readLine()) != null)
{
outputStream.println("Expression Input:" );
outputStream.println(inLine);
outputStream.println();
length = inLine.length();

expArray = inLine.toCharArray();
symb = expArray[0];
if( isOperator( symb) )
{

outputStream.println("Prefix to Infix Expression:") ;
prefixToInfix();
outputStream.println(infix) ;
outputStream.println();
outputStream.println("Postfix Expression:") ;
infixToPostfix();
outputStream.println(postfix);
outputStream.println();
}
else
symb = expArray[length - 1];
if( isOperator(symb))
{
postfixToInfix();
outputStream.println("Infix Expression:") ;
outputStream.println(postfixToInfix());
outputStream.println();
infixToPrefix();
outputStream.println("Infix Expression:") ;
outputStream.println(prefixToInfix());
outputStream.println();
}
else
infixToPrefix();
outputStream.println("Infix Expression:") ;
outputStream.println(prefixToInfix());
outputStream.println();
infixToPostfix();
outputStream.println("Postfix Expression:") ;
outputStream.println(infixToPostfix());
outputStream.println();

} // End while loop

outputStream.close();
inputStream.close();

} // End try block

catch( IOException e )
{
System.out.println("IOException");
e.printStackTrace();
} // End catch block


} // End method fileReader

public boolean isOperator( char symb )
{
return (( symb == '+' || symb == '-' || symb == '*' || symb == '/'
|| symb == '$'));
} // End method isOperator

public int prec(char symb)
{
switch ( symb )
{
case '$':
return 3;
//break;
case '*':
case '/':
return 2;
//break;
case '+':
case '-':
return 1;
//break;
default:
return 0;
//break;
}
}

public String infixToPostfix()
{
Stack operators = new Stack();
String letter;
postfix = "";


for( int i = 0; i < expArray.length; i++ )
{
symb = expArray;

if ( Character.isLetter(symb))
{
letter = Character.toString(symb);
postfix = postfix + letter;
}
else if ( symb == '(')
{
//Character operator = new Character('(');
operators.push(symb);
}
else if ( symb == ')' )
{
while(((Character)operators.peek()).charValue()!='(')
{
postfix = postfix + operators.pop();
} // End while
operators.pop();
} // end if that pops back to left parenthesis
else if ( isOperator(symb))
{
while (!operators.empty() && !(operators.peek()==('(')) &&
prec(symb) <= prec(((Character)operators.peek()).charValue()))
{

postfix = postfix + operators.pop();
}
//Character operator = new Character( symb );
operators.push(symb);

}
else
return "Invalid Expression";

}

while ( !operators.empty())
{
postfix = postfix + operators.pop();
}
length = postfix.length();
expArray = postfix.toCharArray(); // Store infix into Array to pass to next method
return postfix;
}

public String infixToPrefix()
{

Stack operands = new Stack();
prefix = "";


for( int i = length - 1; i >= -1; i-- )
{
symb = expArray[i];

if ( Character.isLetter(symb))
prefix = prefix + symb;
else if ( symb == ')')
{
Character operator = new Character('(');
operands.push(operator);
}
else if ( symb == '(' )
{
while(((Character)operands.peek()).charValue()!=')')
{
prefix = prefix + operands.pop();
} // End while
operands.pop();
} // end if that pops back to right parenthesis
else if ( isOperator(symb))
{
if (operands.empty())
operands.push(symb);

while (!operands.empty() && !(operands.peek()==(')')) &&
prec(symb) <= prec(((Character)operands.peek())))
{

prefix = prefix + operands.pop();
}

//Character operator = new Character( symb );
operands.push(symb);


}
else
return "Invalid Expression";
System.exit(1);
}

return (reverseIt(prefix));

}

public static String reverseIt(String prefix)
{
int i, length = prefix.length(); // declare a local variable length because without () it may be different then the global length
StringBuffer prefixbuf = new StringBuffer(length);

for (i = (length - 1); i >= 0; i--)
prefixbuf.append(prefix.charAt(i));
return prefixbuf.toString();
}

public String postfixToInfix()
{

String letter;
String A;
String B;
StringStack operands = new StringStack();


for( int i = 0; i < length; i++ )
{
symb = expArray[i];

if ( Character.isLetter(symb))
{
letter = Character.toString(symb);
operands.push(letter);
}
else if ( isOperator(symb))
{
A = operands.pop();
B = operands.pop();

infix = "(" + B + symb + A + ")";
operands.push(infix);
}
else
return "Invalid Expression";
System.exit(1);
}

infix = operands.pop();
if ( !operands.empty())
{
return "Invalid Expression";

}

expArray = infix.toCharArray(); // Store infix into Array to pass to next method
return (infix);


}

public String prefixToInfix()
{
String letter;
String A;
String B;
StringStack operands = new StringStack();


for( int i = length - 1; i > -1; i-- )
{
symb = expArray[i];

if ( Character.isLetter(symb))
{
letter = Character.toString(symb);
operands.push(letter);
}
else if ( isOperator(symb))
{
A = operands.pop();
B = operands.pop();

infix = "(" + A + symb + B + ")";
operands.push(infix);
}
else
return "Invalid Expression";

}

infix = operands.pop();
if ( !operands.empty())
{
return "Invalid Expression";

}

length = infix.length();
expArray = infix.toCharArray(); // Store infix into Array to pass to next method
return infix;


}

public static void main(String[] args)
{
ExpConversion Expression = new ExpConversion();
Expression.FileReader();
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 24 2008
Added on Jun 26 2008
4 comments
1,598 views