-2

Hello Stack Overflow.

I have a problem with my code. The goal is to create a text-based calculator that reads an input file and processes the equations. The first number tells the program how many lines are there. I think I set up that part right, count is the number of lines. Here's what I got so far.

Scanner equationScan;
    int count;
    double a=0;
    double b=0;
    double calc1;

    equationScan = new Scanner(new File("calculator.txt"));

    count = equationScan.nextInt();

    while(count > 0)
    {
        String line = equationScan.nextLine();

        if(line.equals("sin"))
        {
            a = equationScan.nextDouble();   *Error shows up here. Req: variable Found: value
            Math.sin(a)= calc1;
        }
    }

The goal is a number of 'if' statements for the program to follow. I understand that part, but I can't get past this error. The first line of the text file reads an integer and I'm trying to see the second line of the file that reads sin of a double and to calculate that and store it. Help would be greatly appreciated!

  • What does your calculator.txt file look like? – dramzy Oct 18 '14 at 21:04
  • Stack Overflow is **not** a substitute of a good tutorial/book/class/google search query. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html – Jeroen Vannevel Oct 18 '14 at 21:07
  • So far my calculator.txt file looks like this: 9 Sin 15.5 – Ricardo Rigaroni Oct 18 '14 at 21:08
  • `while(count > 0)` ... smells like an infinte loop. And `equationScan.nextLine();` directly after a `.nextInt()` might cause an unexpected behavior: http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx – Tom Oct 18 '14 at 21:11

2 Answers2

1

Changes are in comment.

package calculator;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class calc {

    public static void main(String[] args) {



        Scanner equationScan = null;
        int count;
        double a=0;
        double b=0;
        double calc1;

        try { //optional: add a try catch block to catch FileNotFoundException exception
            equationScan = new Scanner(new File("calculator.txt"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        count = equationScan.nextInt();
        equationScan.nextLine();    //add a nextline() according to what TOM pointed

        while(count > 0)
        {
            String line = equationScan.nextLine();

            if(line.equals("sin"))
            {
                String value=equationScan.nextLine(); //Parse the double to a string
                a=Double.parseDouble(value);

                calc1 = Math.sin(a) ; //Assignment should be at the right sign of the = operator
            }
        }


    }
}
tarekgreens
  • 251
  • 3
  • 14
  • Thank you! Yours and Toms was easily the most helpful. Still trying to figure this out, but you're the best! – Ricardo Rigaroni Oct 18 '14 at 22:08
  • The infinite loop is still there :). – Tom Oct 18 '14 at 22:35
  • @Tom Yeah,I was going to decrement count-- but it wasn't compiling either way. So instead, I opted for switch statements in light of what he did and it compiles, but I get errors with the Scanner now saying no line can't be found. Should I just post a new question with what I did? A little more progress for sure. – Ricardo Rigaroni Oct 18 '14 at 22:40
  • @battleskies just fix your loop to `while(equationScan.hasNextLine())`. This loop will stop, if there are no more lines to read from the file. – Tom Oct 18 '14 at 22:42
  • @Tom It worked. No more errors with the compiler! However, when I try to print a or the calc1 variable, I get 0.0 instead of the value 15.0 or the corresponding calculation out of the file. Progress though I love it!What could possibly be causing that? – Ricardo Rigaroni Oct 18 '14 at 22:49
  • @battleskies I don't know what you're doing wrong. Above code works for me and the result of sin is *almost* correclty. `Math.sin` expects the number to be in radian. So you need to convert it `Math.sin(Math.toRadians(a))`. – Tom Oct 18 '14 at 22:58
0

Assignments are always in the format

variable = value;

And what you did is write the value you are calculating on the left hand side of the assignment operator =, and the variable you want to put the value in, on the right side.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79