• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

A little help with my first program

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I've been going through a textbook teaching myself java and I've started on building my first application that's not an exercise in the book. What I'm trying to do is create a recipe book type thing where you can store recipes. I want the user to input the ingredients one by one but I want the program to divide it into a double for the amount, a String for units, and another string for the actual ingredient. For example, if someone puts in "1 1/2 cups flour" I want it stored as 1.5 for amount, cups as the unit, and flour as the ingredient. I want to do this so I can eventually make it so if you want to halve the recipe it can adjust the amounts itself. I'm having trouble figuring out how I can separate each part. This is the method I've been working on but I'm sure someone out there will be able to help me understand an easier way to do this. Please let me know if I'm not explaining this well.

~tim

 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am afraid that its going to be very complicated, as you are going to fetch the data from a string that is not certain I think.
You have to handle all the cases like if user enters "2 Potatoes" or "2 leaves of coriander".

First split the String based on the white space then check if this can be converted to double, if yes then this is the amount.(If there are multiple string those can be converted to double then add them like if user enters"1 1/2 cups flour")
second part of the string can be taken as Unit and last one can be considered as ingredients.
 
Tim Mannion
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. That's what I was thinking but how would I convert the "1/2" string to a ".5" double. I was thinking of a series of if(string == "1/2") x = .5; if(string == "1/4") x = .25; etc etc because in recipes basically everything is 1/2, 1/4, 3/4, 1/3, or 1/8ths but I feel like the application will work better if it were able to convert it itself.

~Tim
 
Sheriff
Posts: 28399
100
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have started programming too early. Before you start writing code you should have some idea of what you are writing that code for. And you haven't really considered what you're going to allow in the "amount" part of the input.

And no, you can't just say you're going to allow anything. It isn't possible to write code which allows "anything". You've imagined some possibilities, but you should carry on and imagine others. Okay, there's the "1 1/2" possibility which you know what you want to do about. But the user could leave out the blank and put "11/2", for example. Or they could put in "1,500 grams butter" and the comma would confuse your code. Or "one egg"... Anyway, my point is that you're going to have to put together some specs for what you're going to accept there.

Later when you aren't "Beginning Java" you might be able to write some smart code which handles all kinds of weird stuff in the amount, but if you try to do that now it's going to drag you into a programming swamp. Keep it simple for now so you can actually write the code. Maybe only accept integers to start with -- you can always change things later.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forget about parsing "1 1/2 cups flour" for now. That's just one particular way to get data into your system. A better place to start (though not necessarily the best) would be with what the model is going to look like and what operations it can perform, and, once you have a decent idea of that landscape, write some test code and some hardcoded dummy data, and then write and refine code until the tests pass.

Only then should you think about taking input from the outside world. And when you do, you'll have to decide on what form it will take. If you're reading from a text file, you'll want to define a strict format, so youdon't have to deal with all the ambiguities that humans are so good at processing but which computers suck at.

So, for instance, you might start with a simple Ingredient class, that has fields for name, unit, and amount. You might start off with the unit being a String, but as you learn more about Java, you'll see that an enum is better suited for that. Or you might decide to have the Ingredient class just have the name and unit, if you want your model to assume that sugar always comes in cups and margarine always comes in tablespoons. For the ingredients in a recipe, you could define a RecipeIngredient or IngredientPortion class that combines an Ingredient with an amount. The Recipe class would then have a List (or Set) of RecipeIngredients (or IngredientPortions, or whatever you decide to call it).

The point is, before you write code, you need to think about how you're going model the system you're representing--what kinds of objects there will be, what they can do, and how they relate to each other. And as much as is practical, it's almost always better to get that part fairly stable on hardcoded test dataa before worrying about how to accept input. That way, you can work out those two parts independently of each other, and bring them together when each is working properly.
 
reply
    Bookmark Topic Watch Topic
  • New Topic