You are Here:
Download Linux Lite 5.2 today - See Release Announcements



Learning Java Programming on Linux

Author (Read 9239 times)

0 Members and 1 Guest are viewing this topic.

Learning Java Programming on Linux
« on: July 23, 2017, 04:55:01 AM »
 

Homework

  • New to Forums
  • *
  • 16
    Posts
  • Country: 00
  • Reputation: 2
  • Linux Lite Member

  • Linux Lite: 3.2 64bit

  • CPU: Intel(R) Core(TM) i5-2410M CPU @ 2.30GHz

  • MEMORY: 6Gb
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]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?
Last Edit: July 25, 2017, 10:48:45 AM by Homework
 


Re: Learning Java Programming on Linux
« Reply #1 on: July 23, 2017, 06:05:37 AM »
 

bitsnpcs

  • Platinum Level Poster
  • **********
  • 3181
    Posts
  • Country: 00
  • Reputation: 300

  • Linux Lite: 3.2 64bit
I don't know how to write Java, have you tried changing -
Code: [Select]
last;} 
change to 
Code: [Select]
last};
Last Edit: July 23, 2017, 06:34:11 AM by bitsnpcs
 

Re: Learning Java Programming on Linux
« Reply #2 on: July 24, 2017, 08:42:23 AM »
 

Homework

  • New to Forums
  • *
  • 16
    Posts
  • Country: 00
  • Reputation: 2
  • Linux Lite Member

  • Linux Lite: 3.2 64bit

  • CPU: Intel(R) Core(TM) i5-2410M CPU @ 2.30GHz

  • MEMORY: 6Gb
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.
 

Re: Learning Java Programming on Linux
« Reply #3 on: July 24, 2017, 10:34:54 AM »
 

bitsnpcs

  • Platinum Level Poster
  • **********
  • 3181
    Posts
  • Country: 00
  • Reputation: 300

  • Linux Lite: 3.2 64bit
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.
 

Re: Learning Java Programming on Linux
« Reply #4 on: July 24, 2017, 01:29:46 PM »
 

firenice03

  • Rockin' the FREE World
  • Global Moderator
  • Platinum Level Poster
  • *****
  • 1497
    Posts
  • Country: us
  • Reputation: 261
  • Linux Lite Member

  • Linux Lite: 4.8 64bit

  • CPU: AMD E2//Atom X5//AMD Phenom II X2

  • MEMORY: 4Gb

  • VIDEO CARD: AMD Mullin Radeon R2//Intel//AMD/ATI RS880

  • Kernel: 5.x
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")
LL 4.8 UEFI 64 bit ASUS E402W - AMD E2 (Quad) 1.5Ghz  - 4GB - AMD Mullins Radeon R2
LL 4.8 UEFI 64 bit Test UEFI Kangaroo (Mobile Desktop) - Atom X5-Z8500 1.44Ghz - 2GB - Intel HD Graphics
LL 4.6 64 bit HP 6005- AMD Phenom II X2 - 8GB - AMD/ATI RS880 (HD4200)
LL 3.8 32 bit Dell Inspiron Mini - Atom N270 1.6Ghz - 1GB - Intel Mobile 945GSE Express
RETIRED LL 2.8 64 bit Dell Optiplex 160 (Thin) - Atom 230 1.6Ghz - 4GB-SiS 771/671 PCIE VGA
Running Linux Lite since LL2.2
 

Re: Learning Java Programming on Linux
« Reply #5 on: July 24, 2017, 03:53:06 PM »
 

justme2

  • PayPal Supporter
  • Forum Regular
  • *****
  • 201
    Posts
  • Country: gb
  • Reputation: 25
  • Linux Lite Member

  • Linux Lite: 3.8 64bit

  • CPU: Intel i5 vPro

  • MEMORY: 8Gb

  • VIDEO CARD: Intel
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?
Last Edit: July 24, 2017, 03:54:45 PM by justme2
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.
 

Re: Learning Java Programming on Linux
« Reply #6 on: July 25, 2017, 10:10:58 AM »
 

