CSC212 Class demo with Eclipse
--D. Thiebaut (talk) 14:50, 7 October 2014 (EDT)
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;
public class Demo1 {
/**
* @param args
*/
public static void main(String[] args) {
ArrayList lines = readInput();
//System.out.println( lines );
Stack stack = new Stack();
ArrayList newLines = process(lines, stack);
//System.out.println( "stack = " + stack );
//System.out.println( "newLines = " + newLines );
replacePrintout( newLines, stack );
}
private static void replacePrintout(ArrayList newLines, Stack stack) {
for ( int i=0; i< newLines.size(); i++ ) {
String line = (String) newLines.get(i);
Scanner scan = new Scanner( line );
while ( scan.hasNext() ) {
String word = scan.next();
if ( word.equals( "#" ) )
System.out.print( stack.pop()+ " " );
else
System.out.print( word + " " );
}
System.out.println();
}
}
private static ArrayList process(ArrayList lines, Stack stack) {
ArrayList newLines = new ArrayList();
for (int i = 0; i < lines.size(); i++) {
String line = (String) lines.get(i);
Scanner scan = new Scanner(line);
String newLine = "";
while (scan.hasNext()) {
String word = scan.next();
if (isNumber(word)) {
int num = Integer.parseInt(word);
stack.push(num);
word = "#";
}
newLine = newLine + " " + word;
}
newLines.add( newLine );
}
return newLines;
}
private static boolean isNumber(String word) {
try {
Integer.parseInt(word);
return true;
} catch (NumberFormatException e) {
return false;
}
}
private static ArrayList readInput() {
ArrayList list = new ArrayList();
list.add("alpha 5 6");
list.add("beta 10 gamma");
list.add("delta 50");
return list;
}
}