Jump to content

[JAVA] Can't compile code, don't know how to fix error

Go to solution Solved by madknight3,

Thanks man, but how did you fix them up. As you can probably tell by my not so helpful posts, this isn't actually something I created myself. My skills are web dev based. So how did you fix them?

 

I'm not sure if this is what was intended, but here's how I'd change it so it makes sense to me. The only changes are at the bottom of the method and I made comments about them. Note this still assumes you use JDK 7+ for issue #2 I mentioned. If you don't have it, you'll have to install and use it. 

    private static String produceAnswer(String input) {        // parse input        String t[] = input.split(" ");        if (t.length != 3) return "Check format!";        MixedFraction a = parseFraction(t[0]);        a.check();        String operator = t[1];        MixedFraction b = parseFraction(t[2]);        b.check();        MixedFraction result;        //addition        switch(operator) {            case "+":                result = a.add(b);                break;            case "-":                result = a.subtract(b);                break;            case "*":                result = a.multiply(b);                break;            case "/":                result = a.divide(b);                break;            default:                //return a + " " + operator + " " + b + " = " + result;                return "Invalid operation"; // I don't know what this was supposed to do so I changed it        }        return result.toString(); // Returned a result here    }

Hey, it's not like me to post topics about software problems, but I don't have enough time to figure this out on my own

 

The code is for a fractions calculator and it appears to be throwing a system error.. I don't know what the exact error is I'm afraid

 

 

See THIS for more information. What is the easiest way to fix this?

package fracCalc; import java.util.Scanner; public class fraction { 	public static void main(String[] args) {		Scanner scanner = new Scanner(System.in); 		System.out.print("Enter a fraction: ");		String input = scanner.nextLine(); 		// loop until user enters 'quit'		while (!input.equalsIgnoreCase("quit")) {			System.out.println(produceAnswer(input));			System.out.print("Enter a fraction: ");			input = scanner.nextLine();		} 		System.out.println("Goodbye!");		scanner.close(); 	} 	private static String produceAnswer(String input) { 		// parse input		String t[] = input.split(" ");		if (t.length != 3) return "Check format!";		MixedFraction a = parseFraction(t[0]);		a.check();		String operator = t[1];		MixedFraction b = parseFraction(t[2]);		b.check(); 		MixedFraction result; 		//addition		switch(operator) {			case "+":				result = a.add(b);				break;			case "-":				result = a.subtract(b);				break;			case "*":				result = a.multiply(b);				break;			case "/":				result = a.divide(b);				break;			default:				return a + " " + operator + " " + b + " = " + result;		} 	} //parse	private static MixedFraction parseFraction(String fraction) { 		MixedFraction mf = new MixedFraction(); 		String[] t;		if (fraction.contains("_")) {			t = fraction.split("_");			mf.number = Integer.parseInt(t[0]);			fraction = t[1];		}		else mf.number = 0; 		t = fraction.split("/");		mf.num = Integer.parseInt(t[0]);		mf.denom = Integer.parseInt(t[1]);		return mf;	} } class MixedFraction {	public int number, num, denom; 	public MixedFraction() {		number = 0;		num = 0;		denom = 0;	} 	private int gcd(int a, int b) {		while (b != 0) {			int t = b;			b = a % b;			a = t;		}		return a;	} 	private int lcm(int a, int b) {		return Math.abs(a * b) / gcd(a, b);	} 	public void check() {		if (this.num <= this.denom) return;		this.number += this.num / this.denom;		this.num = this.num % this.denom;	}	public String toString() {		return (number != 0 ? number + "_" : "") + num + "/" + denom;	} //adding the fractions	public MixedFraction add(MixedFraction b) {		MixedFraction mf = new MixedFraction();		int a_num = this.num + this.number * this.denom;		int b_num = b.num + b.number * b.denom;		int lcm = lcm(this.denom, b.denom);		a_num *= lcm / this.denom;		b_num *= lcm / b.denom;		mf.num = a_num + b_num;		mf.denom = lcm;		mf.check();		return mf;	} 	//multiply the fractions	public MixedFraction multiply(MixedFraction b) {		MixedFraction mf = new MixedFraction();		int a_num = this.num * this.number * this.denom;		int b_num = b.num * b.number * b.denom;		int lcm = lcm(this.denom, b.denom);		a_num *= lcm / this.denom;		b_num *= lcm / b.denom;		mf.num = a_num * b_num;		mf.denom = lcm;		mf.check();		return mf;	} 	//subtract the fractions	public MixedFraction subtract(MixedFraction b) {		MixedFraction mf = new MixedFraction();		int a_num = this.num - this.number * this.denom;		int b_num = b.num - b.number - b.denom;		int lcm = lcm(this.denom, b.denom);		a_num *= lcm / this.denom;		b_num *= lcm / b.denom;		mf.num = a_num - b_num;		mf.denom = lcm;		mf.check();		return mf;	}	//divide the fractions	public MixedFraction divide(MixedFraction b) {		MixedFraction mf = new MixedFraction();		int a_num = this.num - this.number * this.denom;		int b_num = b.num / b.number / b.denom;		int lcm = lcm(this.denom, b.denom);		a_num *= lcm / this.denom;		b_num *= lcm / b.denom;		mf.num = a_num / b_num;		mf.denom = lcm;		mf.check();		return mf;}} 

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to post
Share on other sites

I had 3 issues when trying to compile your code. Once they were all fixed, it compiled fine for me.

 

1. No return statement in

private static String produceAnswer(String input)

2. A string in a switch statement is only acceptable in JDK 7+. It's probably not your issue but thought I'd mention it anyway.

 

3. Variable 'result' may not be initialized in

default:    return a + " " + operator + " " + b + " = " + result;
Link to post
Share on other sites

post the error?!

stdout 

Standard output is empty?

Is that what you need, like I said, I don't really know what the exact error is and I'm trying to remember how to set up eclipse to test this stuff

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to post
Share on other sites

 

I had 3 issues when trying to compile your code. Once they were all fixed, it compiled fine for me.

 

1. No return statement in

private static String produceAnswer(String input)

2. A string in a switch statement is only acceptable in JDK 7+. It's probably not your issue but thought I'd mention it anyway.

 

3. Variable 'result' may not be initialized in

default:    return a + " " + operator + " " + b + " = " + result;

 

Thanks man, but how did you fix them up. As you can probably tell by my not so helpful posts, this isn't actually something I created myself. My skills are web dev based. So how did you fix them?

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to post
Share on other sites

Thanks man, but how did you fix them up. As you can probably tell by my not so helpful posts, this isn't actually something I created myself. My skills are web dev based. So how did you fix them?

 

I'm not sure if this is what was intended, but here's how I'd change it so it makes sense to me. The only changes are at the bottom of the method and I made comments about them. Note this still assumes you use JDK 7+ for issue #2 I mentioned. If you don't have it, you'll have to install and use it. 

    private static String produceAnswer(String input) {        // parse input        String t[] = input.split(" ");        if (t.length != 3) return "Check format!";        MixedFraction a = parseFraction(t[0]);        a.check();        String operator = t[1];        MixedFraction b = parseFraction(t[2]);        b.check();        MixedFraction result;        //addition        switch(operator) {            case "+":                result = a.add(b);                break;            case "-":                result = a.subtract(b);                break;            case "*":                result = a.multiply(b);                break;            case "/":                result = a.divide(b);                break;            default:                //return a + " " + operator + " " + b + " = " + result;                return "Invalid operation"; // I don't know what this was supposed to do so I changed it        }        return result.toString(); // Returned a result here    }
Link to post
Share on other sites

 

I'm not sure if this is what was intended, but here's how I'd change it so it makes sense to me. The only changes are at the bottom of the method and I made comments about them. Note this still assumes you use JDK 7+ for issue #2 I mentioned. If you don't have it, you'll have to install and use it. 

    private static String produceAnswer(String input) {        // parse input        String t[] = input.split(" ");        if (t.length != 3) return "Check format!";        MixedFraction a = parseFraction(t[0]);        a.check();        String operator = t[1];        MixedFraction b = parseFraction(t[2]);        b.check();        MixedFraction result;        //addition        switch(operator) {            case "+":                result = a.add(b);                break;            case "-":                result = a.subtract(b);                break;            case "*":                result = a.multiply(b);                break;            case "/":                result = a.divide(b);                break;            default:                //return a + " " + operator + " " + b + " = " + result;                return "Invalid operation"; // I don't know what this was supposed to do so I changed it        }        return result.toString(); // Returned a result here    }

I still got an error when trying to run

"Could not find or load main class fraction" Any ideas?

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×