If statement doesn't word as planned - java

could you have a look at my code and tell me why it doesn't work as planned
Scanner sc = new Scanner(System.in);
char card1 = ' ';
char card2 = ' ';
int count = 0;
int globalCount = 0;
do {
globalCount++;
card1 = sc.next().charAt(0);
card2 = sc.next().charAt(0);
if ((card1 >= 2 && card1 <= 9 || card1 == 'T' || card1 == 'J' || card1 == 'Q' || card1 == 'K' || card1 == 'A') && (card2 >= 2 && card2 <= 9 || card2 == 'T' || card2 == 'J' || card2 == 'Q' || card2 == 'K' || card2 == 'A')){
if (card1 == 'A' || card1 == 'K' && card2 == 'A' || card2 == 'K') {
count++;
}else {
count = 0;
}
}else {
System.out.println("Invalid cards given!");
}
}while (count < 3);
System.out.println("Number of tries: " + globalCount);
If I input for card1 an number I always get an Invalid cards given even though I think in my if statement I specified that it could take a number as well? Also a little pointer as if I can avoid the repetition of those card1 == 'T' and so on?

Your current condition for card1 and card2
(card1 >= 2 && card1 <= 9 || card1 == 'T' || ...
won't be true for number cards. char type stores the two-byte Unicode value. To check if this value is between character 2 and 9 you have to express the constants in the same way you are comparing symbol cards:
(card1 >= '2' && card1 <= '9' || card1 == 'T' || ...
You can do avoid repeating code by defining all allowed values as String and using String.indexOf(). Your check would be converted to:
"23456789TJQKA".indexOf(char1) >= 0

Try this:
Scanner sc = new Scanner(System.in);
char card1 = ' ';
char card2 = ' ';
int count = 0;
int globalCount = 0;
do {
globalCount++;
card1 = sc.nextLine().charAt(0);
card2 = sc.nextLine().charAt(0);
if (((card1 - '0' >= 2 && card1 - '0' <= 9) || card1 == 'T' || card1 == 'J' || card1 == 'Q' || card1 == 'K' || card1 == 'A') && ((card2 - '0' >= 2 && card2 - '0' <= 9) || card2 == 'T' || card2 == 'J' || card2 == 'Q' || card2 == 'K' || card2 == 'A')){
if (card1 == 'A' || card1 == 'K' && card2 == 'A' || card2 == 'K') {
count++;
}else {
count = 0;
}
}else {
System.out.println("Invalid cards given!");
}
}while (count < 3);
System.out.println("Number of tries: " + globalCount);
This comparison
card1 >= 2
compares the ascii code of the char entered with 2 and not the numeric representation of card1.
So to get this numeric representation you have to subtract the ascii code of '0'.
By subtracting card1-'0' you get the number you entered as opposed to the ascii code you get by only card1, because this what a char is really: an ASCII code from 0 to 255.

Related

How can I keep it running after checking the if condition?

This is a program tic tac toe. It's a computer operation. I want it to check all conditions. If it meets the condition, it will check again if the position of the computer to work. Can add O or not if it can't add O. had the computer check the next condition completely, if it didn't meet the condition it would randomly add O, but now it doesn't work because it happens that sometimes the computer randomly added it in the first place. and then when the players Played the condition that the computer had to prevent the player from winning, but it had a random O In the previous round, it was unable to fill the O at all, thus preventing it from continuing.
private static void computerNormalTurn(char[][] board) {
Random rand = new Random();
int computerMove;
while (true) {
if(board [2][0] == 'X' && board [2][1] == 'X' ||
board [1][1] == 'X' && board [0][0] == 'X' ||
board [1][2] == 'X' && board [0][2] == 'X'){
if(board[2][2] != ' ') {
continue;
}else{
computerMove = 3;
}
} else if (board [2][0] == 'X' && board [2][2] == 'X' ||
board [1][1] == 'X' && board [0][1] == 'X') {
if(board[2][1] != ' ') {
continue;
}else{
computerMove = 2;
}
} else if (board [2][1] == 'X' && board [2][2] == 'X' ||
board [1][0] == 'X' && board [0][0] == 'X' ||
board [1][1] == 'X' && board [0][2] == 'X' ) {
if(board[2][0] != ' ') {
continue;
}else{
computerMove = 1;
}
} else if (board[2][0] == 'X' && board[0][0] == 'X' ||
board [1][1] == 'X' && board [1][2] == 'X' ) {
if(board[1][0] != ' ') {
continue;
}else{
computerMove = 4;
}
} else if (board [2][0] == 'X' && board [0][2] == 'X' ||
board [2][1] == 'X' && board [0][1] == 'X' ||
board [2][2] == 'X' && board [0][0] == 'X' ||
board [1][0] == 'X' && board [1][2] == 'X' ) {
if(board[1][1] != ' ') {
continue;
}else{
computerMove = 5;
}
} else if (board[2][2] == 'X' && board[0][2] == 'X' ||
board [1][0] == 'X' && board [1][1] == 'X') {
if(board[1][2] != ' ') {
continue;
}else{
computerMove = 6;
}
} else if (board[2][0] == 'X' && board[1][0] == 'X' ||
board [2][2] == 'X' && board [1][1] == 'X' ||
board [0][1] == 'X' && board [0][2] == 'X') {
if(board[0][0] != ' ') {
continue;
}else{
computerMove = 7;
}
} else if (board [2][1] == 'X' && board [1][1] == 'X' ||
board [0][0] == 'X' && board [0][2] == 'X' ) {
if(board[0][1] != ' ') {
continue;
}else{
computerMove = 8;
}
} else if (board[2][0] == 'X' && board[1][1] == 'X' ||
board [2][2] == 'X' && board [1][2] == 'X' ||
board [0][0] == 'X' && board [0][1] == 'X' ) {
if(board[0][2] != ' ') {
continue;
}else{
computerMove = 9;
}
}else {
computerMove = rand.nextInt(9) + 1;
}
if (isValidMove(board, Integer.toString(computerMove))) {
break;
}
}
placeMove(board, Integer.toString(computerMove), 'O');
System.out.println("Computer chose " + computerMove);
}
I think the problem is that if your board is empty the computer will still have a move because of the else condition else { computerMove = rand.nextInt(9)+1; }
Then the following if statement will give a valid move and you will break the while loop and place the move on the board.
So if you want to prevent the computer from playing first, you should make a function that checks if the board is empty and if it is then do nothing.
public boolean isEmpty(){
for(int i = 0; i<3; i++)
for(int j = 0; j<3; j++)
if(board[i][j] != ' ') return false;
return true
}
If needed you can pass the board as a parameter in the function.
Hope I helped. Good luck!
There's a problem with the continues. The continue causes the next iteration. Because nothing changed, the game stays in the same state, the same condition will be met and the loop will continue endless.
Rewrite your conditions, so instead of e.g.
if(board [2][0] == 'X' && board [2][1] == 'X' ||
board [1][1] == 'X' && board [0][0] == 'X' ||
board [1][2] == 'X' && board [0][2] == 'X'){
if(board[2][2] != ' ') {
continue;
}else{
computerMove = 3;
}
} else ...
write
if((board [2][0] == 'X' && board [2][1] == 'X' ||
board [1][1] == 'X' && board [0][0] == 'X' ||
board [1][2] == 'X' && board [0][2] == 'X') &&
board [2][2] == ' '){
computerMove = 3;
} else ...

In Java, how do I print something if only 2 out of 3 values are 1, 2 or 3?

This is for an exercise that plays with the idea of cards, so it asks me to print "SIX" if only 2 out of 3 cards are 1, 2 or 3. This is my code now but if I were to input, say, 1 2 and 3 it would still return "SIX" even though there are more than 2 values of 1 2 or 3:
if (card1 == 1 || card1 == 2 || card1 == 3 && card2 == 1 ||card2 == 2 || card2 == 3 && card3 > 3) {
System.out.println("SIX");
}
else if (card1 == 1 || card1 == 2 || card1 == 3 && card3 == 1 ||card3 == 2 || card3 == 3 && card2 > 3) {
System.out.println("SIX");
}
else if (card2 == 1 || card2 == 2 || card2 == 3 && card1 == 1 ||card1 == 2 || card1 == 3 && card3 > 3) {
System.out.println("SIX");
}
else if (card2 == 1 || card2 == 2 || card2 == 3 && card3 == 1 ||card3 == 2 || card3 == 3 && card1 > 3) {
System.out.println("SIX");
}
else if (card3 == 1 || card3 == 2 || card3 == 3 && card2 == 1 ||card2 == 2 || card2 == 3 && card1 > 3) {
System.out.println("SIX");
}
else if (card3 == 1 || card3 == 2 || card3 == 3 && card1 == 1 ||card1 == 2 || card1 == 3 && card2 > 3) {
System.out.println("SIX");
}
Just starting to get into coding so any help would be nice. Thank you very much!
First, your conditions are wrong, because && has higher precedence than ||, which means that the following two statements are the same:
if (card1 == 1 || card1 == 2 || card1 == 3 && card2 == 1 ||card2 == 2 || card2 == 3 && card3 > 3) {
if (card1 == 1 || card1 == 2 || (card1 == 3 && card2 == 1) ||card2 == 2 || (card2 == 3 && card3 > 3)) {
What you meant was:
if ((card1 == 1 || card1 == 2 || card1 == 3) && (card2 == 1 ||card2 == 2 || card2 == 3) && card3 > 3) {
Second, for something like this, it's better to go with a counter, to simplify the code:
int count = 0;
if (card1 == 1 || card1 == 2 || card1 == 3) {
count++;
}
if (card2 == 1 || card2 == 2 || card2 == 3) {
count++;
}
if (card3 == 1 || card3 == 2 || card3 == 3) {
count++;
}
if (count == 2) {
System.out.println("SIX");
}
Using the ? : ternary operator, that can be further simplified:
int count = (card1 == 1 || card1 == 2 || card1 == 3 ? 1 : 0)
+ (card2 == 1 || card2 == 2 || card2 == 3 ? 1 : 0)
+ (card3 == 1 || card3 == 2 || card3 == 3 ? 1 : 0);
if (count == 2) {
System.out.println("SIX");
}
You can let the collections library do the heavy lifting:
List<Integer> list = new ArrayList<>();
list.add(card1);
list.add(card2);
list.add(card3);
list.removeIf(i -> i >= 1 && i <= 3);
if (list.size() == 1) {
System.out.println("SIX");
}
Here is one way to do it using only conditionals. There are many other ways too.
First check that at least 1 card is > 3. This is done by inverting (!) the condition if all three cards are less than or equal to 3.
Then check that only two of the remaining cards are <= 3
if (!(card1 <= 3 && card2 <= 3 && card3 <= 3)) {
// Now check that only two of the remaining cards are <= 3
if ((card1 > 3 && card2 <= 3 && card3 <= 3)
|| (card2 > 3 && card1 <= 3 && card3 <= 3)
|| (card3 > 3 && card1 <= 3 && card2 <= 3)) {
System.out.println("SIX");
}
}
You could also simply stream the cards and filter them.
only let cards of value <= 3 thru the filter.
then count them.
if there are two cards, print SIX
if (IntStream.of(card1,card2,card3).filter(card->card <= 3).count() == 2) {
System.out.println("SIX");
}
For more help on conditionals I recommend you check out De Morgan's Laws

Java 2-D Arrays - Tic Tac Toe Game

This tic tac toe game is user vs computer based.
For this assignment, I'm supposed to fill in the missing pieces, which is just finishing up the main and moveAI methods(the other methods are already preset, I just need to call them); there is comments provided where I need to add code to make this tic tac toe game work. I believe that my main method that I have is almost done except for the 2 empty lines under the comments, I'm not sure what they mean by that and also I'm currently stuck on getting my moveAI method to work. This is where I'm supposed to generate random places where the computer will move after I put an X somewhere. I'm not sure how to write something that will check to make sure the spot where the computer moves is available and not already taken by the user. Thanks in advance for the help!
import java.util.Random;
import java.util.Scanner;
public class TicTacToeGame
{
public static void main ( String[] args )
{
Scanner in = new Scanner ( System.in );
char[][] board = new char[ 3 ][ 3 ];
int x, y = -1;
char winner = 'N';
// Initialize the board to spaces
// boolean noWinner = true;
char player = 'X';
for (int r = 0; x < board.length(); r++)
{
for (int c = 0; c < board.length(); c++)
{
board[r][c] = ' ';
}
}
// Print the game board
printBoard(board);
// Keep playing while the game isn't finished
while (winner == 'N')
{
while (x < 0 && x >2 && y <0 && y > 2)
{
System.out.println("Enter the row and column, separated by spaces: ");
x = in.nextInt();
y = in.nextInt();
}
// Get the location from the user and validate it
// Mark the position in the board according to the user's specified location
// Print the board after the user plays
printBoard(board);
// Check to see if the game is finished. If it is, break out of the loop.
// Have the AI make a move
moveAI(board);
// Print the board after the AI plays
printBoard(board);
// Check to see who the winner is
winner = checkWinner(board);
}
// If the winner is 'X' or 'O', print that, otherwise, it is a tie
if (winner == 'X')
System.out.println("X is the winner!");
else if (winner == 'O')
System.out.println("O is the winner!");
else
System.out.println("Tie");
}
/**
* Makes a move for the AI, and marks the board with an 'O'.
*
* #param board The game board
*/
public static void moveAI ( char[][] board )
{
int x,y = -1;
Random r = new Random();
x = r.nextInt(3);
y = r.nextInt(3);
boolean open = false;
// Validate that the random location generated is valid.
while (x < 0 && x >2 && y < 0 && y > 2)
{
x = r.nextInt(3);
y = r.nextInt(3);
}
while (open == false)
{
if (board[r][c] != ' ')
while (x < 0 && x >2 && y < 0 && y > 2)
{
x = r.nextInt(3);
y = r.nextInt(3);
}
}
}
// Keep recalculating the location if the one generated is not
// if (board[r][c] != ' ')
//{
// an empty space.
System.out.print(" ");
// Be sure to mark the position in the board with an 'O'
board[][]=in.nextInt(3) + 'O';
}
/**
* Prints out the tic-tac-toe board
*
* #param board The game board
*/
public static void printBoard ( char[][] board )
{
// Box drawing unicode characters:
char a = '\u250c'; // U+250C : top-left
char b = '\u2510'; // U+2510 : top-right
char c = '\u2514'; // U+2514 : bottom-left
char d = '\u2518'; // U+2518 : bottom-right
char e = '\u252c'; // U+252C : top-vertical-connector
char f = '\u2534'; // U+2534 : bottom-vertical-connector
char g = '\u251c'; // U+251C : left-horizontal-connector
char h = '\u2524'; // U+2524 : right-horizontal-connector
char i = '\u253c'; // U+253C : center plus sign connector
char j = '\u2500'; // U+2500 : horizontal
char k = '\u2502'; // U+2502 : vertical
String l = j + "" + j + "" + j; // Three horizontals
// Print out the game board
System.out.printf ( "\n 0 1 2\n" +
" %c%s%c%s%c%s%c\n" +
"0 %c %c %c %c %c %c %c\n" +
" %c%s%c%s%c%s%c\n" +
"1 %c %c %c %c %c %c %c\n" +
" %c%s%c%s%c%s%c\n" +
"2 %c %c %c %c %c %c %c\n" +
" %c%s%c%s%c%s%c\n\n",
a, l, e, l, e, l, b,
k, board[0][0], k, board[0][1], k, board[0][2], k,
g, l, i, l, i, l, h,
k, board[1][0], k, board[1][1], k, board[1][2], k,
g, l, i, l, i, l, h,
k, board[2][0], k, board[2][1], k, board[2][2], k,
c, l, f, l, f, l, d );
}
/**
* Checks the result of the game
*
* #param board The game board
* #return 'X' if 'X' is the winner
* 'O' if 'O' is the winner
* 'T' if the game is a tie
* 'N' if the game isn't finished
*/
public static char checkWinner( char[][] board )
{
if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X' || // Check row 0
board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X' || // Check row 1
board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X' || // Check row 2
board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X' || // Check col 0
board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X' || // Check col 1
board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X' || // Check col 2
board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X' || // Check diag \
board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X' || // Check diag /
board[0][0] == 'x' && board[0][1] == 'x' && board[0][2] == 'x' || // Check row 0
board[1][0] == 'x' && board[1][1] == 'x' && board[1][2] == 'x' || // Check row 1
board[2][0] == 'x' && board[2][1] == 'x' && board[2][2] == 'x' || // Check row 2
board[0][0] == 'x' && board[1][0] == 'x' && board[2][0] == 'x' || // Check col 0
board[0][1] == 'x' && board[1][1] == 'x' && board[2][1] == 'x' || // Check col 1
board[0][2] == 'x' && board[1][2] == 'x' && board[2][2] == 'x' || // Check col 2
board[0][0] == 'x' && board[1][1] == 'x' && board[2][2] == 'x' || // Check diag \
board[0][2] == 'x' && board[1][1] == 'x' && board[2][0] == 'x') // Check diag /
{
return 'X';
}
else if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O' || // Check row 0
board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O' || // Check row 1
board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O' || // Check row 2
board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O' || // Check col 0
board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O' || // Check col 1
board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O' || // Check col 2
board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O' || // Check diag \
board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O' || // Check diag /
board[0][0] == 'o' && board[0][1] == 'o' && board[0][2] == 'o' || // Check row 0
board[1][0] == 'o' && board[1][1] == 'o' && board[1][2] == 'o' || // Check row 1
board[2][0] == 'o' && board[2][1] == 'o' && board[2][2] == 'o' || // Check row 2
board[0][0] == 'o' && board[1][0] == 'o' && board[2][0] == 'o' || // Check col 0
board[0][1] == 'o' && board[1][1] == 'o' && board[2][1] == 'o' || // Check col 1
board[0][2] == 'o' && board[1][2] == 'o' && board[2][2] == 'o' || // Check col 2
board[0][0] == 'o' && board[1][1] == 'o' && board[2][2] == 'o' || // Check diag \
board[0][2] == 'o' && board[1][1] == 'o' && board[2][0] == 'o') // Check diag /
{
return 'O';
}
boolean finished = true;
// If there is a blank space in the board, the game isn't finished yet
for (int i = 0; i < board.length; i++)
for (int j = 0; j < board[ i ].length; j++)
if (board[ i ][ j ] == ' ')
finished = false;
// If the board is finished and 'X' or 'O' wasn't returned, then it is a tie
// Otherwise, the game is not finished yet
if ( finished )
return 'T';
else
return 'N';
}
}
// Get the location from the user and validate it
It looks like you are already doing this in the preceding lines:
// you're validating the input here
while (x < 0 && x >2 && y <0 && y > 2)
{
System.out.println("Enter the row and column, separated by spaces: ");
// you're getting the location from the user here
x = in.nextInt();
y = in.nextInt();
}
You probably still need to check to see if the position the user entered is a position that's already been filled in. You need to look in the array for that. There's a comment in moveAI giving you a hint for this. You check if board[x][y] is a space.
Also, I think you have a bug there because you never reset the values for x and y, so the next time the player's turn comes around the loop will not be entered. x and y still have the last turn's values so the loop condition evaluates to false. I think you can solve this problem by using a do{...}while(...); loop instead of a while(...){...} loop.
// Mark the position in the board according to the user's specified location
This just means put an 'O' in board[x][y].
// Check to see if the game is finished. If it is, break out of the loop.
It looks like checkWinner does this. It returns 'T' if the game is done and 'N' if it's still going.
For the AI code, here is some pseudocode for what I think you should be doing:
1. generate random x, y coordinates
2. check if board[x][y] is already used (there's a comment that tells how)
3. if it's used, go back to 1, otherwise put an 'X' in that space and return
Also, you don't need the loops that check for while (x < 0 && x >2 && y < 0 && y > 2) in the AI code. Random.nextInt(3) already returns a number in the range 0-2.

Read input from user and ignore the spaces between words, converting letters to telephone numbers

I've been trying to figure out how to ignore the blank space/digits/letters by using the character.isDigit & character.isLetter method when the users enters a String.. Can you guys please advise me?
When I tried the input with GETLOAN (Without the space) it works well...
But when I enter a space between e..g. Get Loan, the program shows an error..
public static void main(String[] args) {
String letters;
char phoneDigit;
Scanner kb = new Scanner(System.in);
System.out.println("Enter letters : ");
letters = kb.next();
for (int i = 0; i < 7; i++) {
phoneDigit = letters.charAt(i);
// Using character.isDigit...
if (Character.isDigit(phoneDigit) == true || Character.isLetter(phoneDigit) == true);
{
if (i == 3) {
System.out.println("-");
} //If
if (phoneDigit >= 'A' && phoneDigit <= 'C'
|| phoneDigit >= 'a' && phoneDigit <= 'c') {
System.out.println("2");
} else if (phoneDigit >= 'D' && phoneDigit <= 'F'
|| phoneDigit >= 'd' && phoneDigit <= 'f') {
System.out.println("3");
} else if (phoneDigit >= 'G' && phoneDigit <= 'I'
|| phoneDigit >= 'g' && phoneDigit <= 'i') {
System.out.println("4");
} else if (phoneDigit >= 'J' && phoneDigit <= 'L'
|| phoneDigit >= 'j' && phoneDigit <= 'l') {
System.out.println("5");
} else if (phoneDigit >= 'M' && phoneDigit <= 'O'
|| phoneDigit >= 'm' && phoneDigit <= 'o') {
System.out.println("6");
} else if (phoneDigit >= 'P' && phoneDigit <= 'S'
|| phoneDigit >= 'p' && phoneDigit <= 's') {
System.out.println("7");
} else if (phoneDigit >= 'T' && phoneDigit <= 'V'
|| phoneDigit >= 't' && phoneDigit <= 'v') {
System.out.println("8");
} else if (phoneDigit >= 'W' && phoneDigit <= 'Z'
|| phoneDigit >= 'W' && phoneDigit <= 'z') {
System.out.println("9");
} // If
} // If
} // For loop
} //PSVM
remove the semicolon(;) from here
if (Character.isDigit(phoneDigit) == true || Character.isLetter(phoneDigit) == true);//semicolon
; means end of statement.This means if conditions ended there.The statements under if will always be executed irrespective of what if returns(true or false) so the rest of statements under {} will simply behave as non static blocks
When I tried the input with GETLOAN (Without the space) it works well... But when I enter a space between e..g. Get Loan, the program shows an error..
You are getting error when you enter space because of the reason I specified above as the statements under if will always be executed
If you enter two words separated by a space, e.g. "get loan", the Scanner produces two output elements, i.e. the invocation of kb.next() just returns "get". A second invocation would return "loan". The Scanner class is not the right class to use for your purpose.
Use something like
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter String");
String s = br.readLine();
to read from the console.

How to make multiplication and division counted first in my calculator?

Hello guys im trying to make a calculator that is based on user scanner input, The calculator works fine for calculating from left to right but im having difficulties putting priorities in *,/ and ()
for example 3+(5*2)+1 should be 14 instead mine is 17, and then when i tried
5+((2+1)*3)-1 it gives me weird error..
Can anyone help please? and how can i make my code appear more efficient and simpler thank you in advance.
So here is my code :
System.out.print("Input Equation : ");
n = s.next() + s.nextLine();
n = n.replaceAll("\\s+", "");
char[] nans = n.toCharArray();
c = 0;
for (int i = 0; i < n.length(); i++)
if (nans[i] == '+' || nans[i] == '-' || nans[i] == '/' || nans[i] == '*')
c++;
char[] op = new char[c];
int[] num = new int[c + 1];
c = 0;
for (int i = 0; i < n.length(); i++) {
if (nans[i] == '+' || nans[i] == '-' || nans[i] == '/' || nans[i] == '*') {
op[c] = nans[i];
c++;
}
}
c = 0;
for (int i = 0; i < n.length(); i++) {
if (nans[i] == '1' || nans[i] == '2' || nans[i] == '3' || nans[i] == '4' || nans[i] == '5'
|| nans[i] == '6' || nans[i] == '7' || nans[i] == '8' || nans[i] == '9' || nans[i] == '0')
nus = nus + nans[i];
else if (nans[i] == '+' || nans[i] == '-' || nans[i] == '/' || nans[i] == '*') {
num[c] = Integer.parseInt(nus);
nus = "";
c++;
}
if (i == n.length() - 1){
num[c] = Integer.parseInt(nus);
}
}
for (int i = 0; i < c; i++) {
if (op[i] == '+') {
result = result + num[i] + num[i + 1];
num[i + 1] = 0;
}
else if (op[i] == '-') {
result = result + num[i] - num[i + 1];
num[i + 1] = 0;
}
else if (op[i] == '/') {
result = (result + num[i]) / num[i + 1];
num[i + 1] = 0;
}
else if (op[i] == '*') {
result = (result + num[i]) * num[i + 1];
num[i + 1] = 0;
}
}
System.out.print(" = "+ result);
When you read a token (that is an argument for operator or simply a number), make sure that the next operator is not multiplication or division, otherwise, you want to calculate that one. You can achieve this by means of recursion easily.

Categories