• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

error: reached end of file while parsing problem

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// ************************************************
// Positions.java Typed by: Strong
//
// Equation for a body in linear motion.
// ************************************************

import Java.util.Scanner;

public class Positions {

public static void main (String[] args)

{
Double v0 = 3.25;
Double a = -9.8;

Scanner input = new Scanner(System.in);
System.out.printf("Value for s0: ");
Double s0 = input.nextDouble();
System.out.printf("Value for t: ");
Double t = input.nextDouble();
}

{for ( Double s = s0 + (v0 * t) + ((1 % 2) * (a0 * t));
System.out.print((String)s);


Positions.java:25: error: reached end of file while parsing
System.out.print((String)s);



can anyone help me? I've tried to fix this many different ways and i still get this error message.
^
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lines 24, 25,28, and 29 are not contained within a method. No closing brace for the class.
 
Glenn Strong
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:
Lines 24, 25,28, and 29 are not contained within a method. No closing brace for the class.



it kept giving me an error message every time i put a closing brace and so when i took it out thats when i got this one.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Glenn Strong wrote:it kept giving me an error message every time i put a closing brace and so when i took it out thats when i got this one.


That's really not a good way to code. The "i'll guess what should be there and see if it works" is a very slow development process - you will most likely NEVER finish what you want.

So...stop. THINK. that's what coding is all about - thinking, not typing.

I would suggest you remove as much as possible. Write a main method that does nothing but prints "i am in main". Once that works, add 2-3 lines, recompile, and re-test. If things don't work, FIX THEM.

I would suggest at minimum you start by deleting everything from line 22 on - although you may need to add a single closing curly brace. I believe it will then compile. run it. test it. Put in some temporary code to prove that your s0 and t (which, IMHO, are TERRIBLE variable names) get set to what you expect.

THEN try adding in your for loop. I'm not even sure that is a valid "for" statement. but add it in nonetheless. have it do NOTHING but may print "i'm in the loop". TEST it. once it works, update the body to do something. etc.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Greenhorn
Posts: 22
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May be you want something like this?
public class Positions {
public static void main(String[] args) {
Double v0 = 3.25;
Double a = -9.8;

Scanner input = new Scanner(System.in);
System.out.printf("Value for s0: ");
Double s0 = input.nextDouble();
System.out.printf("Value for t: ");
Double t = input.nextDouble();

Double s = s0 + (v0 * t) + ((1 % 2) * (a * t));
System.out.print(s);
}
}
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it probably would be. But please use the code button and maintain indentation, then it would look much better.
Correct indentation would make such errors easier to find. I usually use Allman indentation, as described in our formatting suggestions. You have to learn to write backwards (see the two links in this old post), which makes correct indentation much easier. If you ever find you have somewhere where you expect a { or a } and it isn't there, you know you have an error. Similarly if you find two {s or two }s above each other, that is a different sort of error.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 % 2??? You can leave that term out altogether because it will reduce to 1 *
 
Vitaliy Gaydarenko
Greenhorn
Posts: 22
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:1 % 2??? You can leave that term out altogether because it will reduce to 1 *


You are right. Really we can simplify expression as : Double s = s0 + (v0 * t) + (a * t);
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can simplify that more by omitting the () because the * has a higher precedence than +. You can simplify the expression more by giving proper names to the variables: s0 and v0 are very poor names, and a/t are hardly better.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You can simplify that more by omitting the () because the * has a higher precedence than +. You can simplify the expression more by giving proper names to the variables: s0 and v0 are very poor names, and a/t are hardly better.



They're pretty reasonable.
It's:
s = ut + 1/2 at(sq)
(How do you do a superscript?)

Not that they've written it correctly (I'm guessing s0 is the start position).
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use superscripts for squares because
  • 1: Java® doesn't implement an exponentiation operator, and
  • 2: x * x is usually faster and more precise than x²
  •  
    Campbell Ritchie
    Marshal
    Posts: 79178
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You can find out about superscript numbers here and here.
     
    Dave Tolls
    Rancher
    Posts: 4801
    50
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Don't use superscripts for squares because

  • 1: Java® doesn't implement an exponentiation operator, and
  • 2: x * x is usually faster and more precise than x²


  • I thought there might have been a <sup> equivalent I was missing for whatever forum software is used here .

    And I was showing the equation of motion the OP was working with, in order to highlight that calling the variables 's', 'a' and 't' was reasonable, so using a "proper" t² seemed appropriate.
    s = ut + ½at²


    Found a half as well...
     
    Campbell Ritchie
    Marshal
    Posts: 79178
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You can use &frac12; for half, but on a Linux box I can get ½ with altGr-5.
    There is a sup tag but it doesn't seem to work on the forum. At least it didn't when I tried it, but that was a long time ago. There are HTML tags like &sup2; but I tend not to use them. I usually use &#x00b2; for ²

    When I saw ut + ½at² it did remind me of my school mechanics but that was a long time ago and it does not count as good programming to use such short variable names.
     
    Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic