Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6349633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:39:16+00:00 2026-05-24T21:39:16+00:00

Ive just learned java and asked a question about my one line calculator it

  • 0

Ive just learned java and asked a question about my one line calculator it doenst give error anymore but it miscalculates.
Here is the code

import java.util.Scanner;

public class omg {
    public static void main(String args[]) {
        int fnum,snum,anum = 0;
        String strtype;
        char[] testchar;
        char currentchar;
        int machinecode = 0;
        String tempnumstr;
        int operatorloc = 0;
        char[] tempnum = new char[256]; 
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter The Calculation: ");
        strtype = scan.nextLine();
        testchar = strtype.toCharArray();
        for(int b = 0; b < testchar.length; b++)
        {
            currentchar = testchar[b];
            if(currentchar == '+') {
                machinecode = 1;
                operatorloc = b;
            }
            else if(currentchar == '-') {
                machinecode = 2;
                operatorloc = b;
            }
            else if(currentchar == '*') {
                machinecode = 3;
                operatorloc = b;
            }
            else if(currentchar == '/') {
                machinecode = 4;
                operatorloc = b;
            }
        }
        for(int t = 0;t < operatorloc;t++) {
            tempnum[t] = testchar[t];
        }
            tempnumstr = new String(tempnum).trim();
            fnum = Integer.parseInt(tempnumstr);
        for(int temp = operatorloc;temp < testchar.length;temp++) {
            for(int t = 0;t<(testchar.length-operatorloc);t++) {
                tempnum[t] = testchar[temp];
            }
        }
        tempnumstr = new String(tempnum).trim();
        snum = Integer.parseInt(tempnumstr);
        switch(machinecode) {
        case 1:
            anum = fnum + snum;
            break;
        case 2:
            anum = fnum - snum;
            break;
        case 3:
            anum = fnum * snum;
            break;
        case 4:
            anum = fnum / snum;
        }
        System.out.println(anum);
    }
}

This code would give me 8+8 = 96,
And this is obviously not correct.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:39:17+00:00Added an answer on May 24, 2026 at 9:39 pm

    The loop for the second term is faulty, you are nesting the loop, change it to

    for (int temp = operatorloc+1, t=0 ;temp < testchar.length;temp++, t++ ) {
        tempnum[t] = testchar[temp];
    }
    

    And it will work

    The original loop

    for (int temp = operatorloc;temp < testchar.length;temp++) {
        for (int t = 0;t<(testchar.length-operatorloc);t++) {
            tempnum[t] = testchar[temp];
        }
    }
    

    Is essentially equivalent to

    int temp = testchar.length-1;
    for (int t = 0;t<(testchar.length-operatorloc);t++) {
        tempnum[t] = testchar[temp];//temp doesn't change
    }
    

    And there was also an off by one on the start index (that’s where the +1 came from in int temp = operatorloc+1).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

as I've just learned in in my other question , I could use a
I've got an application that just shipped. Since I wrote it, I've learned about
I've just started playing around with Scala, and I just learned about how methods
I've been using C-style functions, but I just learned they can't see instance variables.
I've just recently learned C# and am interested in .net but I don't know
I've learned programming from Java, then tried to learn one programming language per year,
I've just learned about the CRTP Pattern and am looking for the original work.
i'm a beginner at web development, i've just learned about XHTML 1.0 and CSS
I just learned about the existence of the PRG pattern, and I'm wondering if
I just learned about the i/o part of the STL, more specifically fstream. Although

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.