Hello all. I would like to calculate, assign and do a lot with my program (java). Here are my codes.
import java.util.*;
public class Test
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
String[] s = new String[9];
int a;
for(a=0;a<9;a++)
{
s[a]=scan.next();
}
}
}
Here are my needs:
if the input is 1 2 3 4 5 6 =A1+A2 =A5+A3 =A5-A1
First i wd like to the program to recognize that the formulae start after the "=" sign, then allocate the string after the = sign to a position in the array(A1=s[0],A2=s[1] and so forfth), finally do the calulations and print the result as a matrix.(I know how to do all the rest but i'm stuck on these exceptional cases above). Thanks for help :=)
P.S.: There are spaces between the input.(1_2_3...).

Dani AI

Generated

The simplest, robust approach is to split the input into two zones: the initial numeric values and the formula tokens that follow the first "=" (either as its own token or attached like "=A1+A2"). Map A1 -> first numeric value, A2 -> second, etc. Watch the 1-based naming in formulas vs Java arrays (subtract 1 when you look up). Validate each numeric token with Double.parseDouble and validate any Ai index against the values array so out-of-range references produce a clear error.

A compact evaluator that supports only + and - can scan the formula left-to-right, applying each operator as it appears. Example Java utility (takes a formula string and an array of numeric values):

private static double evalExpr(String expr, double[] vals) {
    expr = expr.trim();
    if (expr.startsWith("=")) expr = expr.substring(1);
    int i = 0;
    double result = 0;
    int sign = +1;
    boolean seen = false;
    while (i < expr.length()) {
        char c = expr.charAt(i);
        if (Character.isWhitespace(c)) { i++; continue; }
        if (c == '+' || c == '-') { sign = (c == '+') ? 1 : -1; i++; continue; }
        if (Character.toUpperCase(c) == 'A') {
            i++;
            int start = i;
            while (i < expr.length() && Character.isDigit(expr.charAt(i))) i++;
            if (start == i) throw new IllegalArgumentException("Missing index after A");
            int idx = Integer.parseInt(expr.substring(start, i)) - 1;
            if (idx < 0 || idx >= vals.length) throw new IndexOutOfBoundsException("A" + (idx+1) + " out of range");
            if (!seen) { result = sign * vals[idx]; seen = true; }
            else { result += sign * vals[idx]; }
            sign = +1;
            continue;
        }
        throw new IllegalArgumentException("Unexpected char: " + c);
    }
    return result;
}

Collect results for each formula token, then print them in a matrix layout by choosing a column count and emitting a newline every cols values. For simple formatting use System.out.printf for fixed-width columns. This keeps parsing logic isolated from I/O, handles inputs like showed, and leverages the simplification noted (only + and -), so you avoid precedence complexity. Add clear error messages for malformed tokens and index errors to make debugging easier.

Recommended Answers

All 3 Replies

depends on the complexity of what kind of equations you want to be able to supply to it.

just addition and subtraction won't be too difficult for you, but if you want it to have MDAS then it will be slightly more complex, and PEMDAS is even more compex... i in fact have a thread on PEMDAS, and solved another on MDAS on this site, if that is what you need search for it on the site.

if you still need help after reading those threads, post more a more specific misunderstanding or problem

Thanks sciwizeh but i only wana do additions and subtractions. Thanks for help.

in that case, you can go through the string, find '=' and then take then get "a1" and associate it with the proper number in the beginning, then look at the symbol if use a flag for '+' or '-' then "a2" and associate it in the same fashion as "a1" then according to the flag set earlier either add or subtract "a2" from "a1"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.