Nightocoder201 30 Newbie Poster

Hello,

I am working a program that starts at a direction and goes to the destination and reverse. I have reversed the list and used a if statement for the switching the directions from R (Right) and L (Left) but it doesnt want to work properly. My question is how can I make it work?

Thank you

import java.io.FileInputStream;
import java.io.*;
import java.util.Scanner;
import java.util.Arrays;

public class Project3 {

  public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    System.out.print("Enter input file name");
    String inFileName = in.nextLine();

    String[] parsedName = inFileName.split("\\.");
    String outFileName = parsedName[0] + "Numbered." + parsedName[1];

    String[]directions = {"R","L"};

    if (directions[i].equals("L")) {
      newDirection = "R";
    } else {
      newDirection = "L";
    }

    try {
      Scanner fIn = new Scanner(new FileInputStream(inFileName));
      PrintWriter fOut = new PrintWriter(new FileOutputStream(outFileName));

      int lineNumber = 0;
      while (fIn.hasNext()) {
        String line = fIn.nextLine();
        lineNumber++;
      }

      int lineNumber2 = 20;
      String[] reverse = new String[20];

       while (fIn.hasNext()) {
        String line = fIn.nextLine();
        reverse[lineNumber] = line;
        lineNumber++;

      }
        for (int i = 0; i < reverse.length; i++) { //Reversing the list
        fOut.println(reverse[(reverse.length-1)-i]);

      }

      fOut.close();
      fIn.close();
      fOut.close();

    } catch (FileNotFoundException e) {
      System.err.println("File " + inFileName + " not found");
    }
    }
  }

Here is the text file that I need to change the directions:

Start at Home
R on College Ave
L on Jefferson Davis Hwy
R on Cowan Blvd
R on Carl D Silver Pkwy
Qdoba on R

And this is the output of the file:

Start at Qdoba
L on Carl …

Nightocoder201 30 Newbie Poster

Guys here is my output for the program

3 + 3 = 6
3 * 5 = 15
3 - 5 = -2
3 / 3 = 1
5 / 5  *  2  +  8  +  8  /  2  +  5 = 17 (not suppsoe to be 17 it suppose to be 11 calculate it)

My output should look like this:

3 + 5 = 8
3 * 5 = 15
3 - 5 = -2
3 / 5 = 0
5  /  5  *  2  +  8  /  2  +  5  = 11
5 * 5 + 2 / 2 - 8 + 5 * 5 - 2 = 41
Nightocoder201 30 Newbie Poster

@xrj, I have re-typed all of my code and it worked as expected. When the program reads 3 / 3 it prints out 0 but, when the program reads "5 / 5 * 2 + 8 / 2 + 5" it prints iut 17 and not 11.

Nightocoder201 30 Newbie Poster

Hello,

May I ask on what is wrong with my division operation. When the program reads it it comes out as an error.

