Posts: 16
Threads: 2
Joined: Jul 2017
Reputation:
0
07-23-2017, 08:55 AM
(This post was last modified: 07-25-2017, 02:48 PM by Homework.)
Hello, I'm new to Linux Lite OS and programming.
At the moment, I'm learning Java programming from a course at Udemy and I've encountered a problem with empty spaces in outputs.
Here's part of the codes:
Code: public String toString()
{
return first + " " + middle + " " + last;
}
Name myName = new Name("Cookie" + "Monster" + "Jr");
System.out.println("myName: " + myName.toString());
Then I compiled the codes and here's what it's shown on the Terminal:
Code: mei@mei-Aspire-4750:~/Documents/java$ javac NameTest.java
mei@mei-Aspire-4750:~/Documents/java$ javac Name.java
mei@mei-Aspire-4750:~/Documents/java$ java NameTest
myName: CookieMonsterJr
All the spaces are appearing right before the name. I'm using Linux's Text Editor. Could this the reason why the codes are not working properly?
Posts: 3,237
Threads: 125
Joined: Jul 2014
Reputation:
0
07-23-2017, 10:05 AM
(This post was last modified: 07-23-2017, 10:34 AM by bitsnpcs.)
I don't know how to write Java, have you tried changing -
change to
Posts: 16
Threads: 2
Joined: Jul 2017
Reputation:
0
Hello bitsnpcs, thank you so much for replying to almost all my posts.
Unfortunately this error message appeared when I tried changing the position of the semicolon:
Code: Name.java:39: error: ';' expected
return first + " " + middle + " " + last
^
1 error
This course is conducted using Windows OS, notepad, and CMD. Since my Windows OS crashed unexpectedly, I decided to carry on the course on Linux Lite. There are other web resources out there that recommended Vim, emacs or DrJava to learn Java on Linux platform, so I just want to confirm if this issue is really because I'm using Lite's text editor.
Posts: 3,237
Threads: 125
Joined: Jul 2014
Reputation:
0
Hello Homework,
Hopefully someone will answer you soon about this.
Linux Lite is more stable than Windows, it will be good for your course learning, once the issue is solved.
I looked at the Udemy where you learn, I liked the beginners Ethical Hacker course contents, it is not free though.
Posts: 1,849
Threads: 21
Joined: Sep 2015
Reputation:
0
Giving it a quick look... The part listed...
Code: new Name("Cookie" + "Monster" + "Jr")
Although you have spacing between " + there isn't any coded spaces..
Maybe try, see if it results properly
Code: new Name("Cookie " + "Monster " + "Jr")
LL4.8 UEFI 64 bit ASUS E402W - AMD E2 (Quad) 1.5Ghz - 4GB - AMD Mullins Radeon R2
LL5.8 UEFI 64 bit Test UEFI Kangaroo (Mobile Desktop) - Atom X5-Z8500 1.44Ghz - 2GB - Intel HD Graphics
LL4.8 64 bit HP 6005- AMD Phenom II X2 - 8GB - AMD/ATI RS880 (HD4200)
LL3.8 32 bit Dell Inspiron Mini - Atom N270 1.6Ghz - 1GB - Intel Mobile 945GSE Express -- Shelved
BACK LL5.8 64 bit Dell Optiplex 160 (Thin) - Atom 230 1.6Ghz - 4GB-SiS 771/671 PCIE VGA - Print Server
Running Linux Lite since LL2.2
Posts: 201
Threads: 21
Joined: Jun 2014
Reputation:
0
07-24-2017, 07:53 PM
(This post was last modified: 07-24-2017, 07:54 PM by justme2.)
I'm not familiar with java either, but one thing springs to mind - I think windows and linux use different line terminators in text editors, CRLF and LF. Could that be the problem?
1) Lenovo T520 i5 LL3.8 8GB ram, fast & stable
2) Medion P4 32bit LL3.8 1GB ram, quite fast & stable
3) eeePC 901 32bit LL3.8 1GB ram, fast & stable
4) eeePC 701 32bit LL3.8 1GB ram, slower & stable but small and light enough to travel with me to New Zealand when visiting family in Blenheim.
Posts: 16
Threads: 2
Joined: Jul 2017
Reputation:
0
(07-24-2017, 02:34 PM)bitsnpcs link Wrote: I looked at the Udemy where you learn, I liked the beginners Ethical Hacker course contents, it is not free though.
My cousin was sponsored by her company to take this course recently, and we have a laugh at it thinking how is hacking ever ethical! ;D
But jokes aside, my course isn't free as well......
Posts: 16
Threads: 2
Joined: Jul 2017
Reputation:
0
(07-24-2017, 05:29 PM)firenice03 link Wrote: Giving it a quick look... The part listed...
Code: new Name("Cookie" + "Monster" + "Jr")
Although you have spacing between " + there isn't any coded spaces..
Maybe try, see if it results properly
Code: new Name("Cookie " + "Monster " + "Jr")
Hello firenice03, thank you for your suggestion. It did work, but there are still extra empty spaces that appeared in front of the name due to the toString method.
newName is supposed to appear as indicated by the toString method which has a space in between first, middle, and last name. The idea of the exercise is to to input each name alone without any spaces just like anyone filling up a field on a form etc., that's why toString method is used.
There are two Java files and they both worked together, and here's the first set of code (called Name.java) that sets the foundation (if that's how programmer(s) put it?):
Code: public class Name
{
private String first;
private String middle;
private String last;
// constructor methods allow us to declare class objects and provide those objects with some data.
public Name(String f, String m, String l)
{
first = f;
middle = m;
last = l;
}
public Name(String f, String l)
{
first = f;
middle = "";
last = l;
}
public Name(String l)
{
first = "";
middle = "";
last = l;
}
//default constructor - it's a good idea to add one that doesn't have data in them
public Name()
{
first = "";
middle = "";
last = "";
}
public String toString()
{
return first + " " + middle + " " + last;
}
}
And here's the other file that has the data called NameTest.java:
Code: public class NameTest
{
public static void main(String[] args)
{
// instantiation - creating an instance of the Name class
Name myName = new Name("Cookie" + "Monster" + "Jr");
Name yourName = new Name("Great" + "Cookie");
Name aName = new Name("Durr");
System.out.println("myName: " + myName.toString());
System.out.println("yourName: " + yourName.toString());
}
}
NameTest.java would not work without Name.java. These are the two files in their entirety. Hope this would give clarity to what the codes are supposed to do.
Posts: 16
Threads: 2
Joined: Jul 2017
Reputation:
0
(07-24-2017, 07:53 PM)justme2 link Wrote: I'm not familiar with java either, but one thing springs to mind - I think windows and linux use different line terminators in text editors, CRLF and LF. Could that be the problem?
Hello justme2, thank you for pointing out the difference!
So I found this article: https://blog.codinghorror.com/the-great-newline-schism/, unfortunately I'm still none the wiser on how this would affect Lite text editor processing the empty spaces.
Windows text editor save encoding as ANSI, while Lite text editor save encoding as UTF-8. Could this be the reason for this problem?
Posts: 201
Threads: 21
Joined: Jun 2014
Reputation:
0
(07-25-2017, 03:19 PM)Homework link Wrote: [quote author=justme2 link=topic=4444.msg33814#msg33814 date=1500925986]
I'm not familiar with java either, but one thing springs to mind - I think windows and linux use different line terminators in text editors, CRLF and LF. Could that be the problem?
Hello justme2, thank you for pointing out the difference!
So I found this article: https://blog.codinghorror.com/the-great-newline-schism/, unfortunately I'm still none the wiser on how this would affect Lite text editor processing the empty spaces.
Windows text editor save encoding as ANSI, while Lite text editor save encoding as UTF-8. Could this be the reason for this problem?
[/quote]
I have no idea if that is the problem, but the text editor I use (Leafpad) has an option to set the required line terminator to CRLF or just LF. I can only suggest trying it as I do not use Java.
1) Lenovo T520 i5 LL3.8 8GB ram, fast & stable
2) Medion P4 32bit LL3.8 1GB ram, quite fast & stable
3) eeePC 901 32bit LL3.8 1GB ram, fast & stable
4) eeePC 701 32bit LL3.8 1GB ram, slower & stable but small and light enough to travel with me to New Zealand when visiting family in Blenheim.
|