-
FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
Whenever I input fractions I just get back my own error message, (the error message I send as System.err.println("There was an error in your input, try again."); ) or nothing at all. I think it might be a problem with my output code but I'm not sure. I'm not having a program crash error message or system error message. The error message I'm talking about is the one I have set up in case someone does something stupid like 2/3 ++ 1/3.
Code Java:
import java.util.*;
public class Fractioncalculator {
public static void main(String[] args){
boolean quit = false; //boolean for recognition of quit command
boolean error = false;
String inputstr = null;
Scanner sc = new Scanner(System.in);
//Loop for input of problem and output of solution
while(quit==false){
int iterator = 31; //iterator for reduction of end result
int first_whole_int = 0; //first whole integer
int firstnum = 0; //first numerator
int firstdenom = 0; //first denominator
int second_whole_int = 0; //second whole integer
int secondnum = 0; //second numerator
int seconddenom = 0; //second denominator
char operator; //operator storage
int cmndenom = 0; //common denominator
int finalint = 0; //final integer
int finalnum = 0; //final numerator
int finaldenom = 0; //final denominator
//Request for input
System.out.println("Please enter your fractions: ");
inputstr = sc.nextLine();
if(inputstr.equals("quit")){ //check if quit received
quit = true;
}
StringTokenizer errorst = new StringTokenizer(inputstr, " ");
if(errorst.countTokens()!=3){
error = true;
}
else {
StringTokenizer st1 = new StringTokenizer(inputstr, " "); //Tokenize by spaces (First Section, Operator, Second Section)
//
//Section One
//
String first = st1.nextToken();
boolean indwhole = first.contains("/"); //boolean for checking if section one contains a fraction
if(indwhole==false){ //parses first whole number if above is false
first_whole_int = Integer.parseInt(first);
}
boolean indmixed = first.contains("_"); //boolean for checking is section one is a mixed number
if(indmixed==true){ //if above is true, execute parsing of first whole integer,
StringTokenizer st1_sub1 = new StringTokenizer(first, "_"); //first numerator and first denominator.
String first_whole = st1_sub1.nextToken();
first_whole_int = Integer.parseInt(first_whole);
String firstfractionstr = st1_sub1.nextToken();
StringTokenizer st1_sub2 = new StringTokenizer(firstfractionstr, "/");
firstnum = Integer.parseInt(st1_sub2.nextToken());
firstdenom = Integer.parseInt(st1_sub2.nextToken());
}
if(indwhole==true&&indmixed==false){ //if section one is just a fraction, parse numerator and denominator
StringTokenizer st1_sub2 = new StringTokenizer(first, "/ ");
firstnum = Integer.parseInt(st1_sub2.nextToken());
firstdenom = Integer.parseInt(st1_sub2.nextToken());
}
//
//operator
//
String op = st1.nextToken();
operator = op.charAt(0);
//
//Second section
//
String second = st1.nextToken();
boolean indwhole2 = second.contains("/"); //boolean for checking if section two contains a fraction
if(indwhole2==false){ //parses first whole number if above is false
second_whole_int = Integer.parseInt(second);
}
boolean indmixed2 = second.contains("_"); //boolean for checking is section two is a mixed number
if(indmixed2==true){ //if above is true, execute parsing of second whole integer,
StringTokenizer st2_sub1 = new StringTokenizer(second, "_"); //second numerator and second denominator.
String second_whole = st2_sub1.nextToken();
second_whole_int = Integer.parseInt(second_whole);
String secondfractionstr = st2_sub1.nextToken();
StringTokenizer st2_sub2 = new StringTokenizer(secondfractionstr, "/");
secondnum = Integer.parseInt(st2_sub2.nextToken());
seconddenom = Integer.parseInt(st2_sub2.nextToken());
}
if(indwhole2==true&&indmixed2==false){ //if section one is just a fraction, parse numerator and denominator
StringTokenizer st2_sub2 = new StringTokenizer(second, "/ ");
secondnum = Integer.parseInt(st2_sub2.nextToken());
seconddenom = Integer.parseInt(st2_sub2.nextToken());
}
if(operator == '+'){ //function execution for if operator is addition
if(firstdenom!=0 && seconddenom!=0){
cmndenom = firstdenom * seconddenom;
}
if(firstdenom!=0 && seconddenom==0){
cmndenom = firstdenom;
}
if(firstdenom==0 && seconddenom!=0){
cmndenom=seconddenom;
}
finalint = first_whole_int + second_whole_int;
if(firstnum!=0 && secondnum!=0){
finalnum = firstnum*seconddenom + secondnum*firstdenom;
}
if(firstnum==0 && secondnum!=0){
finalnum=secondnum;
}
if(firstnum!=0 && secondnum==0){
finalnum=firstnum;
}
}
if(operator == '-'){
finalint = first_whole_int - second_whole_int;
if(firstdenom!=0 && seconddenom!=0){
cmndenom = firstdenom * seconddenom;
}
if(firstdenom!=0 && seconddenom==0){
cmndenom = firstdenom;
}
if(firstdenom==0 && seconddenom!=0){
cmndenom=seconddenom;
}
if(firstnum!=0 && secondnum!=0){
finalnum = firstnum*seconddenom - secondnum*firstdenom;
}
if(firstnum==0 && secondnum!=0){
finalnum=secondnum;
}
if(firstnum!=0 && secondnum==0){
finalnum=firstnum;
}
}
if(operator == '/'){
if(first_whole_int!=0 && firstdenom!=0){
firstnum=first_whole_int*firstdenom + firstnum;
}
if(first_whole_int!=0 && firstdenom==0){
firstnum=first_whole_int;
firstdenom=1;
}
if(second_whole_int!=0 && seconddenom!=0){
secondnum=second_whole_int*seconddenom + secondnum;
}
if(second_whole_int!=0 && seconddenom==0){
secondnum=second_whole_int;
seconddenom=1;
}
finalnum=firstnum*seconddenom;
cmndenom=firstdenom*secondnum;
}
if(operator=='*'){ //function execution for if operator is multiplication
if(firstdenom==0){
firstnum=first_whole_int;
firstdenom=1;
}
if(firstdenom!=0 && first_whole_int!=0){
firstnum=first_whole_int*firstdenom + firstnum;
}
if(seconddenom==0){
secondnum=second_whole_int;
seconddenom=1;
}
if(seconddenom!=0 && second_whole_int!=0){
secondnum=second_whole_int*seconddenom + secondnum;
}
finalnum=firstnum*secondnum;
cmndenom=firstdenom*seconddenom;
}
finaldenom = cmndenom; //nuance of code, ignore
while(iterator>1){ //reduction of fraction through common dividend
if(finalnum%iterator==0 && finaldenom%iterator==0){
finalnum = finalnum / iterator;
finaldenom = finaldenom / iterator;
iterator--;
}
else{
iterator--;
}
}
while(finalnum>finaldenom){ //reduction of fraction to mixed number
finalnum = finalnum - finaldenom;
finalint++;
}
//Output
//Prints are self-explanatory through blue text, and are only executed if they are possible
//Outputs do not print if an error has occurred
if(error=false){
if(finalint!=0 && finaldenom!=1 && finaldenom!=0 && finalint>0){
System.out.println("Mixed number form: " + finalint + "_" + finalnum + "/" + finaldenom);
}
if(finaldenom==0 && finalnum==0 && finalint!=0){
System.out.println("Final Number: " + finalint);
}
finalnum = finalnum + (finalint*finaldenom);
if(finaldenom!=1 && finaldenom!=0){
System.out.println("Fraction form: " + finalnum + "/" + finaldenom);
}
if(finaldenom==1){
System.out.println("Final Number: " + finalnum);
}
}
else {
System.err.println("There was an error in your input, try again.");
}
System.out.println();
}
if(quit==true){
System.out.println("Program has exited.");
}
}
}
}
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
DUE IN 4 HOURS,
As i am guessing from your name your assignment is due in soon. Now, i'm also going to assume you have probably cross-posted this on many forums in the hope of getting a response. Also, I am going to assume that you have skipped reading the forum rules,
Announcements - What's Wrong With My Code?
Perhaps if you give those a read, and then take the time to provide error messages, an indication of where the code is crashing etc etc we will be able to provide help.
Regards,
Chris
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
REMOVED DUE TO INCORRECT AND STUPID ADVICE BY ME LOL :D
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
Code :
if(finalint!=0 && finaldenom!=1 && finaldenom!=0 && finalint>0)
why are you saying if finalint is NOT 0 AND finalint is greater then 0.. its the same thing unless it goes negative. Try removing one.
yeah, i don't think repetition is causing a problem but i tried deleting it anyway and it didn't fix the problem.
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
take a look at this demo ive just done on console reading / writing:
Code :
public demo() throws Exception{
// GET FIRST NUMBER
Scanner sc = new Scanner(System.in);
System.out.println("Please enter your first fraction: ");
String inputstr = sc.nextLine();
// TESTING IF INPUT ENTERED IS 444
if(inputstr.equals("444")){
System.out.println("Works!");
}
// GET SECOND VALUE
System.out.println("Please enter your second fraction: ");
String second = sc.nextLine();
}
Now i've had a play around with your code, It seems that your not parsing the data that the user is giving u into a variable.
try creating 3 different number variables..
when your scanning the console.. make sure that you put
VARIABLE = sc.readLine();
Because as ive said.. the problem looks like the 3 end int values your trying to process are always staying as 0...
You may also ask for a OPERATOR after every number they have entered... and get that.
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
Correct me if im wrong but.. is this not what you're trying to do only simplified?
Code :
public demo() throws Exception{
// CREATE SCANNER
Scanner sc = new Scanner(System.in);
// GET FIRST NUMBER
System.out.println("Please enter your first number: ");
int first = Integer.parseInt(sc.nextLine());
// GET FIRST OPERATOR
System.out.println("Please enter your first operator: ");
String fOP = sc.nextLine();
// GET SECOND NUMBER
System.out.println("Please enter your second number: ");
int second = Integer.parseInt(sc.nextLine());
// GET SECOND OPERATOR
System.out.println("Please enter your second operator: ");
String sOP = sc.nextLine();
// GET THIRD NUMBER
System.out.println("Please enter your third number: ");
int third = Integer.parseInt(sc.nextLine());
System.out.println("The total value = ");
}
Only thing left for you to do there is to convert the operators so that they can read.. and put the numbers together with the specified operators entered.. Gutta leave something for you to do lol :D
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
Quote:
Originally Posted by
macko
Correct me if im wrong but.. is this not what you're trying to do only simplified?
Code :
public demo() throws Exception{
// CREATE SCANNER
Scanner sc = new Scanner(System.in);
// GET FIRST NUMBER
System.out.println("Please enter your first number: ");
int first = Integer.parseInt(sc.nextLine());
// GET FIRST OPERATOR
System.out.println("Please enter your first operator: ");
String fOP = sc.nextLine();
// GET SECOND NUMBER
System.out.println("Please enter your second number: ");
int second = Integer.parseInt(sc.nextLine());
// GET SECOND OPERATOR
System.out.println("Please enter your second operator: ");
String sOP = sc.nextLine();
// GET THIRD NUMBER
System.out.println("Please enter your third number: ");
int third = Integer.parseInt(sc.nextLine());
System.out.println("The total value = ");
}
Only thing left for you to do there is to convert the operators so that they can read.. and put the numbers together with the specified operators entered.. Gutta leave something for you to do lol :D
no this is a fraction calculator 2/3 + 1/3 + 1_2/3 not an integer calculator 1 + 2 + 3
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
simply change the value of first, second and third to String or w.e ur wanting lol :D I've clearly said that this is for demo only.
As i've said tho try actualy sending the values from the console to a variable.. might help.
I'm not here to complete your homework, I'm simply here to point you in the correct direction... remember that.
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
Quote:
Originally Posted by
macko
take a look at this demo ive just done on console reading / writing:
Code :
public demo() throws Exception{
// GET FIRST NUMBER
Scanner sc = new Scanner(System.in);
System.out.println("Please enter your first fraction: ");
String inputstr = sc.nextLine();
// TESTING IF INPUT ENTERED IS 444
if(inputstr.equals("444")){
System.out.println("Works!");
}
// GET SECOND VALUE
System.out.println("Please enter your second fraction: ");
String second = sc.nextLine();
}
Now i've had a play around with your code, It seems that your not parsing the data that the user is giving u into a variable.
try creating 3 different number variables..
when your scanning the console.. make sure that you put
VARIABLE = sc.readLine();
Because as ive said.. the problem looks like the 3 end int values your trying to process are always staying as 0...
You may also ask for a OPERATOR after every number they have entered... and get that.
I don't think I understand what you are saying. If you could make the changes to my code and post your response that way it would be really helpful to me.
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
Quote:
Originally Posted by
DUE IN 4 HOURS
I don't think I understand what you are saying. If you could make the changes to my code and post your response that way it would be really helpful to me.
Sorry, As i have said:
Quote:
Originally Posted by
macko
I'm not here to complete your homework, I'm simply here to point you in the correct direction... remember that.
You need to actually learn yourself rather then me simply giving the code to you, What will you do once you've finished java? so please follow mine and others guides that we display to you and learn the appropriate skills.
As i've said:
You're problem is:
You are not parsing the entered data into a variable.. A variable that you are trying to calculate that is.. So hence the values of the 3 end variables are null..
Please try doing a
System.out.println(VARIABLE NAME);
under your custom error message.. You will then see the values of the 2 variables are null..
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
I think I've already done what your saying I should do.
int firstnum = 0; //first numerator
firstnum = Integer.parseInt(st1_sub2.nextToken());
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
Quote:
Originally Posted by
macko
Why are you using?
Code :
inputstr = sc.nextLine();
It just looks like you trying to scan a line that isnt there... How can it scan the line if the user has not made input?
I would use:
Code :
String demo = System.console().readLine();
OR
String demo = System.console().reader().read();
Which ever one works lol.. been awhile since ive made console based programs, If im helpful please remember to thank me =/ even though you should be doing your assignment yourself.
O_oinvisiblefiller
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
try saying
int firstnum;
int secondnum;
int thirdnum;
and then simply under your custom error message type
System.out.println(firstnum + " " + secondnum + " " + thirdnum);
You will then see what the values of those numbers are on the console.. if the output posts null that means that i was correct and that you need to listen to my previous advice....
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
Quote:
Originally Posted by
newbie
O_oinvisiblefiller
Forget my crappy first comment, Upon further reading i had discovered the real error obv.. as ive updated ;)
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
int firstnum = 0; //first numerator
firstnum = Integer.parseInt(st1_sub2.nextToken());
this didn't do that?
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
True it's just that when you first declare firstnum = 0...
its hard to do the debugging, Hence if you remove the = 0
Then if the next line doesn't go through properly you can see that firstnum is equal to null...
Otherwise it wills how 0 on the console and you wont know if it had calculated something or not unles you did further debugging.. so the easiest way is doin the above..
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
i added System.out.println(firstnum + " " + secondnum + " " + finalnum); to run after the error message and then input 2/3 + 1/3 which should produce an error because my calculator asks for 3 different numbers/fractions and System.out.println(firstnum + " " + secondnum + " " + finalnum); gave me 2 1 1
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
@DUE IN 4 HOURS: Better turn off the PC or laptop, go outside, have a walk for half an hour and come back with Fresh mind. I actually don't have the link to Kevin's blog, but this suggestion is taken from his blog. So, all credit goes to Kevin. Lol try it. you will solve it.
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
i removed the " = 0" from all my variables and i got this when i ran the program:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The local variable firstdenom may not have been initialized
The local variable seconddenom may not have been initialized
The local variable firstdenom may not have been initialized
The local variable seconddenom may not have been initialized
The local variable firstdenom may not have been initialized
The local variable seconddenom may not have been initialized
The local variable firstdenom may not have been initialized
The local variable firstdenom may not have been initialized
The local variable seconddenom may not have been initialized
The local variable seconddenom may not have been initialized
The local variable first_whole_int may not have been initialized
The local variable second_whole_int may not have been initialized
The local variable firstnum may not have been initialized
The local variable secondnum may not have been initialized
The local variable firstnum may not have been initialized
The local variable seconddenom may not have been initialized
The local variable secondnum may not have been initialized
The local variable firstdenom may not have been initialized
The local variable firstnum may not have been initialized
The local variable secondnum may not have been initialized
The local variable secondnum may not have been initialized
The local variable firstnum may not have been initialized
The local variable secondnum may not have been initialized
The local variable firstnum may not have been initialized
The local variable first_whole_int may not have been initialized
The local variable second_whole_int may not have been initialized
The local variable firstdenom may not have been initialized
The local variable seconddenom may not have been initialized
The local variable firstdenom may not have been initialized
The local variable seconddenom may not have been initialized
The local variable firstdenom may not have been initialized
The local variable seconddenom may not have been initialized
The local variable firstdenom may not have been initialized
The local variable firstdenom may not have been initialized
The local variable seconddenom may not have been initialized
The local variable seconddenom may not have been initialized
The local variable firstnum may not have been initialized
The local variable secondnum may not have been initialized
The local variable firstnum may not have been initialized
The local variable seconddenom may not have been initialized
The local variable secondnum may not have been initialized
The local variable firstdenom may not have been initialized
The local variable firstnum may not have been initialized
The local variable secondnum may not have been initialized
The local variable secondnum may not have been initialized
The local variable firstnum may not have been initialized
The local variable secondnum may not have been initialized
The local variable firstnum may not have been initialized
The local variable first_whole_int may not have been initialized
The local variable firstdenom may not have been initialized
The local variable first_whole_int may not have been initialized
The local variable firstdenom may not have been initialized
The local variable firstnum may not have been initialized
The local variable first_whole_int may not have been initialized
The local variable firstdenom may not have been initialized
The local variable first_whole_int may not have been initialized
The local variable second_whole_int may not have been initialized
The local variable seconddenom may not have been initialized
The local variable second_whole_int may not have been initialized
The local variable seconddenom may not have been initialized
The local variable secondnum may not have been initialized
The local variable second_whole_int may not have been initialized
The local variable seconddenom may not have been initialized
The local variable second_whole_int may not have been initialized
The local variable firstnum may not have been initialized
The local variable seconddenom may not have been initialized
The local variable firstdenom may not have been initialized
The local variable secondnum may not have been initialized
The local variable firstdenom may not have been initialized
The local variable first_whole_int may not have been initialized
The local variable firstdenom may not have been initialized
The local variable first_whole_int may not have been initialized
The local variable first_whole_int may not have been initialized
The local variable firstdenom may not have been initialized
The local variable firstnum may not have been initialized
The local variable seconddenom may not have been initialized
The local variable second_whole_int may not have been initialized
The local variable seconddenom may not have been initialized
The local variable second_whole_int may not have been initialized
The local variable second_whole_int may not have been initialized
The local variable seconddenom may not have been initialized
The local variable secondnum may not have been initialized
The local variable firstnum may not have been initialized
The local variable secondnum may not have been initialized
The local variable firstdenom may not have been initialized
The local variable seconddenom may not have been initialized
The local variable cmndenom may not have been initialized
The local variable finalnum may not have been initialized
The local variable finalnum may not have been initialized
The local variable finalnum may not have been initialized
The local variable finalnum may not have been initialized
The local variable finalint may not have been initialized
The local variable finalint may not have been initialized
The local variable finalint may not have been initialized
The local variable finalnum may not have been initialized
The local variable finalnum may not have been initialized
The local variable finalint may not have been initialized
The local variable finalint may not have been initialized
The local variable finalnum may not have been initialized
The local variable finalint may not have been initialized
at Fractioncalculator.main(Fractioncalculator.java:11 2)
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
There you go... you've got the debugging now.. work out how you would fix that..
As ive said i will not help complete home work.. Now that you know what the outcome is doing.. try changing it and working out why that this is giving you that output.
1 major step in java is the debugging, Maybe its reading the second value twice as you have not entered a third fraction, Try doing a third and seing if the anwser shows of the final..
etc... work it out from there. take your code step by step.
EDIT:
There you go.. as i have been saying for the last 2000 posts lol.. You must initialize the variables.. dont just say = 0; as ull never know if your codes working or not.
Remember before you start all your if statements you've got to write code to initialize all those values..
and LOL @ Mr.777
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
Quote:
Originally Posted by
Mr.777
@DUE IN 4 HOURS: Better turn off the PC or laptop, go outside, have a walk for half an hour and come back with Fresh mind. I actually don't have the link to Kevin's blog, but this suggestion is taken from his blog. So, all credit goes to Kevin. Lol try it. you will solve it.
I wish, this is due in just under two hours and ive got a little more than an hour to work on it. i have no idea what the problem is i think i will have to skip class and get an excuse note somehow the other forums that i posted this on my username is: "imsof*cked"....
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
Can you just read my posts and follow them O_O... nobody here will "give you the code" java can be frustrating at times, We all deal with it.
If what you are doing is beginner java which it looks like it is due to the code.. You're code should really be under 50 lines long..
Your if statements should be else if... and your code is just a mess mate.. as ive said 1000 times follow what ive said.. you'll find the awnser yourself and be proud that you learnt a new trick ;)
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
Quote:
Originally Posted by
Mr.777
@DUE IN 4 HOURS: Better turn off the PC or laptop, go outside, have a walk for half an hour and come back with Fresh mind. I actually don't have the link to Kevin's blog, but this suggestion is taken from his blog. So, all credit goes to Kevin. Lol try it. you will solve it.
Here it is: http://www.javaprogrammingforums.com...e-posting.html
Although that's not really a blog, just an article I wrote containing some advice I was sick of repeating 10 times a day! :p
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
Quote:
Originally Posted by
macko
There you go... you've got the debugging now.. work out how you would fix that..
As ive said i will not help complete home work.. Now that you know what the outcome is doing.. try changing it and working out why that this is giving you that output.
1 major step in java is the debugging, Maybe its reading the second value twice as you have not entered a third fraction, Try doing a third and seing if the anwser shows of the final..
etc... work it out from there. take your code step by step.
EDIT:
There you go.. as i have been saying for the last 2000 posts lol.. You must initialize the variables.. dont just say = 0; as ull never know if your codes working or not.
Remember before you start all your if statements you've got to write code to initialize all those values..
and LOL @ Mr.777
i know how to fix that "bug" you press ctrl z and reinitialize all the variables
-
Re: FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
OP, I highly suggest you stop mentioning your deadline. There are hundreds of posts here, each with its own urgent user waiting on a response, and your time is not more valuable than theirs, or ours for that matter. If you are facing a deadline, I assume your time would be better spent actually reading the responses you've received, trying things out, and following the advice in the article I posted, instead of making more posts complaining about your deadline. Chances are, you're going to have to chalk this one up to a lesson learned about time management.