0

Why does the example code not work in Eclipse?

I've written the following example code in Eclipse, yet it shows me error notifications, but without a straight answer to how the variable actually works. This problem occurs on all my variables. I found a similar problem with the print method. SoloLearn says that one should write System.out.printIn ("Hello World"); But Eclipse won't accept that. System.out.print ("Hello World"); does work though. So the app probably need some updating so it can display correct information. My example code is below this.

18th Nov 2016, 7:51 AM
Arterexius
Arterexius - avatar
12 Antworten
+ 2
I cant see your example code. 1st guess: you wrote System.out.printIn ("Hello World"); you should write System.out.println("Hello World"); instead (small L instead of capital i) 2nd guess: you just wrote System.out.println("Hello World"); That wont work on its own. Java is no scripting language. That will maby work on Sololearn, i dont know. But in any regular IDE it will not, because you need a Class with a main function. The so called entrypoint of your application. This will work: public class Testclass { public static void main(String[] args) { System.out.println("Hello World"); } }
18th Nov 2016, 8:04 AM
Roland
+ 2
"When writing In after print" you wrote IN actually, not LN. Its not .PRINTIN() its .PRINTLN() LN is short for "line". so its System.out.println(); copy&paste this if you need ;-) your 2nd Example is fine, but the code does nothing, so you cant really see anything when you run it.
18th Nov 2016, 8:43 AM
Roland
+ 1
I think it does it because you don't use at all the variables, you just declare them but don't do nothing with them. Try to write a print for all of them to see if the error keep going. I may be wrong, but give it a try.
18th Nov 2016, 10:35 AM
Anitei Leonard
Anitei Leonard - avatar
+ 1
assigning a System.out.println(); for each variable seems to work fantastic. Thanks for the help Anitei, this works wonders! :) If anyone else stumbles over this issue as well, then here is the new code: Class Variables { public static void main(String[] args) { String name = "David"; System.out.printIn(name); int age = 42; System.out.printIn(age); double score = 15.9; System.out.printIn(score); char group = 'Z'; System.out.printIn(group); } } The code will print the following in the console: David 42 15.9 Z If anyone whishes to set the variable name up to be printed before their assigned value, then use System.out.printIn("X variable name: "); or copy/paste the following code: Class Variables { public static void main(String[] args) { String name = "David"; System.out.print("Name: "); System.out.printIn(name); int age = 42; System.out.print("Age: "); System.out.printIn(age); double score = 15.9; System.out.print("Score: "); System.out.printIn(score); char group = 'Z'; System.out.print("Group: "); System.out.printIn(group); } } This alteration of the code will print the following in the console: Name: David Age: 42 Score: 15.9 Group: Z Thanks again Anitei for helping me out. My issue is now resolved and I am very thankful for that :)
20th Nov 2016, 6:07 AM
Arterexius
Arterexius - avatar
0
Here's my Example code, which I am struggling with: class Variables { public static void main (String[ ] args) { String name ="David"; int age = 42; double score =15.9; char group = 'Z'; } } Can anyone tell me why "name, age, score, group" doesen't work as I've written it?
18th Nov 2016, 7:57 AM
Arterexius
Arterexius - avatar
0
The code looks fine so the only guess I can make is that there is a problem with eclipse. What error does it exactly gives you ?
18th Nov 2016, 8:21 AM
Anitei Leonard
Anitei Leonard - avatar
0
Thanks Roland :) Sorry for not providing my Hello World code. Here it is as I wrote it, but where printIn wouldn't work, though regular print did. I'm using Eclipse Neon btw. class Hello_World { public static void main(String[] args) { System.out.print("Hello World"); } } I wrote all the L letters with the non capital version of L. When writing In after print, I get a notification icon which looks like a lit lightbulb with a red x in the bottom right corner. Deleting In makes the notification go away. I can see that I have written my Hello World example code almost like your 2nd guess. Adding public before class in the first line did though not change anything. Could the difference be due to me using the Neon version of Eclipse?
18th Nov 2016, 8:22 AM
Arterexius
Arterexius - avatar
0
if you want write the variables separtate by a comma use System.out.printf and before put the formatting codes %s for String %c for char %d for integer number %f for float number try: public class Variables { public static void main(String[] args) { // TODO Auto-generated method stub String name ="David"; int age = 42; double score =15.9; char group = 'Z'; System.out.println(name +" "+age +" "+score+" "+group); System.out.printf("%s%d%f%c\n", name, age, score,group);
18th Nov 2016, 8:49 AM
Simone Novaro
Simone Novaro - avatar
0
It gives me a notification icon which looks like a lit lightbulb with a yellow warning triangle in the bottom right corner. I understand it as an error notification, however I only get these suggestions with a view of what they each will do: Remove 'name' and all assignments: ... public static void main(String[] args) { int age= 42; double score=15.9; ... } } Remove 'name', keep assignments with side effects: ... public static void main(String[] args) { int age= 42; double score=15.9; ... } } Convert local variable to field: ... class Variables { private static String name; public static void main(String[] args) { name = "David"; int age= 42; ... Inline local variable: ... public static void main(String[] args) { int age = 42; double score=15.9; ... } } Rename in file (Ctrl+2, R): Link all references for a local rename (does not change references in other files) Rename in workspace (Alt+Shift+R): Start the Rename refactoring Split variable declaration: ... public static void main(String[] args) { String name; name = "David"; int age= 42; ... Add @SuppressWarnings 'unused' to 'name': ... public static void main(String[] args) { @SuppressWarnings("unused") String name= "David"; int age= 42; ... Add @SuppressWarnings 'unused' to 'main()': ... class Variables { @SuppressWarnings("unused") public static void main(String[] args) { String name= "David"; ... Configure problem severity: Open the Java > Compiler > Errors/Warnings preference page and highlight the problem to configure it's severity. Problem: The value of the local variable name is not used. Eclipse gives me these suggestions to all my variables. I havent clicked any of them because I'm entirely new to coding and I didn't want to mess anything up to a point where I didn't know what was up and down. I am using Eclipse Neon btw :)
18th Nov 2016, 8:54 AM
Arterexius
Arterexius - avatar
0
Thanks again Roland :) That helped a tonne and makes a lot more sense than 'in' did. Thanks for an easily understandable answer, do you have any idea as to why my Variables neither works? Anitei has given me something I will try out, but having more than one solution to test out is always good :) Simone has also posted a solution, but because I haven't coded anything before, then I have close to no idea what all those things stand for. An example of my variables code is posted here in the comments as well :)
18th Nov 2016, 11:45 PM
Arterexius
Arterexius - avatar
0
Thanks a lot Anitei. I'll definitely try that and report back whether it works or not. Thank you too for easily understandable answers to my problems. I deeply appreciate it :)
18th Nov 2016, 11:47 PM
Arterexius
Arterexius - avatar
0
Thanks a lot for the answer Simone :) I do though barely have an idea of what that lineup does aka understand what the individual code means. I'm entirely new to coding, so could you possibly explain it to me? nmw thanks a lot for taking your time to answer me. I really appreciate all of this. I hadn't expected this much help to what probably is quite a nooby problem. Idk ofc, but it might be :)
18th Nov 2016, 11:51 PM
Arterexius
Arterexius - avatar