Homework

  • New to Forums
  • *
  • 16
    Posts
  • Country: 00
  • Reputation: 2
  • Linux Lite Member

  • Linux Lite: 3.2 64bit

  • CPU: Intel(R) Core(TM) i5-2410M CPU @ 2.30GHz

  • MEMORY: 6Gb
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......
 

Re: Learning Java Programming on Linux
« Reply #7 on: July 25, 2017, 10:29:15 AM »
 

Homework

  • New to Forums
  • *
  • 16
    Posts
  • Country: 00
  • Reputation: 2
  • Linux Lite Member

  • Linux Lite: 3.2 64bit

  • CPU: Intel(R) Core(TM) i5-2410M CPU @ 2.30GHz

  • MEMORY: 6Gb
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.
 

Re: Learning Java Programming on Linux
« Reply #8 on: July 25, 2017, 11:19:29 AM »
 

Homework

  • New to Forums
  • *
  • 16
    Posts
  • Country: 00
  • Reputation: 2
  • Linux Lite Member

  • Linux Lite: 3.2 64bit

  • CPU: Intel(R) Core(TM) i5-2410M CPU @ 2.30GHz

  • MEMORY: 6Gb
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?
 

Re: Learning Java Programming on Linux
« Reply #9 on: July 25, 2017, 03:41:50 PM »
 

justme2

  • PayPal Supporter
  • Forum Regular
  • *****
  • 201
    Posts
  • Country: gb
  • Reputation: 25
  • Linux Lite Member

  • Linux Lite: 3.8 64bit

  • CPU: Intel i5 vPro

  • MEMORY: 8Gb

  • VIDEO CARD: Intel
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?

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.
 

Re: Learning Java Programming on Linux
« Reply #10 on: July 25, 2017, 08:02:14 PM »
 

bitsnpcs

  • Platinum Level Poster
  • **********
  • 3181
    Posts
  • Country: 00
  • Reputation: 300

  • Linux Lite: 3.2 64bit
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.
Last Edit: July 25, 2017, 10:43:34 PM by bitsnpcs
 

Re: Learning Java Programming on Linux
« Reply #11 on: July 25, 2017, 11:32:37 PM »
 

Homework

  • New to Forums
  • *
  • 16
    Posts
  • Country: 00
  • Reputation: 2
  • Linux Lite Member

  • Linux Lite: 3.2 64bit

  • CPU: Intel(R) Core(TM) i5-2410M CPU @ 2.30GHz

  • MEMORY: 6Gb
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.
 

Re: Learning Java Programming on Linux
« Reply #12 on: July 26, 2017, 10:21:07 AM »
 

Homework

  • New to Forums
  • *
  • 16
    Posts
  • Country: 00
  • Reputation: 2
  • Linux Lite Member

  • Linux Lite: 3.2 64bit

  • CPU: Intel(R) Core(TM) i5-2410M CPU @ 2.30GHz

  • MEMORY: 6Gb
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!!).
 

Re: Learning Java Programming on Linux
« Reply #13 on: July 26, 2017, 10:45:51 AM »
 

bitsnpcs

  • Platinum Level Poster
  • **********
  • 3181
    Posts
  • Country: 00
  • Reputation: 300

  • Linux Lite: 3.2 64bit
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
 

Re: Learning Java Programming on Linux
« Reply #14 on: July 26, 2017, 12:29:59 PM »
 

torreydale

  • PayPal Supporter
  • Platinum Level Poster
  • *****
  • 1564
    Posts
  • Country: us
  • Reputation: 254
  • * Forum Moderator *

  • Linux Lite: 4.8 64bit

  • CPU: Intel Core i5-5300U (-HT-MCP-)

  • MEMORY: 8Gb

  • VIDEO CARD: Intel Broadwell-U Integrated Graphics

  • Kernel: 4.x
@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.
Want to thank me?  Click my [Thank] link.
 


Tags:
 


Download Linux Lite 5.2 today - See Release Announcements