Getting multiple errors compiling Java code in Eclipse - java

I'm new to programming, and I'm using Eclipse to make some easy programs like calculators and vote counting. (Don't mind the language, it's Portuguese and i'm from Brazil.)
So as you can see in the image with "Questão1.java." Class opened compiles perfectly, and the "Questão2.java" shows quite a lot of errors, and I have absolutely no idea what it means.
This one is giving a lot of errors:
This one compiles perfectly, no errors and results as expected:
Thanks everyone for answering, i found out the error and it was indeed the "printf"...and also i'll remember to never post codes as images next time, again thank you guys.

The problem is that you use printf() instead of print() or println().
printf() adds formatting to whatever you are trying to print, and the String that determines how your output should be formatted uses characters like %, which you also use in the class that throws errors.
The error
Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = ' '
at [and so on...]
at Questao2_Lista[bla bla bla].main(Questao2.java:18)
can be read in the following way:
The first line defines what kind of exception was thrown, in this instance it is an "UnknownFormatConversionException".
The lines below it are called a "stacktrace", that show where exactly the exception was thrown and the "way" it got propagated up the call-stack.
It looks like this is the only error, I can't see any of the other errors you talked about so I guess you just assumed that every line was a separate error?

The problem with "Questão2.java" is the System.out.printf statement. The statement has a '%' which is a special character. % is used as a preceding character for a place holder
Eg: %d in the string will be replaced with a number passed as an argument.
int lines =10;
System.out.printf ("There are %d lines", lines);
Will come as:
There are 10 lines
If its just a print statement Use System.out.println instead of System.out.printf

Try to use System.out.print() or System.out.println() instead System.out.printf() in line 18 of Questão2.java

Related

Are there any circumstances where an empty String would be sent to System.out without calling the print function?

I am currently doing unit tests on the code I wrote, and I noticed something weird happening. My jUnit tests are monitoring System.out and comparing correct values with my code. The thing is, even after searching though my entire project looking for all instances of (and commenting out) System.out.println, the debugger is noting that at some point, the String "" is being sent out. Are there any reasons why the ByteOutput would do this? I thought even creating a string with nothing in it would error out.
I figured it out! The test cases that were handed to me calls the Split function on System.out with the regex as \n. So, whenever I used println instead of print + \n, the string split function adds an empty string to the newly formed array.
Thanks everyone!

Compile attempt gives error that says "error: class, interface, or enum expected", but error points to Chinese character?

I have been attempting to do a HelloWorld for an online Java class I started, but I have run into a weird error. I did look around on this website, and while I did see some similar instances of this error, none of them had the exact same issue that I'm having.
So for my class, I entered the code below (as instructed in the video, copied every letter, bracket, and symbol) into Notepad and saved it, set up javac, and attempted to run a javac.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
Now when I run the javac, it gives me this error.
(The \ shows up like a weird W-like symbol on the command prompt for me, but it still functions properly).
The little arrow should point to the error in the text file, but the problem is it shows up as a Chinese character for some reason. I haven't been able to figure out what causes this, but my guess is it's something beyond the scope of this text document. However, my system doesn't use Chinese, and the system locale isn't in Chinese either, so I have no idea what it could be. I don't think it can be any of the brackets, as they look accurate to me, unless there's something I completely missed. Any help would be greatly appreciated.
It looks like your program file has something in it that is being interpreted as a multi-byte character. The character in error seems to be right before the "p" in public which is why the compiler is giving the error message. It is expecting a keyword and getting a Chinese character.
What editor did you use? I think the real problem is something about how your editor is set up. This error message is just a symptom. Another possibility is that your system is set up for interpreting certain byte sequences as Chinese. That would explain "\ " being interpreted as a character and something starting with a curly brace and going through "p" being seen as a sequence of them.
Windows notepad is the worst txt editor,it will change your line breaks,and it will insert UTF-8 BOM into the text file that you edit.
For your instance,that's why your java file can't compile.
There's no one fixed ANSI encoding - there are lots of them. Usually when people say "ANSI" they mean "the default locale/codepage for my system" which is obtained via Encoding.Default, and is often Windows-1252 but can be other locales.

Replaceall method is not working as expected. Literals are replaced twice instead

when I execute the below code, literals are being replaced twice
public void obify("CREATENEWABCDEFGHTNEW"){
String _obiText=_obi;
_obiText=_obiText.replaceAll("A","OBA");
_obiText=_obiText.replaceAll("E","OBE");
_obiText=_obiText.replaceAll("I","OBi");
_obiText=_obiText.replaceAll("O","OBO");
_obiText=_obiText.replaceAll("U","OBU");
System.out.println(_obiText);
}
Expected output: CROBEOBATOBENOBEWOBABCDOBEFGHTNOBEW ; Replace all the vowels with OB'Vowel Literal'
Actual output: CROBOBEOBOBATOBOBENOBOBEWOBOBABCDOBOBEFGHTNOBOBEW which is undesired and unexpcted.
Is there any other Java function to do the task I intended to do.
What about replacing vowels?
_obiText = _obiText.replaceAll("[AEIOU]","OB$0");
Take a look at what you're doing:
_obiText=_obiText.replaceAll("A","OBA");
here you replace all "A"s with "OBA"s. Fine, works well. Let's keep going.
_obiText=_obiText.replaceAll("E","OBE");
_obiText=_obiText.replaceAll("I","OBi");
All right, a little more...
_obiText=_obiText.replaceAll("O","OBO");
Here you mess up. You've replaced everything before with "OBx", being x the vowel. And of course, the same is happening to the "O"s that you and the replacements have written. So you get a longer text than you expected. The solution? Replace first the "O"s with "OBO"s and then everything else, then it will go fine.
PD: you can say that your result is undesired, as it's not what you want it to do, but not unexpected, because if you read and understand the code that's totally expected.

System.out.println() not working

this is my first post. I am excited to a part of this community and I have been struggling with this problem for a while, so here goes:
In the following code:
if (j == 0)
{
if (!Arrays.equals(cipherData, c))
{
System.out.print("C: ");
for (int i = 0; i < encryptedData.length; i++)
System.out.print((char)cipherData[i]);
System.out.println();
}
}
The System.out.println()
method returns nothing at all. No line, or anything and I have no idea why. The goal is to print a blank line after printing the byte array is printed above when those if conditions are true.
Any help would be much appreciated and welcome.
System.out.print() does not print a newline character.
You're outputting a bunch of stuff, then printing a newline with System.out.println(). This causes the cursor to drop to the next line.
You need another one if you want a blank line after that.
Edit to add: I missed the fact that your for loop conditional is ... different than the array you're printing. Did you mean for that to be the case?
Also, since you're possibly printing non-printable characters, it is completely plausible that you're causing the terminal to be in a state where the newline will no longer work.
What it comes down to is, println() isn't broken. Either it's not getting called, or if you don't see a newline occur when it is called then the terminal is in a state where it no longer recognizes it.
Check
http://docs.oracle.com/javase/6/docs/api/java/io/PrintStream.html#println()
You may need System.out.print('\n');
Before iterating for loop you can check length of encryptedData
System.out.println("encryptedData.length:: "+ encryptedData.length);
if encryptedData.length return greater than 1 then it will go into for loop.
You should debug step by step .
I have concerns about this:
System.out.print((char)cipherData[i]);
Assuming that cipherData is an array of bytes, then casting a byte to a char and printing it via a character stream is not likely to give pretty results. For a start, bytes that are less that 32 decimal will map to ASCII "control characters".
And also you may be printing the wrong array ... or using the length of the wrong array.
(But the explanation for your problem is that you need to call println a second time to be a blank line. The first println is just terminating the line containing the ... umm ... "characters" from your cipher array.)
Sounds like the problem is that you have run a JVM without any standard output attached. Like on Windows, using javaw.exe to run a jar (which is the default, beware). Java.exe outputs to the console window, if you run it from a console, but javaw.exe does not. If you run the program from the file explorer window, even though you are using java.exe, you still won't get any standard out, because it's hidden by windows.
So, run the program in a command line window, and use java.exe, not javaw.exe.

string.equals not working for me

This is the useful part of code:
java.util.List<Element> elems = src.getAllElements();
Iterator it = elems.iterator();
Element el;
String key,value,date="",place="";
String [] data;
int k=0;
Segment content;
String contentstr;
String classname;
while(it.hasNext()){
el = (Element)it.next();
if(el.getName().equals("span"))
{
classname=el.getAttributeValue("class");
if(classname.equals("edit_body"))
{
//java.util.List<Element> elemsinner = el.getChildElements();
//Iterator itinner = elemsinner.iterator();
content=el.getContent();
contentstr=content.toString();
if(true)
{
System.out.println("Done!");
System.out.println(classname);
System.out.println(contentstr);
}
}
}
}
No output. But if I remove the if(classname.equals("edit_body")) condition it does print (in one of the iterations):
Done!
edit_body
"I honestly think it is better to be a failure at something you love than to be a success at something you hate."
Can't get the bug part... help!
I am using an external java library BTW for html parsing.
BTW there are two errors at the start of the output, which is there in both the cases, with or without if condition.:
Dec 20, 2012 11:53:11 AM net.htmlparser.jericho.LoggerProviderJava$JavaLogger error SEVERE: EndTag br at (r1992,c60,p94048) not recognised as type '/normal' because its name and closing delimiter are separated by characters other than white space
Dec 20, 2012 11:53:11 AM net.htmlparser.jericho.LoggerProviderJava$JavaLogger error SEVERE: Encountered possible EndTag at (r1992,c60,p94048) whose content does not match a registered EndTagType
Hope that wont cause the error
Ok guys, Somebody explain me please! "edit_body".equals(el.getAttributeValue("class")) worked!!
I had right now the exactly same problem.
I success to solve it by using: SomeStringVar.replaceAll("\\P{Print}","");.
This command remove all the Unicode characters in the variant (characteres that you cant see- the strings look like equal, even they not really equal).
I use this command on each variant i needed in the equalization, and it works for me as well.
Looks like you are having leading or trailing whitespaces in your classname.
Try using this: -
if(classname.trim().equals("edit_body"))
This will trim any of those whitespaces at the ends.
Firstly, String.equals() is NOT broken. It works for millions of other programs / programmers. This is NOT the cause of your problems (unless you or someone has deliberately modified ... and broken your Java installation ...)
So why can two apparently equal strings compare as unequal?
There could be leading or trailing whitespace characters on the String.
There could be embedded non-printing characters.
There could be pairs Unicode characters that look the same when you display them with a typical font, but in fact are not the same. For instance the Greek code page contains characters that look by Latin vowels ... but are in fact different codes, and hence are not equal.
change the code to:
classname="edit_body"; //<- hardcode
if(classname.equals("edit_body"))
if the code enters the if statement now, then there must obviously be some difference in the string content when you use the original "classname=el.getAttributeValue("class");".
in such case, loop over the individual characters and compare those to find the difference.
If the code still doesnt enter the if statement, either your code is not compiling and you are running old code, or your java installation is broken ;-)
OR.
if java is anything like .net (I don't know java)
is "el.getAttributeValue" typed as string?
if it is typed as object, then the if statement would not enter since those are two different instances of the same string.
equals() is a method of String class. So, it works with double quotes.
if(someString.equals("something")) ✓
if(someString.equals('something')) ×

Categories