I wrote a code that reads any arithmetic expression in form of a string and returns the result by printing it.
All of this is done with regard to the order of operation (PEMDAS).
As an example the input, "2+7*5+3" should return 40
But the problem is that the program doesn't return the correct result and I don't know what seems to be the problem.
The Code:
public class LL {
public static int calc(String op, int n1,int n2){
if(op.equals("+")){
return n1+n2;
}
else if(op.equals("-")){
return n1-n2;
}
else if(op.equals("/")){
return n1/n2;
}
else{
return n1*n2;
}
}
public static boolean check(String oldop, String newop){
if(oldop==null||oldop.equals("=")){
return true;
}
else if(oldop.equals("+")||oldop.equals("-")){
if(newop.equals("+")||newop.equals("-")||newop.equals("/")||newop.equals("*")){
return true;
}
else{
return false;
}
}
else{
if(newop.equals("/")||newop.equals("*")){
return true;
}
else{
return false;
}
}
}
public static void main(String[] args) {
int ch=0;
LinkedList<String> op = new LinkedList<String>();
LinkedList<String> nums = new LinkedList<String>();
Scanner sc = new Scanner(System.in);
String exp = sc.nextLine();
String[] exparr = exp.split(" ");
for(int i = 0 ; i<exparr.length-1;i+=2){
nums.push(exparr[i]);
String oldop = op.pop();
if(check(oldop,exparr[i+1])){
if(oldop!=null)
op.push(oldop);
op.push(exparr[i+1]);
}
else{
String n2=nums.pop();
String n1=nums.pop();
int nn2=Integer.parseInt(n2);
int nn1=Integer.parseInt(n1);
int res = calc(oldop,nn1,nn2);
nums.push(Integer.toString(res));
op.push(exparr[i+1]);
}
}
nums.push(exparr[exparr.length-1]);
String opp = op.pop();
while(opp!=null){
String n2=nums.pop();
String n1=nums.pop();
int nn2=Integer.parseInt(n2);
int nn1=Integer.parseInt(n1);
int res = calc(opp,nn1,nn2);
nums.push(Integer.toString(res));
opp = op.pop();
}
System.out.println(nums.pop());
}
}
Your check method is wrong. You must return true if the old operation is + or - and the new operation is * or / otherwise return false.
public static boolean check(String oldop, String newop){
if(oldop == null || oldop.equals("=")){
return true;
}
return (oldop.equals("+") || oldop.equals("-")) && (newop.equals("/") || newop.equals("*"));
}
Another point is, don't use pop method of LinkedList, this method throws an exception if the list is empty, use poll method that return null if the list is empty.
The other code is correct.
I am making a program that will help me convert DFA to a regular expression using an algorithm we learned at the course.
CODE:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Collections;
import java.util.regex.Pattern;
import java.util.Arrays;
import java.io.*;
public class DFK {
static ArrayList<Path> done = new ArrayList<Path>();
public static void print() {
for(Path i: done) {
System.out.println("NOT REPLACED: "+ i);
if(i.isConverted()) {
System.out.println("WITH REPLACE: "+ i.getConverted()+"\n");
} else
System.out.print("\n");
}
}
public static void develop() {
if(done.get(0).getKP1() > 0) {
DFK.add(new Path(done.get(0).getP(), done.get(0).getQ(), done.get(0).getK()));
DFK.add(new Path(done.get(0).getP(), done.get(0).getKP1(), done.get(0).getK()));
DFK.add(new Path(done.get(0).getKP1(), done.get(0).getKP1(), done.get(0).getK()));
DFK.add(new Path(done.get(0).getKP1(), done.get(0).getQ(), done.get(0).getK()));
}
}
public static void add(Path x) {
boolean exists = (done.indexOf(x)==-1 ? false : true);
if(exists == false) {
done.add(x);
if(x.getKP1() >= 2) {
DFK.add(new Path(x.getP(), x.getQ(), x.getK()));
DFK.add(new Path(x.getP(), x.getKP1(), x.getK()));
DFK.add(new Path(x.getKP1(), x.getKP1(), x.getK()));
DFK.add(new Path(x.getKP1(), x.getQ(), x.getK()));
}
}
}
public static void main(String argv[]) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int p = 0, q = 0, kp1 = 0;
Scanner in = new Scanner(System.in);
System.out.print("p = ");p = in.nextInt();
System.out.print("q = ");q = in.nextInt();
System.out.print("k+1 = ");kp1 = in.nextInt();
System.out.print("\n");
String rkzero[][] = new String[q][q];
for(int i=0; i<q ; i++) {
for(int j=0; j<q ; j++) {
System.out.print(String.format("r(%d,%d,0): ", i+1, j+1));
rkzero[i][j]=input.readLine();
}
}
done.add(new Path(p, q, kp1));
DFK.develop();
Collections.sort(done);
for(int z=0; z<q ; z++) {
for(int j=0; j<q ; j++) {
for(Path i: done)
if(i.getKP1()==1) {
String reg = String.format("r(%d,%d,0)",z+1,j+1); //
i.setConverted(i.toString().replace( reg , rkzero[z][j])); //HERE IS THE PROBLEM I THINK
}
}
}
System.out.print("\n");
DFK.print();
}
}
class Path implements Comparable<Path> {
int p,q,kplus1,k;
String converted = null;
public int getP() {
return p;
}
public int getQ() {
return q;
}
public int getKP1() {
return kplus1;
}
public int getK() {
return k;
}
public Path(int a, int b, int c) {
super();
p = a;
q = b;
kplus1 = c;
k = c-1;
}
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!Path.class.isAssignableFrom(obj.getClass())) {
return false;
}
final Path other = (Path) obj;
if(other.p == this.p && other.q == this.q && other.kplus1 == this.kplus1)
return true;
return false;
}
public int compareTo(Path other) {
return other.kplus1-kplus1;
}
public String toString() {
return String.format("r(%d,%d,%d)=r(%d,%d,%d)+r(%d,%d,%d)r(%d,%d,%d)*r(%d,%d,%d)",p,q,kplus1,p,q,k,p,kplus1,k,kplus1,kplus1,k,kplus1,q,k);
}
public void setConverted(String x) {
converted = new String(x);
}
public String getConverted() {
return converted;
}
public boolean isConverted() {
if(converted == null)
return false;
return true;
}
}
CURRENT OUTPUT:
p = 1
q = 2
k+1 = 2
r(1,1,0): 0
r(1,2,0): 1
r(2,1,0): 1
r(2,2,0): 0
NOT REPLACED: r(1,2,2)=r(1,2,1)+r(1,2,1)r(2,2,1)*r(2,2,1)
NOT REPLACED: r(1,2,1)=r(1,2,0)+r(1,1,0)r(1,1,0)*r(1,2,0)
WITH REPLACE: r(1,2,1)=r(1,2,0)+r(1,1,0)r(1,1,0)*r(1,2,0)
NOT REPLACED: r(2,2,1)=r(2,2,0)+r(2,1,0)r(1,1,0)*r(1,2,0)
WITH REPLACE: r(2,2,1)=0+r(2,1,0)r(1,1,0)*r(1,2,0)
WANTED OUTPUT:
p = 1
q = 2
k+1 = 2
r(1,1,0): 0
r(1,2,0): 1
r(2,1,0): 1
r(2,2,0): 0
NOT REPLACED: r(1,2,2)=r(1,2,1)+r(1,2,1)r(2,2,1)*r(2,2,1)
NOT REPLACED: r(1,2,1)=r(1,2,0)+r(1,1,0)r(1,1,0)*r(1,2,0)
WITH REPLACE: r(1,2,1)=1+00*1
NOT REPLACED: r(2,2,1)=r(2,2,0)+r(2,1,0)r(1,1,0)*r(1,2,0)
WITH REPLACE: r(2,2,1)=0+10*1
As you can see it only replaces r(2,2,0) with 0, and nothing else and I cant find out why. I replicated the problem in a previous question, but it turned out there was a non-ascii comma in my code there which caused the issue, but here I can't find it.
I used grep --color='auto' -P -n "[^\x00-\x7F]" DFK.java and it didn't find any non ascii characters, so I hope it's not the same problem.
The program should iterate over all Paths that have kplus1 = 1 and replace all r(i,j,0) with strings I have previously entered. Keep in mind, it is not finished yet.
The mistake is I was using toString to get the string in which I replace things, and so I replace one string and for the next replacement I use the original string again and thus throwing away the last replacement and so on.
So the solution is to make converted initialize as the same string as toString() inside constructor:
converted = String.format("r(%d,%d,%d)=r(%d,%d,%d)+r(%d,%d,%d)r(%d,%d,%d)*r(%d,%d,%d)",p,q,kplus1,p,q,k,p,kplus1,k,kplus1,kplus1,k,kplus1,q,k);
and then in main use:
i.setConverted( i.getConverted().replace(reg, rkzero[z][j]) );
so I save the intermediate solution in converted.
Im having a class Woning (house) and a subclass KoopWoning (buyable House) and a subclass HuurWoning (rentable House). KoopWoning and Huurwoning extend Woning. HuurWoning is just a Woning, whereas KoopWoning has an extra variable energylevel. KoopWoning has also a function getEnergylevel, which returns the energylevel of the KoopWoning. I also have a class Portefeuille which has an arraylist of Woningen.
Im reading all Woningen in a Portefeuille from a textfile. In a 5th class, I want to be able to sort the ArrayList of Woningen of Portefeuille (from the textfile). I have a function woningenTot(int maxprijs) which returns an ArrayList with all the Woningen that fullfil the requirement (having a price below maxprijs). These Woningen I want to print on the screen.
The problem is as follows:
It can be possible that there is also a KoopWoning in the file. In that case I also want to be able to sort on energylevel. However, I can't sort on the energylevels. I can't call the function getEnergylevel because it's an ArrayList, and Woning doesn't contain the function getEnergylevel.
So how can I solve this? If it's too vague, I could include the code, however it's quite big :O
Any help is appreciated; i have spent a couple of hours on this program, from which at least 1.5 hours on this problem alone :(
EDIT: Here is the code for class KoopWoning
public class KoopWoning extends Woning implements EnergiepeilWoning {
private char energiepeil;
public KoopWoning (Adres adres, int kamers, int vraagPrijs, char energiepeil) {
super(adres, kamers, vraagPrijs);
this.energiepeil = energiepeil;
}
public char getEnergiepeil () {
return energiepeil;
}
public boolean compareEnergiepeil (Object other) {
boolean res = false;
if (other instanceof KoopWoning) {
KoopWoning that = (KoopWoning) other;
res = (this.getEnergiepeil() == that.getEnergiepeil());
}
return res;
}
public String toString () {
String res = adres + ", " + kamers + " kamers, prijs " + prijs + ", energiepeil " + energiepeil;
return res;
}
And here is the code for class Woning
public class Woning {
protected int kamers;
protected int prijs;
protected Adres adres;
protected String tag;
public Woning (Adres adres, int kamers, int prijs) {
this.adres = adres;
this.kamers = kamers;
this.prijs = prijs;
}
public String toString () {
String res = adres + ", " + kamers + " kamers, prijs " + prijs;
return res;
}
public void setTag (String tag) {
this.tag = tag;
}
public String getTag () {
return tag;
}
public boolean kostHooguit (int maxprijs) {
return (prijs <= maxprijs);
}
public boolean equals (Object other) {
boolean res = false;
if (other instanceof Woning) {
Woning that = (Woning) other;
if (this.adres.equals(that.adres))
res = true;
}
return res;
}
public static Woning read (Scanner sc) {
try {
Adres adress = Adres.read(sc);
int kamer = sc.nextInt();
sc.next();
sc.next();
int prijs = sc.nextInt();
String check = sc.next();
if (check.equals("energiepeil")) {
char peil = sc.next().charAt(0);
KoopWoning kwoning = new KoopWoning (adress, kamer, prijs, peil);
return kwoning;
}
else {
Woning woning = new Woning (adress, kamer, prijs);
return woning;
}
}
catch (Exception e) {
System.out.println("Woning: Exception is caught");
System.out.println(e.getMessage());
Adres adress = new Adres ("", "", "", "");
Woning woning = new Woning (adress, 0, 0);
return woning;
}
}
}
And lastly, the code for the class Portefeuille
public class Portefeuille {
private ArrayList<Woning> woninglijst;
public Portefeuille () {
woninglijst = new ArrayList<Woning>();
}
public void voegToe (Woning woning) {
if (!woninglijst.contains(woning))
woninglijst.add(woning);
}
public ArrayList<Woning> woningenTot (int maxprijs) {
ArrayList<Woning> woninglijst2 = new ArrayList<Woning>();
for (int i = 0; i < woninglijst.size(); i++) {
if(woninglijst.get(i).kostHooguit(maxprijs))
woninglijst2.add(woninglijst.get(i));
}
return woninglijst2;
}
public String toStringExt () {
String res = "[";
for (int i = 0; i < woninglijst.size(); i++)
res = res + woninglijst.get(i).toString() + "; ";
if (woninglijst.size() != 0)
res = res.substring (0, res.length() - 2);
res = res + "]";
return res;
}
public String toString () {
String res = "";
for (int i = 0; i < woninglijst.size(); i++)
res = woninglijst.get(i).toString2();
return res;
}
public boolean equals (Object other) {
boolean res = false;
if (other instanceof Portefeuille) {
Portefeuille that = (Portefeuille) other;
if (this.woninglijst.size() == that.woninglijst.size()) {
int i = 0;
while (i < this.woninglijst.size() && this.woninglijst.get(i).equals(that.woninglijst.get(i)))
i = i + 1;
res = (i == this.woninglijst.size());
}
}
return res;
}
public static Portefeuille read (String infile) {
try {
Scanner sc = new Scanner (new File(infile));
ArrayList<Woning> wlijst = new ArrayList<Woning>();
Portefeuille p = new Portefeuille();
int woningen = sc.nextInt();
int i = 0;
while (i < woningen) {
sc.nextLine();
String tag = sc.nextLine();
wlijst.add(Woning.read(sc));
p.voegToe(wlijst.get(i));
i++;
}
sc.close();
return p;
}
catch (Exception e) {
System.out.println("Portefeuille: Exception is caught");
Portefeuille p = new Portefeuille();
return p;
}
}
}
EDIT
I fixed it myself. Thanks for answering you all :)
You could define, on the top-level class, a method like getSortableValue(), and implement it to return a default field (you didn't mention the field you need to sort on for Woningen). In the KoopWoning you override this method to return the energyLevel instead. Then you always sort on the value returned by getSortableValue().
You can let the them implement Comparable, so like Woning implements Comparable<Woning>. This will let you implement the (required) method:
#override
public int compareTo(Woning other) {
int result = Integer.compareto(maxPrijs, other.maxPrijs);
if (result != 0) return result;
result = Integer.compareto(someField, other.someField);
if (result != 0) return result;
// etc...
return 0;
}
The subclass KoopWoning extends Woning implements Comparable<KoopWoning> can have a method like this:
#override
public int compareTo(KoopWoning other) {
int result = Integer.compareto(energylevel, other.energylevel);
if (result != 0) return result;
return super.compareTo(other);
}
Then all you need to do is load all the Woning instances in a list and execute
Collections.sort(list);
Having subclasses inherit Comparable is optional, so HuurWoning will just sort like Woning.
You could define a Comparator on Woning that determines the relative ordering of two Woning. You could do this either by having a method that looks at the actual types of the two arguments and then acts appropriately, or, better, by having an overrideable method of Woning that returns some value that you can use for sorting purposes.
If, for instance, you decide that anything with an energy level should come after anything without one, then you can have KoopWoning return something with the energy level in the high order bits of a long, so that it always comes out higher than anything without one (essentially you'd be setting a default energy level of zero).
Then, you can use
Collections.sort(arrayList, myComparator);
to sort the list based on the Comparator you've created.
There are some nice classes in the Guava library that help with Comparator building on multiple keys, but if your case is fairly simple, you probably won't need them.
I'm creating a program that has a requirement of three classes. The program reads an external text file filled with fractions, and is to return how many times each fraction is repeated. 4/2 has to be reduced to 2/1 and is +1 for the 2/1 count. I believe I am almost done, but I cannot figure out what I need to put into my compareAndIncrement() method in my FractionCounter class. It is suppose to be used to see if the newFraction passed into the function is the same as the Fraction being stored, and if so increments the counter by one and returns true (otherwise, returns false). Below are the codes for my classes.
FractionCounter
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FractionCounter {
private Fraction theFraction;
private int counter = 0;
public FractionCounter(Fraction theFraction ){
}
public boolean compareAndIncrement(Fraction newFraction){
return false;
}
public String toString(){
return "";
}
public static void main(String[] args){
ObjectList num = new ObjectList();
ObjectList den = new ObjectList();
Scanner fractionFile = null;
try{
fractionFile = new Scanner(new FileInputStream("fractions.txt"));
}
catch (FileNotFoundException e){
System.out.println("File not found.");
System.exit(0);
}
while (fractionFile.hasNextLine()){
String[] part = (fractionFile.nextLine().split("/"));
num.add(Integer.parseInt(part[0]));
den.add(Integer.parseInt(part[1]));
}
}
}
Fraction
public class Fraction {
private int numerator;
private int denominator;
public Fraction() {
}
public Fraction(int num, int den) {
setNumerator(num);
setDenominator(den);
}
public void setNumerator(int num) { //sets numerator
numerator = num;
}
public int getNumerator() { //gets numerator
return numerator;
}
public void setDenominator(int den) { //sets denominator
if(den == 0) {
System.out.println("Error: Denominator = 0");
System.exit(0);
} else {
denominator = den;
}
}
public int getDenominator() { //gets denominator
return denominator;
}
public boolean equals(Fraction that) {
return ((double)this.numerator/this.denominator) == ((double)that.numerator/that.denominator);
}
}
ObjectList
public class ObjectList {
private int[] fraction = new int[100];
private int numElements = 0;
public void add(int n){
fraction[numElements] = n;
numElements++;
}
public String toString(){
String retVal = "";
for (int i = 0; i < numElements; i++){
retVal += fraction[i] + ",";
}
return retVal;
}
public int indexOf(int[] input, int target) {
//returns the index of the inputed value
if(contains(input,target) == true){
for(int i = 0;i <= target;i++) {
if(input[i] == target) {
return i;
}
}
}
return -1;
}
public boolean contains(int[] input, int target) {
//is the target in the inputed array?
for(int i=0;i<input.length; i++) {
if(input[i] == target) {
return true;
}
}
return false;
}
}
Any hints or tips for what I need to do to my method would be much appreciated. I can't figure out a way to do it without using numElements and fraction variables from my ObjectList class. Thank you
I would make a Map to make the counter :
private static final Map<Fraction, Integer> counter = new HashMap<Fraction, Integer>();
and for each Fraction element read for the file I would do :
if(counter.containsKey(fraction)){
Integer count = counter.get(fraction);
count++;
counter.put(fraction, count);
} else {
counter.put(fraction, 1);
}
Moreover, I would make a static parse fonction in the Fraction class which return a Fraction instance from the line you just read. And a toString function to print it easely.
I am having some difficulties getting my custom equation evaluator to work. I pass it a string read from a text file (no spaces except between string words) as equation as well as passing it a map of keywords which link to the values they represent. I have tested that and all of my maps are working properly. Below is my attempt to handle the result regardless of it is an int or a string. These will be the only two allowed entry types. Each side of the equation can have one or two elements to it, separated by either a plus or a minus. The only three operators allowed to evaluate the two sides are <,>,=. Sides are restricted to either having only keywords or only integers, so you can't have something like dexterity + 1 = strength + 2.
The error I am currently getting when I try to compile this class is "no suitable method found for parseint" "method Integer.parseInt(String,int) is not applicable". If I am not mistaken since I am compiling this class directly and not the main class it wouldn't even have the map to make that kind of judgement call. Is this a problem? I am compiling in this way because I have been having issues where recompiling the main class did not recompile secondary class files causing problems.
Any example equation: dexterity>3 or background=Ex Legionary
import java.lang.String;
import java.util.HashMap;
import java.util.Map;
public class Equation {
private String[] sides = new String[2];
private String[] rawEquation = new String[3];
private String[] parts = new String[2];
private String type;
private int[] tempInt = new int[2];
private int[] finalSide = new int[2];
private String[] finalStride = new String[2];
public boolean solve(String equation, Map gladMap) {
if (equation.indexOf("<") > -1) {
sides = equation.split("<");
rawEquation[1] = "<";
} else if (equation.indexOf(">") > -1) {
sides = equation.split(">");
rawEquation[1] = ">";
} else if (equation.indexOf("=") > -1) {
sides = equation.split("=");
rawEquation[1] = "=";
}
rawEquation[0] = sides[0];
rawEquation[2] = sides[1];
for (int d = 0; d < 2; d++) {
if (sides[d].indexOf("+") > -1) {
parts = rawEquation[0].split("\\+");
for (int a = 0; a < 2; a++) {
if (isInteger(parts[a])){
tempInt[a] = Integer.parseInt(parts[a]);
} else {
tempInt[a] = Integer.parseInt(gladMap.get(parts[a]));
}
}
finalSide[d] = tempInt[0]+tempInt[1];
type = "Int";
} else if (rawEquation[0].indexOf("-") > -1) {
parts = rawEquation[0].split("\\-");
for (int a = 0; a < 2; a++) {
if (isInteger(parts[a])){
tempInt[a] = Integer.parseInt(parts[a]);
} else {
tempInt[a] = Integer.parseInt(gladMap.get(parts[a]));
}
}
finalSide[d] = tempInt[0]-tempInt[1];
type = "Int";
} else {
if (isInteger(sides[0])){
finalSide[d] = Integer.parseInt(sides[0]);
} else {
if (isInteger(gladMap.get(sides[0]))) {
finalSide[d] = Integer.parseInt(gladMap.get(sides[0]));
type = "Int";
} else {
finalStride[d] = gladMap.get(sides[0]);
type = "Str";
}
}
}
}
if (rawEquation[1].equals("<")) {
if (type.equals("Int")) {
if (finalSide[0] < finalSide[1]) {
return true;
}
}
} else if (rawEquation[1].equals(">")) {
if (type.equals("Int")) {
if (finalSide[0] > finalSide[1]) {
return true;
}
}
} else {
if (type.equals("Int")) {
if (finalSide[0] == finalSide[1]) {
return true;
}
} else if (type.equals("Str")) {
if (finalStride[0].equals(finalStride[1])) {
return true;
}
}
}
return false;
}
public boolean isInteger( String input ) {
try {
Integer.parseInt( input );
return true;
}
catch( Exception NumberFormatException ) {
return false;
}
}
}
I tried to separate the Integer.parseInt() from the gladMap.get(sides[0]) by creating a temporary string variable, but it didn't change anything. Any help would be appreciated!
Here, the map which you are passing is not with the generic types. Hence, get() will always return an object, which is not an appropriate argument for parseInt() method.
Changing the method signature to
public boolean solve(String equation, Map< String ,String > gladMap) {
should solve the errors.
The problem might be following: your map is untyped so calls like gladMap.get(sides[0]) return Object. Integer.parseInt expects String. You can change it to
gladMap.get(sides[0]).toString().
It think it should work. If value is actual String then toString will return itself, it it is Integer it will be converted to string and parsed back.