import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.Integer.*;
import static java.lang.System.*;
public class ExpressionSolver
{
    int answer;
    String expString;
    ArrayList<String> exp;
    public ExpressionSolver(String s)
    {
        setExpression(s);
    }
    public void setExpression(String s)
    {
        expString = s;
        exp = new ArrayList<String>();
        for(String temp: s.split(" "))
        {
         exp.add(temp);
        }
    }
    public void solveExpression()
    { 
       int index1, index2, num1, num2;        
      while(exp.contains("*") || exp.contains("/"))
      {
           index1 = exp.indexOf("*");
           index2 = 0;
           if(index1 <= 0)
           {
             index1 = exp.indexOf("/");
             index2 = 1;
           }
         num1 = Integer.parseInt(exp.get(index1-1));
         num2 = Integer.parseInt(exp.get(index1+1));
        if(index2 == 0)
         {
            answer = num1 * num2;
         }
         else
         {
            answer = num1 / num2;
         }
         exp.remove(index1 - 1);
         exp.remove(index1 - 1);
         exp.set(index1 - 1, Integer.toString(answer));
      }
      while(exp.contains("+") || exp.contains("-"))
      {
           index1 = exp.indexOf("+");
        index2 = 0;
           if(index1 <= 0)
           {
             index1 = exp.indexOf("-");
          index2 = 1;
           }
         num1 = Integer.parseInt(exp.get(index1-1));
         num2 = Integer.parseInt(exp.get(index1+1));
        if(index2 == 0)
         {
            answer = num1 + num2;
         }
         else
         {
            answer = num1 - num2;
         }
         exp.remove(index1 - 1);
         exp.remove(index1 - 1);
         exp.set(index1 - 1, Integer.toString(answer));
      }
}
  public String toString()   
  {
   return expString + " = " + answer;
  }
}
//Runner
import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.Integer.*;
import static java.lang.System.*;
public class ExpressionSolverRunner
{
    public static void main( String args[] )
    {  
      ExpressionSolver test = new ExpressionSolver("3 + 5");
      test.solveExpression();
      System.out.println(test);
      test.setExpression("3 / 3");
      test.solveExpression();
      System.out.println(test);
      test.setExpression("5 / 5 * 2 + 8 / …
Nightocoder201 30 Newbie Poster

JamesCherrill,

Thanks for the comment it makes sense now. I acctually hate it when people just give me the answers. I want to know on what I did wrong with my code so I can lean and do a program similar to it later on.

Nightocoder201 30 Newbie Poster

Guys,

I am sorry that I am asking for so much help. I have learned on what you wrote I am not here just copying and pasting, I am here to learn. I know you guys probably hate me alot since you think that I am here just for answers while I am not I am like alot of other people learning and helping.

Again I am sorry and I wont be beg for answers, I just want to learn.

rproffitt commented: I sense no hate here. +15
Nightocoder201 30 Newbie Poster

@xrj I have changed the code in my program and when I run it it says "Out of bounce exeception: Index 99, size 1"

//Main

import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.Integer.*;
import static java.lang.System.*;
public class ExpressionSolver
{
    int answer;
    String expString;
    ArrayList<String> exp;
    public ExpressionSolver(String s)
    {
        setExpression(s);
    }
    public void setExpression(String s)
    {
        expString = s;
        exp = new ArrayList<String>();
        for(String temp: s.split(" "))
        {
         exp.add(temp);
        }
    }
    public void solveExpression()
    { 
       int index1, index2, num1, num2;        
      while(exp.contains("*") || exp.contains("/"))
      {
           index1 = exp.indexOf("*");
           index2 = 0;
           if(index1 <= 0)
           {
             index1 = exp.indexOf("/");
             index2 = 1;
           }
         num1 = Integer.parseInt(exp.get(index1-1));
         num2 = Integer.parseInt(exp.get(index1+1));
        if(index2 == 0)
         {
            answer = num1 * num2;
         }
         else
         {
            answer = num1 / num2;
         }
         exp.remove(index1 - 1);
         exp.remove(index1 - 1);
         exp.set(index1 - 1, Integer.toString(answer));
      }
      while(exp.contains("+") || exp.contains("-"))
      {
           index1 = exp.indexOf("+");
        index2 = 0;
           if(index1 <= 0)
           {
             index1 = exp.indexOf("-");
          index2 = 1;
           }
         num1 = Integer.parseInt(exp.get(index1-1));
         num2 = Integer.parseInt(exp.get(index1+1));
        if(index2 == 0)
         {
            answer = num1 + num2;
         }
         else
         {
            answer = num1 - num2;
         }
         exp.remove(index1 - 1);
         exp.remove(index1 - 1);
         exp.set(index1 - 1, Integer.toString(answer));
      }
}
  public String toString()   
  {
   return expString + " = " + answer;
  }
}

//Runner

import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.Integer.*;
import static java.lang.System.*;

public class ExpressionSolverRunner
{
    public static void main( String args[] )
    {  
      ExpressionSolver test = new ExpressionSolver("3 + 5");
      test.solveExpression();
      System.out.println(test);

      test.setExpression("3 / 3");
      test.solveExpression();
      System.out.println(test);

      test.setExpression("5 / 5 * 2 + …
Nightocoder201 30 Newbie Poster

Hey guys,

I am sorry for everything. As @xrj said that he modified the "+" and "-" operation method on the code below and I am stumoed on the "*" and "/" operation. May I ask on what I need to do.

Again I am sorry for getting this help. I never ask for alot of help.

import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.Integer.*;
import static java.lang.System.*;
public class ExpressionSolver
{
    int answer;
    String expString;
    ArrayList<String> exp;
    public ExpressionSolver(String s)
    {
        setExpression(s);
    }
    public void setExpression(String s)
    {
        expString = s;
        exp = new ArrayList<String>();
        for(String temp: s.split(" "))
        {
         exp.add(temp);
        }
    }
    public void solveExpression()
    { 
       int index1, index2, num1, num2;        
       while(exp.contains("*") || exp.contains("/"))
       {
         index1 = exp.indexOf("*");
         index2 = exp.indexOf("/");
         if(index1 <= 0)
         {
          index1 = 100;
         }
         if (index2 <= 0)
         {
          index2 = 100;
         }
          if(index1 < index2)
         {   
          num1 = Integer.parseInt(exp.get(index1-1));
          num2 = Integer.parseInt(exp.get(index1+1));
          answer = num1 * num2;
          exp.remove(index1 - 1);
          exp.remove(index1 - 1);
          exp.set(index1 - 1, Integer.toString(answer));
         }
         else if(index2 < index1)
         {
           num1 = Integer.parseInt(exp.get(index2 - 1));
           num2 = Integer.parseInt(exp.get(index2 + 1));
           answer = num1 / num2;
           exp.remove(index2 -1);
           exp.remove(index2 - 1);
           exp.set(index1 -1 ,Integer.toString(answer));
          }
      }
      while(exp.contains("+") || exp.contains("-"))
      {
           index1 = exp.indexOf("+");
        index2 = 0;
           if(index1 <= 0)
           {
             index1 = exp.indexOf("-");
          index2 = 1;
           }
         num1 = Integer.parseInt(exp.get(index1-1));
         num2 = Integer.parseInt(exp.get(index1+1));
        if(index2 == 0)
         {
            answer = num1 + num2;
         }
         else
         {
            answer = num1 - num2;
         }
         exp.remove(index1 - 1);
         exp.remove(index1 - 1);
         exp.set(index1 - 1, Integer.toString(answer)); …
Nightocoder201 30 Newbie Poster

Thank You guys again for all of your help. I have learned on what I did wrong.

Nightocoder201 30 Newbie Poster

Hello everyone,

Thank you for your tips, I have recheck my brackets and I was missling 2 of them and now it works.

Again thank you for your help.

Abdullah

EDIT: The complier works but whne I run the program it prints out the number but the answer is 0

Here is my updated code

import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.Integer.*;
import static java.lang.System.*;

public class ExpressionSolver
{
    int answer;
    String expString;
    ArrayList<String> exp;
    public ExpressionSolver(String s)
    {
        setExpression(s);
    }

    public void setExpression(String s)
    {
        expString = s;
        exp = new ArrayList<String>();

        for(String temp: s.split(" "))
        {
         exp.add(temp);
        }
    }

    public void solveExpression()
    { 
       int index1, index2, num1, num2;        

       while(exp.contains(" ") || exp.contains("/"))
       {
       index1 = exp.indexOf("*");
       index2 = exp.indexOf("/");

       if(index1 <= 0)
       {
        index1 = 100;
       }
        if (index2 <= 0)
       {
        index2 = 100;
       }

        if(index1 < index2)
      {   
        num1 = Integer.parseInt(exp.get(index1-1));
        num2 = Integer.parseInt(exp.get(index1+1));
        answer = num1 * num2;

        exp.remove(index1 - 1);
        exp.remove(index1 - 1);
        exp.set(index1 - 1, Integer.toString(answer));
       }
        else if(index2 < index1)
       {
        num1 = Integer.parseInt(exp.get(index2 - 1));
        num2 = Integer.parseInt(exp.get(index2 + 1));
        answer = num1 / num2;

        exp.remove(index2 -1);
        exp.remove(index2 - 1);
        exp.set(index1 -1 ,Integer.toString(answer));
        }
      }
        while(exp.contains("+") || exp.contains("-"))
        {
           index1 = exp.indexOf("+");
           index2 = exp.indexOf("-");

           if(index1 <= 0)
           {
             index1 = 100;
           }
           if(index2 <= 0)
           {
            index2 = 100;
           }      
            if(index2 < index1)
        {   
         num1 = Integer.parseInt(exp.get(index1-1));
         num2 = Integer.parseInt(exp.get(index1+1));
         answer = num1 + num2;

         exp.remove(index1 - 1);
         exp.remove(index1 - 1);
         exp.set(index1 - 1, Integer.toString(answer));
        }
         else if(index2 < index1) …
Nightocoder201 30 Newbie Poster

Hello everyone,

I am having trouble with my program its solving expressions and when I compile it I get a "Illegal start of expresson" line in my output. I am using Jgrasp as my softwear. May I ask to reveiw my code and see on what I did wrong? I am asking for learning purposes.

Thank You.

import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.Integer.*;
import static java.lang.System.*;

public class ExpressionSolver
{
    int answer;
    String expString;
    ArrayList<String> exp;
    public ExpressionSolver(String s)
    {
        setExpression(s);
    }

    public void setExpression(String s)
    {
        expString = s;
        exp = new ArrayList<String>();

        for(String temp: s.split(" "))
        {
         exp.add(temp);
        }
    }

    public void solveExpression()
    { 
       int index1, index2, num1, num2;        

       while(exp.contains(" ") || exp.contains("/"))
       {
       index1 = exp.indexOf("*");
       index2 = exp.indexOf("/");

       if(index1 <= 0)
       {
        index1 = 100;
       }
        if (index2 <= 0)
       {
        index2 = 100;
       }

        if(index1 < index2)
      {   
        num1 = Integer.parseInt(exp.get(index1-1));
        num2 = Integer.parseInt(exp.get(index1+1));
        answer = num1 * num2;

        exp.remove(index1 - 1);
        exp.remove(index1 - 1);
        exp.set(index1 - 1, Integer.toString(answer));
       }
        else if(index2 < index1)
       {
        num1 = Integer.parseInt(exp.get(index2 - 1));
        num2 = Integer.parseInt(exp.get(index2 + 1));
        answer = num1 / num2;

        exp.remove(index2 -1);
        exp.remove(index2 - 1);
        exp.set(index1 -1 ,Integer.toString(answer));

        while(exp.contains("+") || exp.contains("-"))
        {
           index1 = exp.indexOf("+");
           index2 = exp.indexOf("-");

           if(index1 <= 0)
           {
             index1 = 100;
           }
           if(index2 <= 0)
           {
            index2 = 100;
           }      
            if(index2 < index1)
        {   
         num1 = Integer.parseInt(exp.get(index1-1));
         num2 = Integer.parseInt(exp.get(index1+1));
         answer = num1 + num2;

         exp.remove(index1 - 1);
         exp.remove(index1 - 1);
         exp.set(index1 - 1, Integer.toString(answer));
        }
         else if(index2 < …
Reverend Jim commented: Asked nicely, picked a good title, showed proof of effort. +15