• Post Reply Bookmark Topic Watch Topic
  • New Topic

Beginner help with easy program  RSS feed

 
Hamed Haghjo
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
School assignment says that we should create a simple java program to calculate some things.
The info provided is: A book seller sells books for 35 euros each and he gives you a package price if you buy more than 50 books, more than 100 books and more than 200 books.
More than 50 = 10% sale
More than 100 = 15% sale
More than 200 = 20% sale

So a user interface... This is how my program looks like at the moment, it's unfinished, mostly because I stumbled upon problems. (I can't use keyboard class so I use a scanner)



As you can see I forgot the 200 books thingy, and I'm new to java, so help?
 
Carey Brown
Bartender
Posts: 3001
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your amountofbooks should be an int, not a double.
For the range checks, start with the highest (i.e. 200) first and work backwards using 'else if's. That way you don't need to check both min and max of range.
 
Hamed Haghjo
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now this comes up:

(Don't mind the adresses, I altered them a bit)

--------------------Configuration: <Default>--------------------
C:\Users\user\Documents\JCreator Pro\MyProjects\pocketbooks\src\pocketbooks.java:17: error: 'else' without 'if'
}else if (amountofbooks >=50); {
^
C:\Users\user\Documents\JCreator Pro\MyProjects\pocketbooks\src\pocketbooks.java:19: error: 'else' without 'if'
}else if (amountofbooks >=0); {
^
2 errors

Process completed.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hamed Haghjo wrote:
C:\Users\user\Documents\JCreator Pro\MyProjects\pocketbooks\src\pocketbooks.java:17: error: 'else' without 'if'
}else if (amountofbooks >=50); {
^


The error message is saying that the compiler can't find the matching "if" for the "else" that is on that line (line 17). You need to find the matching "if" to figure out what is wrong.

Henry
 
Hamed Haghjo
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry can't find no errors in this code, but it wont run properly though. The general output gives me default configuration text.

 
Hamed Haghjo
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried a new code, but this one says I have purchased more than or equal to 50 books even though I typed 2 books.

 
Carey Brown
Bartender
Posts: 3001
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hamed Haghjo wrote:Sorry can't find no errors in this code, but it wont run properly though. The general output gives me default configuration text.

Not sure what you mean by 'default'?
 
Carey Brown
Bartender
Posts: 3001
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope. You broke it. The previous listing was correct. What's the problem?
 
Hamed Haghjo
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't worry about that, I bypassed that by reconfiguring the codes, this is the new code but it still thinks I bought more than 100 books if I type for example 59 books.

 
Hamed Haghjo
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone download Teamviewer and help me through there, please?
 
Carey Brown
Bartender
Posts: 3001
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read line 16 carefully. It is working as you told it.
 
Carey Brown
Bartender
Posts: 3001
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hamed Haghjo wrote:Sorry can't find no errors in this code, but it wont run properly though. The general output gives me default configuration text.


This listing you posted should work. It seems like you're thrashing about now. I'd go back to this and carefully describe what input you gave, what it returned, and what you expected.

EDIT: Errors on line 19 and 22. You'll have to look closely at this one.
 
Hamed Haghjo
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This one works now, but it stills thinks 5 books is equal to 50 or more books
 
Carey Brown
Bartender
Posts: 3001
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just ran it with 5 books and it worked, so, what output are you getting (cut and paste the text in a post).
Still have an error on line 22.
 
Hamed Haghjo
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get this the default configuration thing on the general output now with this code:


This general output:
--------------------Configuration: <Default>--------------------
Syntax: java [-alternativ] class [argument...]
(för att köra en klass)
eller java [-alternativ] -jar jarfile [argument...]
(för att köra en jar-fil)
där alternativen omfattar:
-d32 använd en 32-bitsdatamodell om det finns
-d64 använd en 64-bitsdatamodell om det finns
-server för att välja "server" VM
Standard-VM är server.

-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
En lista över kataloger, JAR-arkiv och och ZIP-arkiv
för sökning efter klassfiler avgränsad med ;.
-D<name>=<value>
ange en systemegenskap
-verbose:[class|gc|jni]
aktivera utförliga utdata
-version skriv ut produktversion och avsluta
-version:<value>
Varning: den här funktionen är inaktuell och kommer
att tas bort i en framtida utgåva.
kräv den angivna versionen för att köra
-showversion skriv ut produktversion och fortsätt
-jre-restrict-search | -no-jre-restrict-search
Varning: den här funktionen är inaktuell och kommer
att tas bort i en framtida utgåva.
inkludera/exkludera användarprivata JRE:er i versionssökningen
-? -help skriv ut det här hjälpmeddelandet
-X skriv ut hjälp för icke-standardalternativ
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
aktivera verifieringar med den angivna detaljgraden
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
avaktivera verifieringar med den angivna detaljgraden
-esa | -enablesystemassertions
aktivera systemverifieringar
-dsa | -disablesystemassertions
avaktivera systemverifieringar
-agentlib:<libname>[=<options>]
ladda det ursprungliga agentbiblioteket <libname>, t.ex. -agentlib:hprof
se även -agentlib:jdwp=help och -agentlib:hprof=help
-agentpath:<pathname>[=<options>]
ladda det ursprungliga agentbiblioteket med det fullständiga sökvägsnamnet
-javaagent:<jarpath>[=<options>]
ladda agenten för programmeringsspråket Java, se java.lang.instrument
-splash:<imagepath>
visa välkomstskärmen med den angivna bilden
Mer information finns på http://www.oracle.com/technetwork/java/javase/documentation/index.html.

Process completed.
 
Hamed Haghjo
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's in Swedish, sorry. But I think it wont matter because the error would look alike except for the language if you have english on your settings
 
Carey Brown
Bartender
Posts: 3001
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I don't understand that language. Is this during compilation or trying to run it? Are you trying to do this from the command line or from an IDE? If from the command line, what are you typing in before you get that message? Seems like this issue has nothing to do with the code itself but what you're typing on the command line and perhaps specifying -classpath.
 
Carey Brown
Bartender
Posts: 3001
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like you are trying to run the program from the command line. You should be in the same directory as your alibaba5.class file resides. Then run:
java alibaba5

That should work unless the 'java' command isn't recognized, or it can't find the path to the java libraries.



English translation

Syntax: java [-options] class [arguments ...]
(to drive a class)
or java [-options] -jar jarfile [argument ...]
(to drive a jar file)
where options include:
-d32 use a 32-bit data model, if there is
-d64 use a 64-bit data model, if there is
server to select the "server" VM
Standard VM is server.

-cp <class search path of directories and zip / jar files>
-classpath <class search path of directories and zip / jar files>
A list of directories, JAR archives, and ZIP archives and
to search for class files separated by;.
-D <Name> = <value>
specify a system property
-verbose: [class | gc | JNI]
enable detailed output
-version print product version and exit
-version: <value>
Warning: This function is obsolete and will
be removed in a future release.
Require the specified version to run
-showversion print product version and continue
-jre-restrict-search | -no-jre-restrict-search
Warning: This function is obsolete and will
be removed in a future release.
Include / Exclude user private JREs in the version search
-? -help print this help message
-X Print help for non-standard options
-ea [: <package name> ... |: <className>]
-enableassertions [: <package name> ... |: <className>]
enable verification of the specified level of detail
· d [: <package name> ... |: <className>]
-disableassertions [: <package name> ... |: <className>]
disable verification with the specified degree of detail
-esa | -enablesystemassertions
activate systemverifieringar
-dsa | -disablesystemassertions
deactivating systemverifieringar
-agentlib: <libname> [= <options>]
download the original agent library <libname>, for example, -agentlib: hprof
see also -agentlib: jdwp = help and -agentlib: hprof = help
-agentpath: <path name> [= <options>]
download the original agent library by full path name
-javaagent: <jarpath> [= <options>]
download the agent for the Java programming language, see java.lang.instrument
-splash: <image path>
display the Welcome to the specified image
More information is available at http://www.oracle.com/technetwork/java/javase/documentation/index.html.
 
Carey Brown
Bartender
Posts: 3001
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What OS are you on? What is your CLASSPATH environment variable set to?

On windows you can:
echo %CLASSPATH%
 
Hamed Haghjo
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the problem, I opened the .java files from a folder which made it give me weird general output. This code works now:



But can someone simplyfy my work and tell me or write on my code if a user says he buys 42 books how much it will cost him.
With caution that you get a sale
if you buy 50 or more books by 10%
if you buy 100 or more books by 15%
if you buy 200 or more books by 20%
 
Carey Brown
Bartender
Posts: 3001
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hamed Haghjo wrote:But can someone simplyfy my work and tell me or write on my code if a user says he buys 42 books how much it will cost him.

As a first cut I would implement a simple cost calculation if quantity was less than 50, i.e., no discount.
 
Hamed Haghjo
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will try to wait for someone to do it for me, it shouldn't take toooooo long. And I'd highly appreciate anyone who took their time. For me it'd take hours, I would have been stuck on this site not getting anywhere.

I learn from having a problem and seeing the solution, that's how I work It's like answers on a mathematical question, if I see how it gets its solution then I'll generalize the method and learn from it.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Do you still have a question? From your previous post, you stated that your "code works now".... so not sure what you are waiting for.

Henry
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hamed Haghjo wrote:I will try to wait for someone to do it for me, it shouldn't take toooooo long. And I'd highly appreciate anyone who took their time. For me it'd take hours, I would have been stuck on this site not getting anywhere.

I learn from having a problem and seeing the solution, that's how I work It's like answers on a mathematical question, if I see how it gets its solution then I'll generalize the method and learn from it.

You'll learn here that we will not "do it for you." I'm sure some one will find the link for the 'we are not a codemill' FAQ, and provide that for you.

That said, you are on the correct path here. You know how many books the user is ordering (see line 12 in your code), so it is a simple matter of doing the math. You could do the math at each if-else statement, jsut to get a quick and easy feel for an approach.

Regards,
Robert
 
Knute Snortum
Sheriff
Posts: 4081
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are NotACodeMill (that's a link). That means we will try to gently guide you towards a solution but not provide a complete solution you can just copy and paste. You learn more that way!
 
John Morgan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I was first starting out and posted questions here I ran into the same frustration that I am sure you are feeling now... one of the suggestions that was suggested to me is to write each step out in regular expressions... so you would have something like:

I am purchasing 42 books.. that is not over 200 I will not see this

I am purchasing 42 books.. that is not over 100 I will not see this

I am purchasing 42 books.. that is not over 50 I will not see this

I am purchasing 42 books.. that is over 0 I will see this....


Then step through with different amounts (you can do this in your head) and you will start to see where the errors are.... (hint it is in line 22 as many have suggested).

Writing it out is still something I do today, and it has helped me immensely.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would write the else ifs differentlyThere is a subtle difference from what you wrote.
 
Carey Brown
Bartender
Posts: 3001
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I would write the else ifs differentlyThere is a subtle difference from what you wrote.

You'd have to use "<=" in your case.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:. . . You'd have to use "<=" in your case.
Or you have the ranges 0...49, 50...99, 100...199 and 200...∞
You pays your money and you takes your choice.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!