Java code is not working properly what i can do? - java

class java
{
public static void main(String args[])
{
System.out.println("Testing");
}
}`
not working above code and explain the above method.
`

I guess it's cause of Apostrophe on the last line of your code.
Plus a class name should always start with a capital letter, jus a bit of information thought it has nothing to do with error.
Your code is trying to print out a line on the console which is Testing

Remove the apostrophe at the last line

Related

BYACCJ: How do I include line number in my error message?

This is my current error handling function:
public void yyerror(String error) {
System.err.println("Error: "+ error);
}
This is the default error function I found on the BYACC/J homepage. I can't find any way to add the line number. My question is similar to this question. But the solution to it doesn't work here.
For my lexer I am using a JFlex file.
It's not that different from the bison/flex solution proposed in the question you link. At least, the principle is the same. Only the details differ.
The key fact is that it is the scanner, not the parser, which needs to count lines, because it is the scanner which converts the input text into tokens. The parser knows nothing about the original text; it just receives a sequence of nicely-processed tokens.
So we have to scour the documentation for JFlex to figure out how to get it to track line numbers, and then we find the following in the section on options and declarations:
%line
Turns line counting on. The int member variable yyline contains the number of lines (starting with 0) from the beginning of input to the beginning of the current token.
The JFlex manual doesn't mention that yyline is a private member variable, so in order to get at it from the parser you need to add something like the following to your JFlex file:
%line
{
public int GetLine() { return yyline + 1; }
// ...
}
You can then add a call to GetLine in the error function:
public void yyerror (String error) {
System.err.println ("Error at line " + lexer.GetLine() + ": " + error);
}
That will sometimes produce confusing error messages, because by the time yyerror is called, the parser has already requested the lookahead token, which may be on the line following the error or even separated from the error by several lines of comments. (This problem often shows up when the error is a missing statement terminator.) But it's a good start.

Typesafe ConfigFactory error with reserved characters

Hi I am trying to load configuration from a String in Java as follows:
#Test
public void testIllegalCharacter(){
String input = "prop=\\asd";
Config conf = ConfigFactory.parseString(input);
}
The code above produces the following error:
com.typesafe.config.ConfigException$Parse: String: 1: Expecting a value but got wrong token: '\' (Reserved character '\' is not allowed outside quotes) (if you intended '\' (Reserved character '\' is not allowed outside quotes) to be part of a key or string value, try enclosing the key or value in double quotes, or you may be able to rename the file .properties rather than .conf)
I understand I have an illegal character in my String. Although how do I find the full set of illegal characters?
If I (for example) convert this String into a Properties object and then parse it with ConfigFactory.parseProperties I can see the value "\\asd" in resolved as "asd". So there must be some some sanitising going on in the typesafe library, I wish I could call that sanitisation myself, but I cannot see how. Parsing to Properties is not a viable solution as the configuration could be composed by Objects or Lists too.
Has anyone have any suggestion how to solve this issue?
Alternatively can someone point out all the reserved characters set?
Many thanks
If I understand the error message correctly, you should put quotes around you special characters, e.g. like this:
"prop=\"\\asd\"";
Not sure why you's want a property definition with a backslash a ('\a') in it, but I guess I don't need to know :-)
I think I might have found the answer. I need to set the ConfigParseOptions.defaults().setSyntax(ConfigSyntax.PROPERTIES)
Which works for the test below:
#Test
public void test(){
String input = "prop=C:/MyDocuments/mydir";
Config conf = ConfigFactory.parseString(input, ConfigParseOptions.defaults().setSyntax(ConfigSyntax.PROPERTIES));
assertEquals("C:/MyDocuments/mydir", conf.getAnyRef("prop"));
}
But will not work for the test with backslashes
#Test
public void test(){
String input = "prop=C:\\MyDocuments\\mydir";
Config conf = ConfigFactory.parseString(input, ConfigParseOptions.defaults().setSyntax(ConfigSyntax.PROPERTIES));
assertEquals("C:\\MyDocuments\\mydir", conf.getAnyRef("prop"));
}
Which fails:
org.junit.ComparisonFailure:
Expected :C:\MyDocuments\mydir
Actual :C:MyDocumentsmydir
So I am not sure this is the definitive answer...

