• Post Reply Bookmark Topic Watch Topic
  • New Topic

I cannot get the right input for my feet and inches program  RSS feed

 
seph Sel
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for trying to help me.

The program that I am working on is supposed to ask a person for their input in feet and inches

2' 2 2/2" <--- (This format)

However, I cannot come up with a way that enables such kind of user input.

The program is composed of three classes

Fraction

Pair

Mix



I have managed to get result after inputting

2' 2 2/2"1

I don't know what I am doing wrong, but something is off since i need a number after the " to get a result back.

I am pretty sure that something is wrong with the parsing method, i just dont know where it is.

This is the Fraction class




This is the third class

 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly are you trying to accomplish? You told us what input you are supposed to accept, but you didn't tell us what you want to do with it.

Also, welcome!
 
seph Sel
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Christophel wrote:What exactly are you trying to accomplish? You told us what input you are supposed to accept, but you didn't tell us what you want to do with it.

Also, welcome!


Hello,
What i want is to input

2' 2 2/2"

Without the need of the 1 that comes after "

I don't know how to fix that part of the program.
What I want is for the program to accept this input

2' 2 2/2"

however, in its current state if i were to do that, I would receive an error.

But if I put a 1 after " I no longer receive the error and the program continues.
That 1 is not supposed to be entered for the program to work.
This is were i get lost, since i dont know how to fix that issue.


 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

That looks like something which can be done by guesswork; after 1000000 guesses (all different) you get one which actually works.
Or you can work it out once and get something which works at the first attempt. Start by turning your PC off. Then write down on paper what the different inputs mean. What is 2′ 2 2/2″? It does not look like a standard feet‑and‑inches format. Write down how you would explain to a 6‑year‑old how you convert that String into feet and inches, as I had to be able to do as a 6‑year‑old.
Once you have done that, you will have some chance of getting that String into a format to analyse feet and inches.
 
seph Sel
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your response,
Yeah, guesswork will be my main ally on this issue.

Although i will plan on how to create that string as you explained.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seph Sel wrote:
Hello,
What i want is to input

2' 2 2/2"

Without the need of the 1 that comes after "

I don't know how to fix that part of the program.
What I want is for the program to accept this input

2' 2 2/2"

however, in its current state if i were to do that, I would receive an error.

But if I put a 1 after " I no longer receive the error and the program continues.
That 1 is not supposed to be entered for the program to work.
This is were i get lost, since i dont know how to fix that issue.




You still haven't told me what you are trying to do with the input.



The above code successfully accepts the input 2' 2 2/2" without error. I'm assuming, however, that you want your program to do more than accept input.
 
seph Sel
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You still haven't told me what you are trying to do with the input.



The above code successfully accepts the input 2' 2 2/2" without error. I'm assuming, however, that you want your program to do more than accept input.


I apologize,

Once the input of 2' 2 2/2" has been accepted by the program, I wish to convert it into
feet and inches, such as
2' 2 2/2" ----> 2' 3"

The program asks for a second number.
lets assume it is the same number of 2' 2 2/2"
2' 2 2/2" ----> 2' 3"

Once the two results have been obtained the program will add them together and
display the result like this
The sum = 4' 6"

Now, I know how to do the addition part and the conversion part, However I am still stuck at
trying to get the program to accept
2' 2 2/2"
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seph Sel wrote:
I apologize,

Once the input of 2' 2 2/2" has been accepted by the program, I wish to convert it into
feet and inches, such as
2' 2 2/2" ----> 2' 3"

The program asks for a second number.
lets assume it is the same number of 2' 2 2/2"
2' 2 2/2" ----> 2' 3"

Once the two results have been obtained the program will add them together and
display the result like this
The sum = 4' 6"

Now, I know how to do the addition part and the conversion part, However I am still stuck at
trying to get the program to accept
2' 2 2/2"


Thanks for explaining. My second question is: why do you have more than one main method in one program? You generally should only have one main method per program.
 
seph Sel
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Thanks for explaining. My second question is: why do you have more than one main method in one program? Only one main method runs during the life of a program.


I have more than one method?
I thought I only had one main method there.

I composed the public main method to call for the displays of the first, second numbers and also the addition of the both.

I know this may sound silly, but can you show me where i have more than one method in the program?
I have around three weeks learning the language, and I am eager to learn it.

(out of curiosity)
If i had more than one method in the program, wouldnt the program just not run, or even compile?
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seph Sel wrote:
I have more than one method?
I thought I only had one main method there.

I composed the public main method to call for the displays of the first, second numbers and also the addition of the both.

I know this may sound silly, but can you show me where i have more than one method in the program?
I have around three weeks learning the language, and I am eager to learn it.


You have a main method in the Mix class and in the Pair class. Which method are you really trying to run your program with?

seph Sel wrote:(out of curiosity)
If i had more than one method in the program, wouldnt the program just not run, or even compile?

You can have one main method per class without upsetting the compiler. However, the main method from the class that you start your program with is the one that will execute.
 
seph Sel
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You have a main method in the Fraction class and in the Pair class. Which method are you really trying to run your program with?

The methods in the Fraction and Pair class do not matter, the one that really matters is the Mix class.

The Mix class extends into the Fraction class, and the Fraction class extends into the Pair class.

I could definitely put it all in one class, but the amount of code would increase by a lot.

But, the one that is meant to be ran in the compiler is the Mix class.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seph Sel wrote:
The Mix class extends into the Fraction class, and the Fraction class extends into the Pair class.
I could definitely put it all in one class, but the amount of code would increase by a lot.


The extends keyword is the same as saying is a. In other words, "Vanilla extends Flavor" means "Vanilla is a Flavor". Is a Mix a Fraction, and is a Fraction a Pair? Explain to me why you are using the keyword extends.

seph Sel wrote:But, the one that is meant to be ran in the compiler is the Mix class.

So if you want the program to start at the main method in the Mix class, you can remove the other main method (unless you use it for testing).
 
seph Sel
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The extends keyword is the same as saying is a. In other words, "Vanilla extends Flavor" means "Vanilla is a Flavor". Is a Mix a Fraction, and is a Fraction a Pair? Explain to me why you are using the keyword extends.


So if you want the program to start at the main method in the Mix class, you can remove the other main method (unless you use it for testing).

Since I am new to the language, I wanted to practice by using three classes and linking them with the extends keyword.
I was using the keyword extends as a way to test that concept.

Indeed, I could delete the main methods of the Fraction and Pair classes.
but i keep them for testing.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seph Sel wrote:
Since I am new to the language, I wanted to practice by using three classes and linking them with the extends keyword.
I was using the keyword extends as a way to test that concept.

What do you mean by linking them? It is generally a very good practice to break your program into different classes instead of having everything in one file, so kudos to you for that. However, you can use all your classes in conjunction without having them extend each other.
 
seph Sel
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[quote=Joel Christophel
What do you mean by linking them? You can use all three classes in conjunction without having them extend each other.

I did not know that. haha. thanks.
So, what do you recommend i do to fix my issue with the input.

just try my luck and guess until i get the input that i want?
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seph Sel wrote:
I did not know that. haha. thanks.
So, what do you recommend i do to fix my issue with the input.

just try my luck and guess until i get the input that i want?


To be honest, I haven't actually looked at your actual code yet because I wanted to get to the bottom of the multiple main methods and your use of extends. Now I'll actually take a look at your code.
 
seph Sel
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be honest, I haven't actually looked at your actual code yet because I wanted to get to the bottom of the multiple main methods and your use of extends. Now I'll actually take a look at your code.


Thanks for all the help.
I would really appreciate it if you looked over the code and
maybe you can solve the issue that has stopped me for quite some hours.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seph Sel wrote:
Thanks for all the help.
I would really appreciate it if you looked over the code and
maybe you can solve the issue that has stopped me for quite some hours.

Well, I'm not going to solve it for you, but hopefully I can lead you in the right direction.

1. A great way to debug a program is to add print statements throughout your code to see what's happening behind the scenes. I've rewritten your parse method in your Mix class with print statements for debugging. It should become clear why you're getting an error when trying to parse an integer from sTop.



2. Also, try inputs other than 2' 2 2/2. Upon inputting 2' 2 1/2", for example, you will see that sA[2] is not what it should be.
 
seph Sel
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.
I learned something new today.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seph Sel wrote:Thank you very much.
I learned something new today.


You're welcome. Feel free to post back here if you have more questions.

One suggestion I have is to vary your test input. In this case, I would be sure to include test input that has double-digit numbers (e.g. 2' 10 1/2").
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!