Linux Lite Forums

Development => Coding => Topic started by: Homework on July 23, 2017, 04:55:01 AM

Title: Learning Java Programming on Linux
Post by: Homework on July 23, 2017, 04:55:01 AM
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: [Select]
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: [Select]
[email protected]:~/Documents/java$ javac NameTest.java
[email protected]:~/Documents/java$ javac Name.java
[email protected]:~/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?
Title: Re: Learning Java Programming on Linux
Post by: bitsnpcs on July 23, 2017, 06:05:37 AM
I don't know how to write Java, have you tried changing -
Code: [Select]
last;} 
change to 
Code: [Select]
last};
Title: Re: Learning Java Programming on Linux
Post by: Homework on July 24, 2017, 08:42:23 AM
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: [Select]
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.
Title: Re: Learning Java Programming on Linux
Post by: bitsnpcs on July 24, 2017, 10:34:54 AM
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.
Title: Re: Learning Java Programming on Linux
Post by: firenice03 on July 24, 2017, 01:29:46 PM
Giving it a quick look... The part listed...
Code: [Select]
new Name("Cookie" + "Monster" + "Jr")Although you have spacing between " + there isn't any coded spaces..


Maybe try, see if it results properly
Code: [Select]
new Name("Cookie " + "Monster " + "Jr")
Title: Re: Learning Java Programming on Linux
Post by: justme2 on July 24, 2017, 03:53:06 PM
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?
Title: Re: Learning Java Programming on Linux
Post by: Homework on July 25, 2017, 10:10:58 AM
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......
Title: Re: Learning Java Programming on Linux
Post by: Homework on July 25, 2017, 10:29:15 AM
Giving it a quick look... The part listed...
Code: [Select]
new Name("Cookie" + "Monster" + "Jr")Although you have spacing between " + there isn't any coded spaces..


Maybe try, see if it results properly
Code: [Select]
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: [Select]
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: [Select]
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.
Title: Re: Learning Java Programming on Linux
Post by: Homework on July 25, 2017, 11:19:29 AM
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?
Title: Re: Learning Java Programming on Linux
Post by: justme2 on July 25, 2017, 03:41:50 PM
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/ (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?

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.
Title: Re: Learning Java Programming on Linux
Post by: bitsnpcs on July 25, 2017, 08:02:14 PM
Hello,
I use the gedit and terminal for the Python book I use, I had a look on Google it says gedit can be used for Java with the terminal.

I think they call it ethical because it is legal like pentesting.
Title: Re: Learning Java Programming on Linux
Post by: Homework on July 25, 2017, 11:32:37 PM
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.

Hi justme2, Lite's text editor has this option too but it did not work for me. Thanks for suggesting it though.

Hello,
I use the gedit and terminal for the Python book I use, I had a look on Google it says gedit can be used for Java with the terminal.

I think they call it ethical because it is legal like pentesting.


Thanks bitsnpcs, I'll install gedit later this evening and try it out.
Title: Re: Learning Java Programming on Linux
Post by: Homework on July 26, 2017, 10:21:07 AM
Just realised I made a mistake with the codes while I was re-watching the tutorial and trying out gedit.
The erroneous code:
Code: [Select]
Name myName = new Name("Cookie" + "Monster" + "Jr");

The correct code:
Code: [Select]
Name myName = new Name("Cookie", "Monster", "Jr");
Programming really puts attention to details to the test.
Thank you to everyone who responded and mulled over this issue (when it's due to my carelessness, sorry!!).
Title: Re: Learning Java Programming on Linux
Post by: bitsnpcs on July 26, 2017, 10:45:51 AM
Do you find gedit okay for your needs ?

@torreydale told me about gedit

You can use custom templates, I use one, it helped me to like the gedit a lot more as the colours were easier for me, @LL-user  showed me how to install it in this thread  https://www.linuxliteos.com/forums/coding/beginning-with-python/msg29409/#msg29409
Title: Re: Learning Java Programming on Linux
Post by: torreydale on July 26, 2017, 12:29:59 PM
@bitsnpcs is correct.  I don't think Leafpad, the default text editor, is going to be sufficient for coding.  Gedit or Geany (if you need column mode) are going to be better for you.
Title: Re: Learning Java Programming on Linux
Post by: Homework on July 30, 2017, 02:30:27 AM
@bitsnpcs Yes, I've been using gedit for 2 lessons now and the coloured codes are very useful in helping me to differentiate between the texts. I've chosen a darker colour scheme on gedit so it's easier on my eyes. Thanks very much for the recommendation! :D
Title: Re: Learning Java Programming on Linux
Post by: bitsnpcs on July 30, 2017, 04:42:49 AM
@Homework glad to pass the help along, @torreydale recommended to me.
I also use darker theme, is called monokai-extended. I find the coloured codes very helpful too :)
Great work, keep up your lessons ;)