compiling Java error, cannot get constructor working

I am trying to code a simple bit of java but i cannot get it to compile
I have defined the objects within my class as so
public class teams
{
public char sponsor;
public char tires;
I am trying to set the default value of class sponsor to N/A
public teams()
{
this.sponsor = "N/A";
}
Can anyone figure out why it wont work? Im fairly new to java and i know this is probably extremely simple, any help would be appreciated!
EDIT
So thanks to Kevin Esche, I managed to get it to compile by using,
this.sponsor = 'N';
How would I get the term N/A instead of just N? Would I use unicode?, and if yes how do you format it?
Either you make your sponsor a String
public String sponsor;
Or you store only one character in it:
sponsor = 'X';
Good Luck.

Can i nest two output statement in Java

Is this possible ? possible means, how to do it correctly? System.out.println("System.out.println("")");
No, you cannot. System.out.println() return type is void.
public void println()
When you write
System.out.println("System.out.println("")");
Compiler treats that the content inside "" as String not the function.
System.out.println() is not returning values(void) so you can't do something like you want here. BTW what is the purpose of doing this?
Again
System.out.println("System.out.println("")"); // this is not valid statement
you can write as follows
System.out.println("System.out.println(\"\")");
But out put is just
System.out.println("")
If you just want to print System.out.println("") then do like this
System.out.println("System.out.println(\"\")");
No. System.out.println is a void method. I'm not sure why you want to do that, just print one line after another.
no it is not possible Please check these link to know how the "system.out.print()" works
http://javapapers.com/core-java/system-out-println/
http://www.cis.upenn.edu/~matuszek/General/JavaSyntax/print-statements.html
This depends on what you want. What result do you want to achieve? How much effort do you want to put into it?
What do you mean under nesting? The answer is: generally no, but if you really want, yes.
class SOP { // SOP stands for system.out.println
public static SOP p = new SOP();
public String toString() {return "";}
SOP p(Object... oo) {for(Object o : oo){System.out.print(o.toString());} return this;}
SOP pl(Object... oo) {p(oo); return l();}
SOP l() {System.out.println(); return this;}
}
public class A {
public static void main(String[] p) {
SOP.p.p("hello,").pl(" world!");
SOP.p.p("What exactly do you mean under \"nesting\"?", SOP.p.p("Is this nesting?"));
SOP.p.l();
}
}
and the output:
$ javac A.java
$ java A
hello, world!
Is this nesting?What exactly do you mean under "nesting"?
Have fun!
PS probably you want to write a program that prints itself.
In this case: the stuff in the quotes is not evaluated as Java code, and there's no way in java to do it... unless you call the compiler via a command of the underlying OS, provided that the compiler is there on the target platform.

java printf help needed

public class Format
{
public static void main(String args[])
{
System.out.printf("%30s|%30s","Organization","Number of users");
System.out.printf("%30s|%30s","Arcot","100");
}
}
It prints:
Organization| Number of users Arcot| 100
Why is the 2nd row out of alignment? The word "Arcot" is not given enough padding, although the word "100" is. I'm sorry, this text window applies its own formatting, it is not showing what I have pasted as the output. You may need to run the code to see the output obtained.
Try these.
System.out.println(String.format("%30s|%30s","Organization","Number of users"));
System.out.println(String.format("%30s|%30s","Arcot","100"));
System.out.printf("%30s|%30s\n","Organization","Number of users");
System.out.printf("%30s|%30s\n","Arcot","100");
You have to insert \n issue a newline character end of first parameter of printf.
More info about using escape character.
This works as expected:
System.out.printf("%30s|%30s%n","Organization","Number of users");
System.out.printf("%30s|%30s%n","Arcot","100");
results in
Organization| Number of users
Arcot| 100
on my machine. You didn't add line feeds. %n is the preferred notation in format Strings.

Categories

Resources