Help with breaking out of a scanner.hasnext

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 9
Reputation: yingfo is on a distinguished road 
Solved Threads: 0
yingfo yingfo is offline Offline
Newbie Poster

Help with breaking out of a scanner.hasnext

 
0
  #1
Apr 14th, 2009
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.


  1. import java.util.*;
  2. import java.io.*;
  3. import java.util.Scanner;
  4. import java.util.Stack;
  5. import java.lang.Character;
  6.  
  7.  
  8. class PostEval2
  9. {
  10.  
  11. static Stack<Integer> postFix = new Stack<Integer>(); // a new stack of integers
  12. public static final int integers=26; // the max number of integers for the array
  13. static int [] array = new int[integers]; // a new array
  14.  
  15.  
  16.  
  17.  
  18.  
  19. // int addr(char ch) returns the equivalent interger address
  20. // for the letter given in ch, 'A' returns 0, 'Z' returns 25 and all
  21. // other letters return their corresponding position as well.
  22.  
  23. public static int addr(char ch)
  24. {
  25. return (int)ch-(int)'A';
  26. }
  27.  
  28.  
  29. // void eval( char letter) will evaluate the postfix expression
  30. // from what the user entered. If will make sure that letter is a digit
  31. // then it will begin doing the equations for whatever operand is used.
  32. // it will final push the result into the stack.
  33. public static void eval(char letter)
  34. {
  35. int result = 0;
  36. int code1 = 0;
  37. if(Character.isDigit(letter))
  38. {
  39. int operand1 = postFix.pop();
  40. int operand2 = postFix.pop();
  41. if (letter == '+') result = operand1 + operand2;
  42. else if (letter == '-') result = operand1 - operand2;
  43. else if (letter == '*') result = operand1 * operand2;
  44. else if (letter == '/') result = operand1 / operand2;
  45. postFix.push(result);
  46. code1=addr(letter);
  47. postFix.push(array[code1]);
  48. }
  49. }
  50.  
  51.  
  52. // void splitString(String postInput, char letter) will
  53. // will convert the inputed postfix expression into individual
  54. // characters then evaulate each character. It will error
  55. // handle to see if there is more than one item left
  56. // in the stack
  57.  
  58. public static void splitString (String postInput, char letter)
  59. {
  60. System.out.println(postFix+ "="); // the program was ended so print out the equation then figure out the answer
  61. int size = postInput.length(); //get the lenght of the input
  62. for (int i = 0; i<=size; i++) // brake each character into into indivdual characters
  63. {
  64. letter = postInput.charAt(i);
  65. eval(letter); // evaluate the postfix expression
  66. if(postFix.size()>1) // error check to see if the size is greater than 1
  67. {
  68. while(postFix.size() > 0) // if it is then there is more than one item in the stack and the problem is not solved
  69. System.out.println("The stack contains more than one item");
  70. postFix.pop();
  71. }
  72. else if(postFix.size()==1) // but if there only one item in the stack then it is solved. and print it out
  73. {
  74. System.out.println(postFix.pop());
  75. }
  76. }
  77. }
  78.  
  79. static public void main(String args[])
  80. {
  81. Scanner scanner = new Scanner(System.in); //begin standard input
  82. System.out.println("Input Letter and Number it equals: "); // enter letters and numbers
  83. String postInput; // the postFix input
  84. char letter=0; // the letter entered by the user
  85. String equals; // the character to read in the equals sign
  86. int number = 0; // the number the letter equals
  87. int code1 = 0; // code will get the ASC from the addr function
  88. while(scanner.hasNext())
  89. {
  90. letter=(scanner.next()).charAt(0); // get the letter from the user input
  91. System.out.println("Test1 ");
  92. if(letter=='$')
  93. {break;}
  94. // if a user enters $PART2 begin the postfix read in
  95. System.out.println("Test% ");
  96. equals = scanner.next(); // the user is suppose to enter a = symbol
  97. System.out.println("Test2 ");
  98. code1=addr(letter); // get the value of the operand by using the addr function
  99. System.out.println("Test3 ");
  100. array[code1]=scanner.nextInt(); // we then put the code into the array
  101. System.out.println("Test4 ");
  102. }
  103.  
  104. while(scanner.hasNext()) // this is part two of the postfix evaluation
  105. {
  106. System.out.println("Enter PostFix Expression: "); // signifys the postfix part
  107. postInput=scanner.nextLine(); // get a string of the postfix expression
  108. if(postInput.equals("$END"))break; // if the user enters $END then the program is done
  109. splitString(postInput, letter); // insert the string and the letter into the splitstring operation and begin the evaulation
  110. }
  111. }
  112.  
  113. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,613
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 205
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Help with breaking out of a scanner.hasnext

 
0
  #2
Apr 14th, 2009
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.
Last edited by BestJewSinceJC; Apr 14th, 2009 at 11:25 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC