programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Need Help With a Java Practice Problem

Graham Wolk
Ranch Hand
Posts: 31
I am now doing a practice problem on my teacher's textbook page. The question:

"A marathon is a foot race of 26 miles and 385 yards. The best way to run a marathon is to run at the same pace for the entire race. A runner measures his or her pace in minutes and seconds per mile. Most runners in a marathon are attempting to meet a target time, expressed in hours and minutes. For example, a runner might want to complete a marathon in three hours and twenty minutes (3:20). To do this, she would need to run each mile in 7 minutes and 38 seconds (7:38).

Write a program that accepts as input the target time in the form H:MM (one digit for hours and two digits for minutes, separated by a colon) and outputs the mile pace needed in the form M:SS (one digit for minutes and two digits for seconds)."

The sample context that should be the format when the program is run is :

Enter marathon target time: 3:00
Mile pace: 6:52

Now that I have the question given, I will show you guys what I've done so far:

I know that I need to convert the marathon total time (hours per minute) into the minutes per mile. I would have to divide the marathon time by 60 for each hour. Now the thing I'm having trouble with is how to put that into NetBeans. If there is no single int variable declared, how can I create a sort of 'universal' arithmetic operation in order to have whatever I input always be divided by the conversion factor?

Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152

I've gone ahead and added the code tags for you. See how much easier the code is to read? (And it'd be even more so with proper indentation.)

Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
I'm not sure I understand what you are asking. But you can create int variable that you need for the conversion factor.

Graham Wolk
Ranch Hand
Posts: 31
Thank you Bear for the proper format for my code, this is one of my first times posting actual code to the forum.

And Jeanne, I would like to know how I can create a variable that no matter what number it is, when it is taken by the scanner it will always be converted from hours to minutes and seconds.

Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Graham Wolk wrote:And Jeanne, I would like to know how I can create a variable that no matter what number it is, when it is taken by the scanner it will always be converted from hours to minutes and seconds.

The user isn't passing in an arbitrary format. Per your spec, it is "H:MM (one digit for hours and two digits for minutes, separated by a colon)".This means you can use a method in the String class to get the hours and minutes parts separately. And Integer.parseInt() to convert Strings to numbers. Then it is just math to divide and convert to minutes to seconds.

Graham Wolk
Ranch Hand
Posts: 31
Ah ah, I actually do believe that parse.Int might work for me. I will try that out and play around with it a bit.

Here is my problem written in plain English (not Java code :P) for assistance in reading what I wish to accomplish:

1. Need to have the program print the text: "Enter marathon target time:"

2. Need to have the program take an input number with a colon format for hour:minute minute. Must be able to accept a number with a colon in it.

3. Program must convert the hours of the inputted number of the total marathon time to minutes, and the minutes to seconds so that it is in M:SS format.

4. To convert, have program divide the hours by 60 for minutes. Then, divide the minutes by 60 for seconds.

5. Finally, have the program print the line "Mile Pace:" with the converted time variable after it.

Graham Wolk
Ranch Hand
Posts: 31
So would I use the substring class in this program?

Graham Wolk
Ranch Hand
Posts: 31
I am still having a hard time figuring out how to make the program allow me to type in any kind of digit. It's easy for me to declare a single, set variable. But I cannot figure out how to have it just accept any type of value I type in?

Graham Wolk
Ranch Hand
Posts: 31

But how do I make the program accept a number with a colon in it? No matter what type of data type I use it will not accept any of the colon numbers. Even double will not allow the program to take it.

Campbell Ritchie
Marshal
Posts: 56599
172
The nextInt method of your Scanner would work better, but you need to change the delimiter first. You probably don't know enough about regular expressions to be able to do that, so we shall have to think again.

If you pass "12:34" that is a String. You can read it from the keyboard with nextLine, provided you haven't used nextAnythingElse first. [If you have used nextAnythingElse first, simply call nextLine once and do nothing with that statement. That will get your nextLine()s back in synch.] Once you have that String, you can call nextInt on it with another Scanner, provided you change the delimiter first. If you look up the Scanner documentation, there is an example quite similar to what you want. Only your example can probably be simpler than what they show there.

Campbell Ritchie
Marshal
Posts: 56599
172
Graham Wolk wrote: . . .

Here is my problem written in plain English (not Java code :P) for assistance in reading what I wish to accomplish:

1. . . .
You are right to try and break the problem into smaller parts. I would suggest each of those represents a method by itself.
I also think you should do one of those at a time. Get the user to enter 12:34 and print 12:34. The replace the print 12:34 bit with code to convert it to two numbers and then convert those two numbers to 12.566666666666666.

Beware: 34 / 60 gives 0. You may be better off with 1.0 * 34 / 60.

Graham Wolk
Ranch Hand
Posts: 31
Ah thank you very much Campbell, your answer was the type of answer I was waiting for. Maybe I phrased my question poorly that it may have been a bit hard to understand.

Anywho, I can now see how to allow the compiler to accept colons with the nextLine method you told me to use, which is great. That's one of my issues solved .

I am now trying to figure out how to use the nextPattern method the way that it can benefit me in this problem. I have never used it before, and I am currently just fiddling around with it to make it work for me.

Let me try understanding it in plain English again,

so we have the compiler accept our input as a nextLine method. So let's say I type in 4:45 to the compiler.

The only thing that it will do is return "4:45" back to me. Okay, so now I need to have it accept "4:45" and change it to a delimiter that can be divided by 60, the first thing to do in the conversion process.

