Page 1 of 2 12 LastLast
Results 1 to 20 of 32
  1. #1
    Reviloest is offline Member
    Join Date
    Jan 2016
    Posts
    13
    Rep Power
    0

    Default 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default 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,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default 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 **

  4. #4
    Reviloest is offline Member
    Join Date
    Jan 2016
    Posts
    13
    Rep Power
    0

    Default 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.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default 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 **

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default 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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default 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,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  8. #8
    Reviloest is offline Member
    Join Date
    Jan 2016
    Posts
    13
    Rep Power
    0

    Default Re: Illegal start of expression problem + some questions about formating output.

    Quote Originally Posted by JosAH View Post
    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,

    Jos
    Thanks! Got it working. Still havent figured out the 2nd question.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Illegal start of expression problem + some questions about formating output.

    Still havent figured out the 2nd question.
    Can you post the program's output to show the problem?
    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.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Illegal start of expression problem + some questions about formating output.

    Quote Originally Posted by Reviloest View Post
    Thanks! Got it working. Still havent figured out the 2nd question.
    Why don't you use the printf( ... ) method (as you already did in your original incorrect code)?

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  11. #11
    Reviloest is offline Member
    Join Date
    Jan 2016
    Posts
    13
    Rep Power
    0

    Default Re: Illegal start of expression problem + some questions about formating output.

    Quote Originally Posted by JosAH View Post
    Why don't you use the printf( ... ) method (as you already did in your original incorrect code)?

    kind regards,

    Jos
    I did but where does the printf() go?

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Illegal start of expression problem + some questions about formating output.

    Quote Originally Posted by Reviloest View Post
    I did but where does the printf() go?
    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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    Reviloest is offline Member
    Join Date
    Jan 2016
    Posts
    13
    Rep Power
    0

    Default 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 ");
      }
     
    }
    New code.

    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

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default 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,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default 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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default 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);
    with this printf on the following line:
    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 ");
    The latter has no formatting in there t all (that's what the %.2f stuff in the first one is all about).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #17
    Reviloest is offline Member
    Join Date
    Jan 2016
    Posts
    13
    Rep Power
    0

    Default Re: Illegal start of expression problem + some questions about formating output.

    Quote Originally Posted by JosAH View Post
    @OP: didn't you read my edit comments both in your OP and in your last post? Programmers can never be sloppy ...

    kind regards,

    Jos
    yes i read them both. Tried how you explained, didint work, thats why.

  18. #18
    Reviloest is offline Member
    Join Date
    Jan 2016
    Posts
    13
    Rep Power
    0

    Default Re: Illegal start of expression problem + some questions about formating output.

    Quote Originally Posted by Tolls View Post
    Compare this printf usage from your code:
    Java Code:
    System.out.printf("%.2f", m);
    with this printf on the following line:
    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 ");
    The latter has no formatting in there t all (that's what the %.2f stuff in the first one is all about).
    Does that mean the latter one overwrites the first one? If so, whats the proper formating in order to achieve that?

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Illegal start of expression problem + some questions about formating output.

    latter one overwrites the first one
    I don't think there is a way to overwrite text written to a Windows console. I don't know about Linux.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default 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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Page 1 of 2 12 LastLast

Similar Threads

  1. Problem witch "Illegal start of expression"
    By thyks in forum New To Java
    Replies: 19
    Last Post: 06-12-2012, 11:06 AM
  2. problem with illegal start of expression methdo
    By lathspell in forum New To Java
    Replies: 1
    Last Post: 12-15-2011, 06:29 PM
  3. Illegal start of expression
    By lodaSchitt in forum New To Java
    Replies: 2
    Last Post: 04-28-2011, 11:04 PM
  4. Illegal start of expression
    By Tekila in forum New To Java
    Replies: 10
    Last Post: 03-04-2011, 01:23 AM
  5. Illegal Start of expression
    By Macca07 in forum New To Java
    Replies: 3
    Last Post: 11-23-2009, 09:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •