PDA

View Full Version : Programming in java, lots of false errors.



michael12345
21st July 2009, 04:11 AM
Just trying to put together a small program which should be straight forward but having issues.
Also, I'm using openjdk.

What I input in vim;
--------
mport java.util.Scanner;
import java.text.DecimalFormat;
import java.* //was troubleshooting

public class phy114

{

public static void main( String [] args )
{
char continue = 'y';

Scanner scan = new Scanner (System.in);

while ( continue != n)
{
System.out.print ("Enter V> ");
float v = scan.nextFloat();
System.out.print ("Enter d(pc)";
float d = scan.nextFloat();
float mv = v - 5(Math.log(d)+5);
System.out.print ("Mv = " +mv );
System.out.print ("Continue, y/n");
continue = scan.nextChar();
}
System.out.print ("Process terminated");
}
}

---------------------------
I seem to get a lot of false errors.

output from javac
---------------------------

$ javac phy114.java
phy114.java:3: ';' expected
import java.*
^
phy114.java:11: not a statement
char continue = 'y';
^
phy114.java:11: ';' expected
char continue = 'y';
^
phy114.java:11: ';' expected
char continue = 'y';
^
phy114.java:15: illegal start of expression
while ( continue != n)
^
phy114.java:15: illegal start of expression
while ( continue != n)
^
phy114.java:15: ';' expected
while ( continue != n)
^
phy114.java:15: illegal start of expression
while ( continue != n)
^
phy114.java:15: ';' expected
while ( continue != n)
^
phy114.java:19: ')' expected
System.out.print ("Enter d(pc)";
^
phy114.java:21: ';' expected
float mv = v - 5(Math.log(d)+5);
^
phy114.java:21: not a statement
float mv = v - 5(Math.log(d)+5);
^
phy114.java:21: ';' expected
float mv = v - 5(Math.log(d)+5);
^
phy114.java:24: ';' expected
continue = scan.nextChar();
^
phy114.java:26: <identifier> expected
System.out.print ("Process terminated");
^
phy114.java:26: illegal start of type
System.out.print ("Process terminated");
^
phy114.java:28: class, interface, or enum expected
}
^
17 errors
------------------------------

Min
21st July 2009, 05:41 AM
you forget the ";" after import java.*
try that first then tell use more

michael12345
21st July 2009, 06:06 AM
Stupid, error, I had added that right before posting just to rule out something goofey, fixed the semi colon with no resolve.

jonduf
21st July 2009, 07:16 AM
$ javac phy114.java
phy114.java:3: ';' expected
import java.*
^

Needs a semicolon ";" at the end of the line.



phy114.java:11: not a statement
char continue = 'y';
^
phy114.java:11: ';' expected
char continue = 'y';
^
phy114.java:11: ';' expected
char continue = 'y';
^
phy114.java:15: illegal start of expression
while ( continue != n)
^
phy114.java:15: illegal start of expression
while ( continue != n)
^
phy114.java:15: ';' expected
while ( continue != n)
^
phy114.java:15: illegal start of expression
while ( continue != n)
^
phy114.java:15: ';' expected
while ( continue != n)
^


"continue" is a keyword, do not use it as a variable name.



phy114.java:19: ')' expected
System.out.print ("Enter d(pc)";
^

you are missing a closing parenthesis, try "System.out.print ("Enter d(pc)");"



phy114.java:21: ';' expected
float mv = v - 5(Math.log(d)+5);
^
phy114.java:21: not a statement
float mv = v - 5(Math.log(d)+5);
^
phy114.java:21: ';' expected
float mv = v - 5(Math.log(d)+5);
^


there is no implied multiplicaiton, try "float mv = v - 5 * (Math.log(d)+5);"


Try fixing those and then recompile

michael12345
21st July 2009, 01:12 PM
Needs a semicolon ";" at the end of the line.



"continue" is a keyword, do not use it as a variable name.


you are missing a closing parenthesis, try "System.out.print ("Enter d(pc)");"



there is no implied multiplicaiton, try "float mv = v - 5 * (Math.log(d)+5);"


Try fixing those and then recompile

The continue was causing most of the errors,have a few more, but I should be able to figure those out. Thank you.

import java.util.Scanner;
import java.text.DecimalFormat;
import java.*;

public class phy114

{

public static void main( String [] args )
{
String sent= "y";

Scanner scan = new Scanner (System.in);

while ( sent != "n")
{
System.out.print ("Enter V> ");
float v = scan.nextFloat();
System.out.print ("Enter d(pc)");
float d = scan.nextFloat();
float mv = v - 5 * (Math.log(d)+5);
System.out.print ("Mv = " +mv );
System.out.print ("Continue, y/n");
sent= scan.next();
}
System.out.print ("Process terminated");
}
}

Min
21st July 2009, 01:40 PM
while ( sent != "n")

Don't do that :)
use

sent.compareToIgnoreCase("n")!=0
sent.compareTo("n")!=0
!sent.equal("n")

instead. if you use char (!='n') it wouldn't be a problem. But never ever compare string using == or !=