Well, I can't seem to divide the string "4:45" by 60, because I'm assuming the compiler doesn't look at "4:45" as a number, it looks at it as a word.

So I would need to find some type of method like, "changeDelimiter" or "convertDelimiter". Something like that.

In other words, I want to use the nextInt method, however, I have to cross the bridge of changing the delimiter first.

So, I tried using the delimiter method by declaring a new Pattern and declaring a new delimiter method, however, that doesn't seem to work. "Pattern" doesn't seem to be a modifier like I thought it would be, like "int" or "String".

I've reached another stump. Maybe I'm missing something?

Campbell Ritchie
Marshal
Posts: 56599
172
Agree: "4:45" is a String not a number. As I said earlier, you can convert it into two numbers.

Campbell Ritchie
Marshal
Posts: 56599
172
Graham Wolk wrote: . . . Maybe I phrased my question poorly that it may have been a bit hard to understand. . . .
That is one of the reasons for using a forum like this one. If one person doesn't understand the question, there is a chance that somebody else will. It cuts both ways. If one person's answer is difficult to understand, maybe somebody else's will be easy to understand.

Please show us what you are doing about getting 12:34 into two numbers. Show us code which works as far as
System.out.println(hours + 1 + " hours, " (minutes + 1) + " minutes");
Pass "12:34" and get it to print 13 hours 45 minutes.
The strange bit with + 1 confirms that you have correctly turned the text into numbers. My definition of numbers includes something you can do arithmetic with.

Campbell Ritchie
Marshal
Posts: 56599
172
I suspect Pattern will be too complicated for now.
By the way: Pattern is not a modifier. It is the identifier for a type. As are int and String, only int is a keyword and mustn't be used for anything else.

Campbell Ritchie
Marshal
Posts: 56599
172
Did you read the examples in the Scanner documentation? They tell you which method you can use.

Graham Wolk
Ranch Hand
Posts: 31
I'm reading the Scanner documentation for anything I can use. I understand what I want to do: make the string into a workable number, but I can't seem to find anything like a "convert" or "change" method. I am reading about local numbers because every method that I think will work seems to lead back to that section of the documentation.

I am still having a bit of a hard time figuring it out though.

Graham Wolk
Ranch Hand
Posts: 31
Here is what I have so far:

Here's an example of what happens when I try this in the compiler:

"Enter marathon time: '4:45'"

"4:451"

It literally adds a 1 onto the string, which I thought I had converted to a number with the next method.

I'll try messing around with the Number Syntax section of the Scanner documentation.

Graham Wolk
Ranch Hand
Posts: 31
Would it be something to do with "match" or "MatchResult"?

Graham Wolk
Ranch Hand
Posts: 31
Ah nevermind, that isn't what I want. :/

Edit: I'm stuck again. I just can't seem to find what it is that will help me here in the Scanner package.

Graham Wolk
Ranch Hand
Posts: 31

I think I may be going in the right direction here. I can now work whatever numbers I put into the program, Ie = "Enter 4:45 / 60" result is "7".

However, now I need to figure out how to put these answers into M:SS form.

Aj Prieto
Ranch Hand
Posts: 75
Graham Wolk wrote:
I think I may be going in the right direction here. I can now work whatever numbers I put into the program, Ie = "Enter 4:45 / 60" result is "7".

Not quite, if you print out marathontimeasint, you'll notice that you're dividing 445 by 60, which isn't correct.

I think it would be easier if you calculated the total amount of minutes. After some math, you should end up with minutes / mile. (note: This would not be the final answer because it would be a decimal value rather than M:SS)

The example Campbell probably wants you to look at is towards the top of the page of the Scanner documentation. (hint: The example talks about fish).

Campbell Ritchie
Marshal
Posts: 56599
172
Graham Wolk wrote: . . .

I think I may be going in the right direction here. . . .
Afraid I don't.

Scanner doesn't have convert methods. It takes input and any conversion is done by the nextSomething methods. I suggested what to do yesterday. Read the time in the form 12:34 with nextLine() [I think you can get the same effect in this in stance with next().]
Print that out. With any luck it will show 12:34.
Pass that to another Scanner.
Read the documentation. It gives an example for dividing up Strings on the first page. You can work out what methods to use to divide it on the colon.

Graham Wolk
Ranch Hand
Posts: 31

I created a second scanner like you told me to, but at this point I'm just typing in random code without actually understanding what I'm doing. I really have no clue where this one is going.

Not to mention, this is just a simple practice problem, and I have my second Java exam in 2 weeks. I can't even figure this one out :/

Campbell Ritchie
Marshal
Posts: 56599
172
No, you created a second Scanner quite different from what I suggested. Read what I suggested properly.
If you use ":" as your delimiter, go back to the example about fish which I drew your attention to on Monday. Read that properly, too. Try that example with "12:34" as its input and ":" as its delimiter.

Yes, your code does look as though you were guessing. You can guess 1000000 times; if they are all different, there is a good chance of one working. If you work it out however, it will work first time. You need to divide and rule. Get small bits working. Each bit working can constitute a method. Then you can put them all together later.

Graham Wolk
Ranch Hand
Posts: 31
What exactly is a delimiter? What does that do?

Janeice DelVecchio
Bartender
Posts: 1812
12
a delimiter is a fancy word for separator.

csv files use commas as delimiters:
Dog, Leroy, Beagle
Cat, Garfield, Tabby

times use colons as delimiters:
4:30:59