| | |
Help with breaking out of a scanner.hasnext
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2008
Posts: 9
Reputation:
Solved Threads: 0
I've got this bug in my progrom that I cannot figure out and it's due within the next couple of hours. I was hoping someone could help.
My problem is in my main function that reads in from standing input
but when the user types in $PART2 it is suppose to break and go onto
the next input. But whenever I type in:
C = 7
D = 6
$PART2
the function will not go on to the next part because it should print
System.out.println("Enter PostFix Expression: ")
If you could help me figure out why I would greatly appreciate it.
My problem is in my main function that reads in from standing input
but when the user types in $PART2 it is suppose to break and go onto
the next input. But whenever I type in:
C = 7
D = 6
$PART2
the function will not go on to the next part because it should print
System.out.println("Enter PostFix Expression: ")
If you could help me figure out why I would greatly appreciate it.
Java Syntax (Toggle Plain Text)
import java.util.*; import java.io.*; import java.util.Scanner; import java.util.Stack; import java.lang.Character; class PostEval2 { static Stack<Integer> postFix = new Stack<Integer>(); // a new stack of integers public static final int integers=26; // the max number of integers for the array static int [] array = new int[integers]; // a new array // int addr(char ch) returns the equivalent interger address // for the letter given in ch, 'A' returns 0, 'Z' returns 25 and all // other letters return their corresponding position as well. public static int addr(char ch) { return (int)ch-(int)'A'; } // void eval( char letter) will evaluate the postfix expression // from what the user entered. If will make sure that letter is a digit // then it will begin doing the equations for whatever operand is used. // it will final push the result into the stack. public static void eval(char letter) { int result = 0; int code1 = 0; if(Character.isDigit(letter)) { int operand1 = postFix.pop(); int operand2 = postFix.pop(); if (letter == '+') result = operand1 + operand2; else if (letter == '-') result = operand1 - operand2; else if (letter == '*') result = operand1 * operand2; else if (letter == '/') result = operand1 / operand2; postFix.push(result); code1=addr(letter); postFix.push(array[code1]); } } // void splitString(String postInput, char letter) will // will convert the inputed postfix expression into individual // characters then evaulate each character. It will error // handle to see if there is more than one item left // in the stack public static void splitString (String postInput, char letter) { System.out.println(postFix+ "="); // the program was ended so print out the equation then figure out the answer int size = postInput.length(); //get the lenght of the input for (int i = 0; i<=size; i++) // brake each character into into indivdual characters { letter = postInput.charAt(i); eval(letter); // evaluate the postfix expression if(postFix.size()>1) // error check to see if the size is greater than 1 { while(postFix.size() > 0) // if it is then there is more than one item in the stack and the problem is not solved System.out.println("The stack contains more than one item"); postFix.pop(); } else if(postFix.size()==1) // but if there only one item in the stack then it is solved. and print it out { System.out.println(postFix.pop()); } } } static public void main(String args[]) { Scanner scanner = new Scanner(System.in); //begin standard input System.out.println("Input Letter and Number it equals: "); // enter letters and numbers String postInput; // the postFix input char letter=0; // the letter entered by the user String equals; // the character to read in the equals sign int number = 0; // the number the letter equals int code1 = 0; // code will get the ASC from the addr function while(scanner.hasNext()) { letter=(scanner.next()).charAt(0); // get the letter from the user input System.out.println("Test1 "); if(letter=='$') {break;} // if a user enters $PART2 begin the postfix read in System.out.println("Test% "); equals = scanner.next(); // the user is suppose to enter a = symbol System.out.println("Test2 "); code1=addr(letter); // get the value of the operand by using the addr function System.out.println("Test3 "); array[code1]=scanner.nextInt(); // we then put the code into the array System.out.println("Test4 "); } while(scanner.hasNext()) // this is part two of the postfix evaluation { System.out.println("Enter PostFix Expression: "); // signifys the postfix part postInput=scanner.nextLine(); // get a string of the postfix expression if(postInput.equals("$END"))break; // if the user enters $END then the program is done splitString(postInput, letter); // insert the string and the letter into the splitstring operation and begin the evaulation } } }
•
•
Join Date: Sep 2008
Posts: 1,613
Reputation:
Solved Threads: 205
So if they type in $PART2, you WANT it to print error postfix expression?
edit: It probably isn't working because you didn't prompt the user for input before the second loop. So it says while(scanner.hasNext()), but you never prompted the user (using a println statement), so it never entered that loop. Good luck.
edit: It probably isn't working because you didn't prompt the user for input before the second loop. So it says while(scanner.hasNext()), but you never prompted the user (using a println statement), so it never entered that loop. Good luck.
Last edited by BestJewSinceJC; Apr 14th, 2009 at 11:25 pm.
![]() |
Other Threads in the Java Forum
- Previous Thread: Connect to OLAP cube on a MSsql server
- Next Thread: xml deserilization
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component data database derby design draw eclipse encryption error event exception fractal game givemetehcodez graphics gui html ide if_statement image inheritance input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception open-source oracle print printing problem program programming project property recursion reference ria scanner screen search server set size sms sort sourcelabs splash sql static stop string swing testautomation threads time tree ui unicode validation windows






