I am running this program which taking inputs from command line for t(Desired Length),no of test
cases(N). But it is giving a run time error
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index
out of range: -1
at java.lang.AbstractStringBuilder.insert(AbstractStringBuilder.java:979)
at java.lang.StringBuilder.insert(StringBuilder.java:300)
at Test.main(Test.java:28)
I am getting confused where i am accessing wrong index.
import java.io.BufferedReader;
import java.io.InputStreamReader;
class TestClass {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
String line = br.readLine();
int N = Integer.parseInt(line);
String[] profiles = null;
for (int i = 0; i < N; i++) {
profiles = br.readLine().trim().split(" ");
}
for (int i = 0; i < N; i++) {
if (Integer.parseInt(profiles[0]) > t && Integer.parseInt(profiles[1]) == t ||
Integer.parseInt(profiles[0]) > t || Integer.parseInt(profiles[1]) > t
|| Integer.parseInt(profiles[0]) == t && Integer.parseInt(profiles[1]) > t)
{
System.out.println("Crop");
}
if (Integer.parseInt(profiles[0]) < t && Integer.parseInt(profiles[1]) < t) {
System.out.println("Upload next");
}
if (Integer.parseInt(profiles[0]) == t && Integer.parseInt(profiles[1]) == t) {
System.out.println("Accepted");
}
}
System.out.println("Hello World!");
}
}
Your coding is unsafe, as you use the indexes [0] and [1] on profiles without checking that this array has any elements. Further you loop around overwriting the contents of profiles in a loop. What happens when there is no text to read from br?
You should always check the values before access (as in defensive programming).
Perhaps some Rubber Duck debugging is needed here.
I don't understand the checks you want to do, but if you want to check if each string is longer, shorter or equals to t, you should use this code:
public class Main
{
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[]) throws IOException {
int t = Integer.parseInt(br.readLine());
int N = Integer.parseInt(br.readLine());
/* MUST INITIALISE BEFORE FILLING IT! */
String [] profiles = new String[N];
for (int i = 0; i < N; i++) {
profiles[i] = br.readLine().trim();
if (profiles[i].length() < t)
System.out.println("shorter than t");// do something
else if (profiles[i].length() == t)
System.out.println("equals to t");// do something
else
System.out.println("longer than t");// do something
}
}
}
You are using a buffer reader to read some lines, with System.in . And before closing this buffer you try to make use of System.out . Could it be the reason for your trouble ? I think that asking the System to write and read at the same time is definitely not a good idea. Can you try to put a br.close() before the second loop ?
Here Lies the Demon in your Code :
// Let t=3 and N=5
String [] profiles=null; // Let the Input Here Be 12, 23, 34, 45, 56
for (int i = 0; i < N; i++) {
profiles=br.readLine().trim().split(" ");
}
System.out.println(Arrays.toString(profiles)); // [56] Only ? because you are initializing the same rer again and again
So when execution Goes Here :
for (int i = 0; i < N; i++) {
if (Integer.parseInt(profiles[0]) > t &&
Integer.parseInt(profiles[1]) == t || ... // Throws Index out of bounds exception
EDIT 1 : Here is the full Working Code :
package com.stackoverflow;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class TestClass {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
System.out.println(t);
String line = br.readLine();
int N = Integer.parseInt(line);
System.out.println(N);
String[] profiles = new String[N];
for (int i = 0; i < N; i++) {
profiles[i] = br.readLine().trim();
}
for (int i = 0; i < N; i++) {
System.out.println(Integer.parseInt(profiles[0]) > t && Integer.parseInt(profiles[1]) == t);
System.out.println(Integer.parseInt(profiles[0]) > t || Integer.parseInt(profiles[1]) > t);
System.out.println(Integer.parseInt(profiles[0]) == t && Integer.parseInt(profiles[1]) > t);
if (Integer.parseInt(profiles[0]) > t && Integer.parseInt(profiles[1]) == t ||
Integer.parseInt(profiles[0]) > t || Integer.parseInt(profiles[1]) > t
|| Integer.parseInt(profiles[0]) == t && Integer.parseInt(profiles[1]) > t)
{
System.out.println("Crop");
}
System.out.println(Integer.parseInt(profiles[0]) < t && Integer.parseInt(profiles[1]) < t);
if (Integer.parseInt(profiles[0]) < t && Integer.parseInt(profiles[1]) < t) {
System.out.println("Upload next");
}
System.out.println(Integer.parseInt(profiles[0]) == t && Integer.parseInt(profiles[1]) == t);
if (Integer.parseInt(profiles[0]) == t && Integer.parseInt(profiles[1]) == t) {
System.out.println("Accepted");
}
}
System.out.println("Hello World!");
}
}
EDIT 2 : Adding the Test Cases for the if conditions inside for loop :
Test Case1 Values : Let t=3, N=5 and the values for five(i.e N) test case be 12, 23, 34, 45, 56
Prints "crop" to the Console.
Test Case2 Values : Let t=30, N=5 and the values for five(i.e N) test case be 12, 23, 34, 45, 56
Prints "upload next" to the Console.
Test Case3 Values : Let t=10, N=5 and the values for five(i.e N) test case be 10, 20, 34, 45, 56
Prints "Accepted" to the Console.
Related
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int test_cases = Integer.parseInt(br.readLine());
if (test_cases >= 1 && test_cases <= 20) {
String[] ans = new String[test_cases];
for (int i = 0; i < test_cases; i++) {
char[] n = br.readLine().toCharArray();
int k = 0;
int j = n.length - 1;
while (k <= j) {
if (n[k] == n[j]) {
k++;
j--;
ans[i] = "wins";
}
else
ans[i] = "loses";
}
}
for (String s : ans)
System.out.println(s);
}
}
catch (Exception x) {
}
}
PROBLEM STATEMENT: The first line of the input contains an integer T, the number of testcases. This is followed by T lines containing an integer N. For each input output "wins" if the number is a palindrome and "loses" if not, in a new line.
For test_cases=1, the program works fine but for test_cases>1 the program keeps taking input. I have solved the palindrome problem but I still can't understand what is wrong with this code. Can anybody explain to me why does it keeps taking input?
For non palindrome, your code runs in infinite loop. Just add break for that.
while (k <= j) {
if (n[k] == n[j]) {
k++;
j--;
ans[i] = "wins";
}
else {
ans[i] = "loses";
break;
}
}
This can be one of the solution.
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int test_cases = Integer.parseInt(br.readLine());
if (test_cases >= 1 && test_cases <= 20) {
for (int i = 0; i < test_cases; i++) {
char[] n = br.readLine().toCharArray();
int k = 0;
int j = n.length - 1;
boolean flag=false;
while (k <= j) {
if (n[k] == n[j]) {
k++;
j--;
flag=true;
}
else{
flag=false;
break;
}
}
if(flag) System.out.println("wins");
else System.out.println("loses");
}
}
}
catch (Exception x) {
}
}
For a string which is not a palindrome, your loop runs infinitely.
Also, you need not store the results in an array for all the test cases, you can print it every time for each test case. However, if you intend to store strings and display results later, you can use something like StringBuffer classes.
I am trying to do a summation puzzle, the questions asks to use summation puzzles by enumerating and testing all possible configurations and then it says use it to solve the examples given. The examples given were
pot + pan = bib
dog+cat= pig
boy + girl = baby
I keep getting an error saying left hand side of assignment must be a variable
charSet.charAt(setIndex++) = stringTwo.charAt(loop);
cannot convert from int to bool.
if (exists = 0)
Also in my code where I try to display the output it doesn't run.
import java.util.Scanner;
public class Recursion
{
// Example program
public static String stringOne = new String(new char[10]);
public static String stringTwo = new String(new char[10]);
public static String stringThree = new String(new char[11]);
public static String charSet = new String(new char[11]);
public static int numberOne;
public static int numberTwo;
public static int numberThree;
public static int maxCharCount;
public static int[] numberSet = new int[10];
public static void checkForEquality()
{
numberOne = numberTwo = numberThree = 0;
int loop;
int subloop;
for (loop = 0; loop < stringOne.length(); loop++)
{
for (subloop = 0; subloop < maxCharCount; subloop++)
{
if (stringOne.charAt(loop) == charSet.charAt(subloop))
{
if (loop == 0 && numberSet[subloop] == 0)
return;
//generate the number
numberOne = (numberOne * 10) + numberSet[subloop];
}
}
}
for (loop = 0; loop < stringOne.length(); loop++)
{
for (subloop = 0; subloop < stringTwo.length(); subloop++)
{
if (stringTwo.charAt(loop) == charSet.charAt(subloop))
{
if (loop == 0 && numberSet[subloop] == 0)
return;
//generate the numeber
numberTwo = (numberTwo * 10) + numberSet[subloop];
}
}
}
for (loop = 0; loop < stringThree.length(); loop++)
{
for (subloop = 0; subloop < maxCharCount; subloop++)
{
if (stringThree.charAt(loop) == charSet.charAt(subloop))
{
if (loop == 0 && numberSet[subloop] == 0)
return;
//generate the number
numberThree = (numberThree * 10) + numberSet[subloop];
}
}
}
if (numberOne + numberTwo == numberThree)
{
//display the output
System.out.print(" Summation Puzzle solved. ");
System.out.print("\n");
System.out.print(stringOne);
System.out.print("<==>");
System.out.print(numberOne);
System.out.print("\n");
System.out.print(stringTwo);
System.out.print("<==>");
System.out.print(numberTwo);
System.out.print("\n");
System.out.print(stringThree);
System.out.print("<==>");
System.out.print(numberThree);
System.out.print("\n");
//loop to show the result
for (loop = 0; loop < maxCharCount; loop++)
{
System.out.print(charSet.charAt(loop));
System.out.print("<==>");
System.out.print(numberSet[loop]);
System.out.print("\n");
}
System.exit(0);
}
}
public static void generateCombinations(int indexCounter, int[] availableSet)
{
int loop;
if (indexCounter != 0)
{
for (loop = 0; loop < 10; loop++)
{
numberSet[indexCounter] = loop;
if (availableSet[loop] == 1)
{
availableSet[loop] = 0;
generateCombinations(indexCounter + 1, availableSet);
availableSet[loop] = 1;
}
}
}
if (indexCounter == maxCharCount)
{
checkForEquality();
}
}
public static void createCharSet()
{
int loop;
int setIndex;
int exists;
int subloop;
setIndex = 0;
for (loop = 0; loop < stringOne.length(); loop++)
{
exists = 0;
for (subloop = 0; subloop < setIndex; subloop++)
{
if (stringOne.charAt(loop) == charSet.charAt(subloop))
{
exists = 1;
}
}
if (exists == 0)
{
charSet = StringFunctions.changeCharacter(charSet, setIndex++, stringOne.charAt(loop));
}
}
for (loop = 0; loop < stringTwo.length(); loop++)
{
exists = 0;
for (subloop = 0; subloop < setIndex; subloop++)
{
if (stringTwo.charAt(loop) == charSet.charAt(subloop))
{
exists = 1;
}
}
if (exists == 0)
{
charSet = StringFunctions.changeCharacter(charSet, setIndex++, stringTwo.charAt(loop));
}
}
for (loop = 0; loop < stringThree.length(); loop++)
{
exists = 0;
for (subloop = 0; subloop < setIndex; subloop++)
{
if (stringThree.charAt(loop) == charSet.charAt(subloop))
{
exists = 1;
}
}
if (exists == 0)
{
charSet = StringFunctions.changeCharacter(charSet, setIndex++, stringThree.charAt(loop));
}
}
maxCharCount = setIndex;
}
public static void calculateSummation()
{
int loop;
if (maxCharCount > 10)
{
System.out.print("Please check the input again");
return;
}
else
{
int[] avaliableSet = new int[10];
for (loop = 0; loop < 10; loop++)
{
avaliableSet[loop] = 1;
}
generateCombinations(0, avaliableSet);
}
}
public static void main(String[]args)
{
Scanner scan = new Scanner(System.in);
System.out.print(" Enter the first String :");
stringOne = scan.next();
System.out.print(" Enter the second String :");
stringTwo = scan.next();
System.out.print(" Enter the thirsd String :");
stringThree = scan.next();
createCharSet();
System.out.print(" The character set formed from the given string = ");
System.out.print(charSet);
calculateSummation();
checkForEquality();
}
}
A lot of your problems are stemming from the syntax errors in the code you've written. For example:
line 74: if (stringThree.charAt(loop) == charSet.charAt(subloop) != null)
charSet.charAt(subloop) != null is an invalid comparison since the != operator cannot be used for booleans when comparing to null. If you're trying to determine if the characters return from .charAt(var) exist, use parentheses to make independent comparisons of each object.charAt(var) to null.
line 183: charSet = tangible.StringFunctions.changeCharacter(charSet, setIndex++, stringOne.charAt(loop));
tangible is ironically not tangible, as the variable does not exist locally or has not been defined globally.
charSet.charAt(setIndex++) = stringTwo.charAt(loop);
charSet.charAt(setIndex++) is a method that returns a character. This does not mean you can set the character at the specified index like it's a variable.
line 227: if (exists = 0)
You must use == when conducting comparisons in a conditional.
line 269: Scanner scan = new Scanner(System.in);
The Scanner class was not imported and thus cannot be used.
line 283: charSet.charAt(maxCharCount) = '\0';
Again, you can't use .charAt(var) to set the character at that index like it's a variable.
All of these problems can be self-determined by using a proper IDE, such as Eclipse.
Edit: Try to spend a little more time with pencil and paper working out the logic of your program before writing the code to represent your algorithm. This way you have focus and can write more comprehensive, commented, cleaner code. Here is a bit of a guide to help condense your existing project.
I am trying to do a school project and I'm having problems; my code is:
public class Class {
public static void main(String[] args) {
Scanner lector = new Scanner(System.in);
int code = 0, i = 0;
boolean error = true;
//Start of program
System.out.println("Inputs ------------------");
//Ask for input
do {
System.out.print("Code: ");
code = lector.nextInt();
if ( code < 0 || code > 2000) {
error = false;
} i = i + 1;
} while (!error || i < 3);
if (error) {...rest of the program
My problem is that I need to exit the loop if the input is > 0 & < 2000, and I need to stop executing the program if the user exceed 3 intents.
Any help would be very apreciated! Thank you!
This
while (!error || i < 3);
should be
while (!error && i < 3);
You want to continue looping while error is false and i < 3. Also i = i + 1; can be written as i++ (or with a preincrement). So you could do
boolean valid = false;
do {
System.out.print("Code: ");
code = lector.nextInt();
if (code > 0 && code < 2000) {
valid = true;
}
i++;
} while (!valid && i < 3);
The issue I'm having is that the 2D array is not printing out everything in the array, and when I run the evolve method, it doesn't print out the 2D array the same as the first time. Wondering what's wrong to make it not print out the entire array, and not print the array the same way when the evolve method runs? Each print out of the array needs to be in a 25x75 grid
Desired initial result:
Actual result:
evolve method result:
Project 4:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Project4 {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in); // Created a scanner
System.out.println("Enter the file name you would like to use");
File file = new File(input.nextLine()); // Takes file name to find file
Scanner inputFromFile = new Scanner(file);
FileInputStream fileInput = new FileInputStream(file); // reads file
int r;
int y = 0;
int i = 0;
while ((r = fileInput.read()) != -1) { // goes through each character in
// file, char by char
char c = (char) r;
GameOfLife.grid[i][y] = c;
y++;
if (y == 75) {
y = 0;
i++;
if (i == 25) {
break;
}
}
}
// Prints the initial environment
System.out.println("Initial set: ");
for (int j = 0; j < GameOfLife.grid.length; j++) {
System.out.print(GameOfLife.grid[j]);
}
boolean operate = true;
while (operate = true) {
System.out.println("Do you want to see the next generation? Y/N?");
String q = input.nextLine();
if (q.equalsIgnoreCase("y")) {
GameOfLife.evolve();
for (int j = 0; j < GameOfLife.grid.length; j++) {
System.out.print(GameOfLife.grid[j]);
operate = true;
}
} else {
operate = false;
System.exit(0);
}
}
}
}
GameOfLife:
import java.util.Arrays;
public class GameOfLife {
static final int m = 25; // number of rows
static final int n = 75; // number of columns
static char[][] grid = new char[m][n]; // Creates an empty (no dots or
// X's)grid of rows and columns.
static int count_neighbors(int i, int j) {
int nn = 0; // number of neighbors of cell(i,j)
if (i > 0 && j > 0 && grid[i - 1][j - 1] == 'X') {
nn++;
}
;
if (i > 0 && grid[i - 1][j] == 'X') {
nn++;
}
;
if (i > 0 && j < 72 && grid[i - 1][j + 1] == 'X') {
nn++;
}
;
if (j > 0 && grid[i][j - 1] == 'X') {
nn++;
}
;
if (j < 72 && grid[i][j + 1] == 'X') {
nn++;
}
;
if (j > 0 && i < 22 && grid[i + 1][j - 1] == 'X') {
nn++;
}
;
if (i < 22 && grid[i + 1][j] == 'X') {
nn++;
}
;
if (i < 22 && j < 72 && grid[i + 1][j + 1] == 'X') {
nn++;
}
return nn;
}
static void evolve() {
for (int i = 0; i < 23; i++) {
for (int j = 0; j < 73; j++) {
int s = count_neighbors(i, j);
if (s < 2) {
grid[i][j] = '.';
}
if (s == 2 || s == 3) {
grid[i][j] = 'X';
}
if (s > 3) {
grid[i][j] = '.';
}
}
}
}
}
Add a System.out.println() after each for-loop to start printing the grid on the next line.
// Print out grid.length characters
for (int j = 0; j < GameOfLife.grid.length; j++) {
System.out.print(GameOfLife.grid[j]);
}
// Print a new line
System.out.println();
Updated
The problem I failed to notice above is that you are printing an entire row in one line, and the whole grid in one (visible) loop. The following System.out.println() would therefore print a blank line after the entire grid is printed. Please see my solution which extracts out the printing of the game, uses a try-catch for the file reader, and removes the unnecessary operate variable at the bottom since you were just exiting anyway.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Project4 {
private static void printGame() {
for (char[] row : GameOfLife.grid) {
for (char c : row) {
System.out.print(c);
}
System.out.println();
}
}
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in); // Created a scanner
System.out.println("Enter the file name you would like to use");
File file = new File(input.nextLine()); // Takes file name to find file
BufferedReader br = null;
String line;
int i = 0;
try {
br = new BufferedReader(new FileReader(file));
while ((line = br.readLine()) != null) {
for (int col = 0; col < line.length(); col++) {
GameOfLife.grid[i][col] = line.charAt(col);
}
i++;
if (i == 25) {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
br.close();
}
}
// Prints the initial environment
System.out.println("Initial set: ");
printGame();
while (true) {
System.out.println("Do you want to see the next generation? Y/N?");
String q = input.nextLine();
if (q.equalsIgnoreCase("y")) {
GameOfLife.evolve();
printGame();
} else {
System.exit(0);
}
}
}
}
I think you should try using println this in both places like this:
System.out.println(GameOfLife.grid[j]);
System.out.print() is not flushed automatically, it's only flushed when a newline is written. This means that rather than writing out everything, it might buffer the IO to keep things more efficient.
This question already has an answer here:
Output of numbers are incorrect sometimes.
(1 answer)
Closed 9 years ago.
When I do different combinations like d-c+a+b it gives me a inccorect number like 118.0. Can someone tell me where in my code my calculations are wrong..
Thank you
the ValVarPairs.txt contains these numbers-> a=100,b=5,c=10,d=13
This is what i coded.
package com.ecsgrid;
import java.io.*;
public class testC {
public static void main(String[] args) {
int i = 0,j = 0;
double result, values[] = new double[4];
char k, operators[] = new char[3];
for (i = 0; i <= 2; i++)
operators[i] = '+'; // default is to add the values
File myfile;
StreamTokenizer tok;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String InputText;
i = 0;
try {
myfile = new File("C:\\VarValPairs.txt");
tok = new StreamTokenizer(new FileReader(myfile));
tok.eolIsSignificant(false);
while ((tok.nextToken() != StreamTokenizer.TT_EOF) && (i <= 3)){
if ((tok.ttype == StreamTokenizer.TT_NUMBER))
values[i++] = tok.nval;
}
}
catch(FileNotFoundException e) { System.err.println(e); return; }
catch(IOException f) { System.out.println(f); return; }
System.out.println("Enter letters and operators:");
try {
InputText = in.readLine();
}
catch(IOException f) { System.out.println(f); return; }
for (i = 0; i < InputText.length(); i++)
{
k = InputText.charAt(i);
if ((k == '+') || (k == '-'))
{
if (j <= 2) operators[j++] = k;
}
}
result = values[0];
for (i = 0; i <= 2; i++){
if (operators[i] == '+')
result = result + values[i+1];
else
result = result - values[i+1];
}
System.out.println(result);
}
}
Right now you would be getting the same output if your input was -++
You never parse for the order or a, b, c and d. You always assume the order a->b->c->d.
So d-c+a+b will be: a-b+c+d which is consistent with the output you provided(100-5+10+13 = 118)
OP's CODE
for (i = 0; i < InputText.length(); i++)
{
k = InputText.charAt(i);
if ((k == '+') || (k == '-'))
{
if (j <= 2) operators[j++] = k;
}
}
/OP'S CODE
In this loop, when k is not an operator, you should be reading which letter it is, and store the order in which the letters appeared. Or build some other kind of mapping. In any case you can't just ignore non-operator characters because they are part of the input.
Let's debug this a little, add a few system outs...
this is what you would see for each step
100.0 - 5.0
95.0 + 10.0
105.0 + 13.0
118.0
your value array is {100,5,10,13} and your operator array is {-,+,+}
you have not mapped a = 100, b = 5, c= 10, d = 13, unless you map those then parse the operands using the mapping based on the non operand input keys, it is not going to work.
So, if I were to use the character's int values, I would be able to translate it this way.
import java.io.*;
public class TestC {
public static void main(String[] args) {
int i = 0, j = 0;
double result, values[] = new double[4];
char k, operatorsAndOperands[] = new char[3];
for (i = 0; i <= 2; i++)
operatorsAndOperands[i] = '+'; // default is to add the values
File myfile;
StreamTokenizer tok;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String InputText;
i = 0;
try {
myfile = new File("C:\\VarValPairs.txt");
tok = new StreamTokenizer(new FileReader(myfile));
tok.eolIsSignificant(false);
while ((tok.nextToken() != StreamTokenizer.TT_EOF) && (i <= 3)) {
if ((tok.ttype == StreamTokenizer.TT_NUMBER))
values[i++] = tok.nval;
}
for (int l = 0; l < values.length; l++) {
System.out.println(values[l]);
}
} catch (FileNotFoundException e) {
System.err.println(e);
return;
} catch (IOException f) {
System.out.println(f);
return;
}
System.out.println("Enter letters and operators:");
try {
InputText = in.readLine().toUpperCase();
} catch (IOException f) {
System.out.println(f);
return;
}
if(InputText.length() > 0){
operatorsAndOperands = new char[InputText.length()];
} else {
System.out.println("No Operations specified");
return;
}
for (i = 0; i < InputText.length(); i++) {
k = InputText.charAt(i);
operatorsAndOperands[j++] = k;
}
result = 0;
for (i = 0; i < operatorsAndOperands.length; i++) {
System.out.println(operatorsAndOperands[i] + " " + (int)operatorsAndOperands[i]);
if(i+1<operatorsAndOperands.length)
System.out.println(operatorsAndOperands[i+1]);
switch(operatorsAndOperands[i]){
case '+':
if(operatorsAndOperands[i+1] != '+' && operatorsAndOperands[i+1] != '-'){
result+=values[(int)operatorsAndOperands[i+1] - (int)'A'];
i++;
}
break;
case '-':
if(operatorsAndOperands[i+1] != '+' && operatorsAndOperands[i+1] != '-'){
result-=values[(int)operatorsAndOperands[i+1] - (int)'A'];
i++;
}
break;
default:
result = values[(int)operatorsAndOperands[i] - (int)'A'];
break;
};
System.out.println(result);
}
System.out.println(result);
}
}