Results 1 to 20 of 32
- 01-05-2016, 03:27 PM #1
Member
- Join Date
- Jan 2016
- Posts
- 13
- Rep Power
- 0
Illegal start of expression problem + some questions about formating output.
Hey there,
So i have stumbled across this problem "illegal start of expression", i have no idea whats wrong.
And another thing- how can i customize the output so there are 2 spaces after the "," for example: 10.01. I know how you can do it if the variable is calculated with other variables.
Java Code:public class Problem { double h; double p; double k; double m; double t; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Palun sisestage läbitud vahemaa kilomeetrites: "); p = scan.nextDouble(); System.out.println("Palun sisestage kasutatud kütuse hind: "); h = scan.nextDouble(); System.out.println("Palun sisestage tarbitud kütuse kogus liitrites: "); k = scan.nextDouble(); double t; t = k / (p * 100); public static ; double t = 2.6789; System.out.printf("%.2f", t); double m; m = k * h; public static ; double m = 2.6789; System.out.printf("%.2f", m); System.out.printf(" You have driven " + p + " kilometers and used " + k + " liters of fuel. Your car used " + t + " liters of fuel per 100 kilometers. Cost of the drive was " + m + " euros. "); } }
Last edited by JosAH; 01-05-2016 at 05:55 PM. Reason: fixed the [code] ... [/code] tags
- 01-05-2016, 03:43 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
Re: Illegal start of expression problem + some questions about formating output.
In the first line: 'problem?' is not a valid class name (it isn't even a valid identifier name); make that 'Problem'.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 01-05-2016, 03:51 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Re: Illegal start of expression problem + some questions about formating output.
If you want us to comment on your code it really ought to be posted here.
Many people (including me) will not download stuff.Please do not ask for code as refusal often offends.
** This space for rent **
- 01-05-2016, 04:13 PM #4
Member
- Join Date
- Jan 2016
- Posts
- 13
- Rep Power
- 0
Re: Illegal start of expression problem + some questions about formating output.
I thought that, but i do not know how can i poste code formats. Tried seaching, nothing.
- 01-05-2016, 04:33 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Re: Illegal start of expression problem + some questions about formating output.
[code]
Your code goes here
[/code]
:)Please do not ask for code as refusal often offends.
** This space for rent **
- 01-05-2016, 05:50 PM #6
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: Illegal start of expression problem + some questions about formating output.
You have a number of "problems" here. You won't be able to access your unqualified instance variables (h,p, etc) from a static context like main. The dangling public static; statement is also causing problems. There may be others since I didn't even try to compile this.
And do what Tolls suggested in post #5.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 01-05-2016, 05:58 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
Re: Illegal start of expression problem + some questions about formating output.
Lines #25 and #31 don't make any sense at all; you need to fix them. Also, non static member variables need an object or they won't exist.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 01-06-2016, 01:40 PM #8
Member
- Join Date
- Jan 2016
- Posts
- 13
- Rep Power
- 0
- 01-06-2016, 02:00 PM #9
Re: Illegal start of expression problem + some questions about formating output.
Still havent figured out the 2nd question.
Add some comments saying what is wrong with it and show what the output should look like.If you don't understand my response, don't ignore it, ask a question.
- 01-07-2016, 01:41 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
- 01-07-2016, 05:14 PM #11
Member
- Join Date
- Jan 2016
- Posts
- 13
- Rep Power
- 0
- 01-07-2016, 06:03 PM #12
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: Illegal start of expression problem + some questions about formating output.
I am using great restraint here but I simply have to ask how you can't know the answer to that? Also, did you remove the "public static;" statement?
And I presume the second declaration of 't' is also an error since a) it will cause a compiler error, and b) you already calculated 't'.
Once you think your code is correct, you should repost it (don't edit the old code). Otherwise, we can only guess what you have done.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 01-07-2016, 06:12 PM #13
Member
- Join Date
- Jan 2016
- Posts
- 13
- Rep Power
- 0
Re: Illegal start of expression problem + some questions about formating output.
Java Code:import java.util.Scanner; public class Arvutamine1 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Please enter the distance: "); double p = scan.nextDouble(); //here is the problem- how do i make the value of this variable shown with 2 decimal places? Same with others. System.out.println("Please enter the price of fuel: "); double h = scan.nextDouble(); System.out.println("Please enter the amount of consumed fuel: "); double k = scan.nextDouble(); double t = k / ( p / 100); System.out.printf("%.2f", t); double m = k * h; System.out.printf("%.2f", m); System.out.printf(" You have driven " + p + " kilometers and used " + k + " liters of fuel. Your car used " + t + " liters of fuel per 100 kilometers "); } }
Edit: still cant get the code format working.Last edited by JosAH; 01-07-2016 at 06:32 PM. Reason: fixed the [code] ... [/code] tags again
- 01-07-2016, 06:33 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
Re: Illegal start of expression problem + some questions about formating output.
@OP: didn't you read my edit comments both in your OP and in your last post? Programmers can never be sloppy ...
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 01-07-2016, 06:47 PM #15
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: Illegal start of expression problem + some questions about formating output.
You're not using your second printf() correctly. It is basically just a print().
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 01-07-2016, 06:51 PM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Re: Illegal start of expression problem + some questions about formating output.
Compare this printf usage from your code:
Java Code:System.out.printf("%.2f", m);
Java Code:System.out.printf(" You have driven " + p + " kilometers and used " + k + " liters of fuel. Your car used " + t + " liters of fuel per 100 kilometers ");
Please do not ask for code as refusal often offends.
** This space for rent **
- 01-08-2016, 02:39 PM #17
Member
- Join Date
- Jan 2016
- Posts
- 13
- Rep Power
- 0
- 01-08-2016, 02:40 PM #18
Member
- Join Date
- Jan 2016
- Posts
- 13
- Rep Power
- 0
- 01-08-2016, 03:33 PM #19
Re: Illegal start of expression problem + some questions about formating output.
latter one overwrites the first oneIf you don't understand my response, don't ignore it, ask a question.
- 01-08-2016, 03:50 PM #20
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: Illegal start of expression problem + some questions about formating output.
You need to read the Java API on the Formatter class (Formattable (Java Platform SE 8 )). This is what printf uses to do its magic. There are plenty of examples in the API.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
Problem witch "Illegal start of expression"
By thyks in forum New To JavaReplies: 19Last Post: 06-12-2012, 11:06 AM -
problem with illegal start of expression methdo
By lathspell in forum New To JavaReplies: 1Last Post: 12-15-2011, 06:29 PM -
Illegal start of expression
By lodaSchitt in forum New To JavaReplies: 2Last Post: 04-28-2011, 11:04 PM -
Illegal start of expression
By Tekila in forum New To JavaReplies: 10Last Post: 03-04-2011, 01:23 AM -
Illegal Start of expression
By Macca07 in forum New To JavaReplies: 3Last Post: 11-23-2009, 09:43 AM
Bookmarks