Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Input into another class (I think)

 
Marshal
Posts: 79531
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stan Austin wrote:Haha, im not sure what assert even does, but they are there. and i've just ignored them.

Uness you use the -ea tag when executing the code, those assertions don't do anything.

. . . this code just runs on no compiler errors. . . .

Does it really? I can see several compiler errors in it.
 
Ranch Hand
Posts: 50
1
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Your code indentation is terrible. And you should have spent 2 minutes on fixing that before you post. No excuse, at all  


okay i will work on it, however i did spend about 5 mins adding indentation just for you, after your responce earlier in the topic, i had actually noted it and attempted to ensure it was better this time.
however i wonder if you know how to help me or just point out my short comings as a newbie
 
Saloon Keeper
Posts: 10835
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Stan Austin
Ranch Hand
Posts: 50
1
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:This is your culprit
i = i++



ahh now im getting errors .... and lots of them :'(
Thanks though at least I know where to start picking from (Y)
 
Stan Austin
Ranch Hand
Posts: 50
1
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
New code; still not compiling, the Error is on line 24 erroneous tree type: <any>
Also says cannot find class, person (still line 24) and "value" required "class" found.
I have tried re-writing it in several ways but it then just doesn't call the method, is my method signature wrong too?
(reason the longest name is i+2 is because every other argument will be an string and others an int that is allowed right?)

Hopefully this is much better formatting. i am using netbeans Formatting here and have created it as it's own little program not an excerpt of the bigger thing, so hopefully that helps.
Any pointers on this?


 
Marshal
Posts: 8884
638
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think lines of your posted code are not in synch with what you have in your Netbeans, presumably because you have posted just a snippet of code. In different words, error occurs at line 24 as compiler says, but we don't see here that line 24.

Line 20 in the posted code contains compiler error too, you can't pass 'person[]' like that. That shouldn't have '[]'.

And yes, indentation a bit better, not ideal yet, but better

Somehow I feel you need organize yourself a bit more. Problem is in my opinion, that you solving this exercise in a trial and error way. Randomly trying things and expecting to see if it works - that is usually not a good way, at least not so efficient, so you shouldn't expect quick results.

Try to read some literature on relevant topics, so you'd feel a bit more confident. There are Oracle tutorials (google for it), could learn a lot from there.
 
Liutauras Vilda
Marshal
Posts: 8884
638
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Another observation. If from the command line arguments you get an array of names, you don't actually need the loop to put everything to 'person' array.
You can simply do: String[] person = args; In fact, you don't even need that, you can pass just 'args' to your method.

2. Going further. You don't need to pass argslength as an argument and have it as a paremeter in longest_name() method, as you can identify that from the already passed array. Less arguments - better.

3. Method name longest_name isn't conventional, it supposed to sound 'longestName'.

Q: What are you trying to achieve with this line of code?
I understand what this line technically does, but what are you trying to achieve with this in general?
As well as you have such line:
This is something uncommon, verify you understood correctly given requirements.
 
Stan Austin
Ranch Hand
Posts: 50
1
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:I think lines of your posted code are not in synch with what you have in your Netbeans, presumably because you have posted just a snippet of code. In different words, error occurs at line 24 as compiler says, but we don't see here that line 24.


yes sorry i missed the top lines package and public class to save space... (as such it moved everything up 4 lines)

Liutauras Vilda wrote:Line 20 in the posted code contains compiler error too, you can't pass 'person[]' like that. That shouldn't have '[]'.


So simple but effective, thank you i will note this down!

Liutauras Vilda wrote:And yes, indentation a bit better, not ideal yet, but better


Thank you i will work on this once my schedule clears up abit

Liutauras Vilda wrote:Somehow I feel you need organize yourself a bit more. Problem is in my opinion, that you solving this exercise in a trial and error way. Randomly trying things and expecting to see if it works - that is usually not a good way, at least not so efficient, so you shouldn't expect quick results.


haha in all honesty I have been so crazy busy at work, coding has just been forgotten about for the last 3 weeks, (and I only started 7 ago :/ not a good start really) yet I am meaning to keep up, so this is a slap-dash attempt to put the program together by any means...
as i have fallen behind, it could be lack of organisation, but essentially i have bitten off more than i can chew, also programming isnt as easy as i thought it would be

Liutauras Vilda wrote:Try to read some literature on relevant topics, so you'd feel a bit more confident. There are Oracle tutorials (google for it), could learn a lot from there.


I will get onto this I'm confident i will have more time after xmas, so that is the plan.
But thank you for pointing out my error, hopefully I will get it finished in time to actually sleep before work :'D (thanks again)
 
Stan Austin
Ranch Hand
Posts: 50
1
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:1. Another observation. If from the command line arguments you get an array of names, you don't actually need the loop to put everything to 'person' array.
You can simply do: String[] person = args; In fact, you don't even need that, you can pass just 'args' to your method.


I did originally do this but i had lots "more" errors occur so thought since they are there then i will just call them.

Liutauras Vilda wrote:2. Going further. You don't need to pass argslength as an argument and have it as a paremeter in longest_name() method, as you can identify that from the already passed array. Less arguments - better.


is this person[].size?

Liutauras Vilda wrote:3. Method name longest_name isn't conventional, it supposed to sound 'longestName'.


explicit instruction from tutor

Liutauras Vilda wrote:Q: What are you trying to achieve with this line of code?
I understand what this line technically does, but what are you trying to achieve with this in general?
As well as you have such line:
This is something uncommon, verify you understood correctly given requirements.


so I'm going to split this section of main into 2, if there are even args, read as described in code... however if there is an odd number of args then i will have one remaining... this i will treat as an outlier and print using the toString method in Employee, on its own... it's not elegant but it should be effective (and probably an extra 25 lines of code than needed :'D)

so the array should be for example  {bob, 123, john, 1234, steve, 12345, mike, 4}
so the next step is a function that add all the odd array inputs
 
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stan Austin wrote:Hopefully this is much better formatting. i am using netbeans Formatting here and have created it as it's own little program not an excerpt of the bigger thing, so hopefully that helps.
Any pointers on this?


No, it's not any much better. Have you tried using the NetBeans keyboard shortcut to automatically format code? From what most of those search results say, I guess that would be Alt+Shift+F (Windows) or Ctrl+Shift+F (MacOS X). That automatically formats all the code in your source file using the default Java style, usually Kernighan and Ritchie style, aka "K&R Style."

Use that feature often until you kind of get a sense for what properly formatted code looks like (and even then, it's much easier to just keep using the keyboard shortcut -- let your tool do the heavy lifting of the minutiae).
 
Liutauras Vilda
Marshal
Posts: 8884
638
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stan Austin wrote:is this person[].size?

No.

Now the question is how to know that. Well, there is a website, Java 8 API (<- click there). Once you there, click CTRL + A (assuming you're on Windows) and type in Array (locate to it). Over there look for method which tells you about array length.

From Java 9 there should be a nice search function in the API website, but currently you need to do some effort in order to locate to the right place and find what you need. When programming, have an API website open and search through the class you're working with methods, that way you'll get familiar with standard Java's functionality. Quite often you'll notice, that there are already implemented methods you might want, you just would need to use it.

Searching and knowing where to find information yourself is a good and helpful thing - that way you don't need to wait until somebody answers (or not) to your question.

Please tell us what you found.
 
Campbell Ritchie
Marshal
Posts: 79531
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stan Austin wrote:. . . My course is sponsored by my employer . . .

Did they choose the course or did you? Maybe have a quiet word with your line manager and suggest a different choice of course might be beneficial for staff in future.

As for what assert does: unless you use the -ea option when you execute that code, it doesn't do anything. Without -ea the assertions are not even loaded into memory.
 
Campbell Ritchie
Marshal
Posts: 79531
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stan Austin wrote:. . .

Liutauras Vilda wrote:And yes, indentation a bit better, not ideal yet, but better


Thank you i will work on this once my schedule clears up abit. . . .

Lots of people say that. It is a bit like saying, “I'll check my oil and tyre pressures when I have a bit of time.” I know people who ended up with an enormous garage bill by doing that. You shoul‍d be indenting your code from the word go. The amount of effort that takes will be overwhelmed by the confusion you get when incorrect indentation obscures one of your mistakes. Remember: you are the person who most needs to find your mistakes and you are probably also the person who needs the most help finding mistakes. Also, you can set up options on your text editor that will make the process much easier. Look at the three links in this old post of mine.
 
Campbell Ritchie
Marshal
Posts: 79531
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:. . . From Java 9 there should be a nice search function in the API website . . .

Oh good. It wasn't too difficult to navigate the API documentation when it contained 2000 classes but now there are about 4200 of them, it is hard work even finding the name of the class.

And I seem to have mentioned the -ea tag twice. Sorry.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code has all sorts of problems and shouldn't compile.  Can you post your complete code with proper formatting?
 
Liutauras Vilda
Marshal
Posts: 8884
638
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:Can you post your complete code with proper formatting?


@OP

I've spent a good amount of time looking for this link http://javaranch.com/style.jsp
Allocate one evening studying it (with all sub links) so you'd understand importance of it and wouldn't make formatting and indentation errors again.
 
Campbell Ritchie
Marshal
Posts: 79531
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I quote that every now and again. There are some things in it which I disagree with, however.
 
Liutauras Vilda
Marshal
Posts: 8884
638
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Knute Snortum wrote:Can you post your complete code with proper formatting?


@OP

I've spent a good amount of time looking for this link http://javaranch.com/style.jsp
Allocate one evening studying it (with all sub links) so you'd understand importance of it and wouldn't make formatting and indentation errors again.


And once you finish that, read this post (it even earned some cattle), where you'll find a good amount of tips how to get braces right. Technique goes along suggested braces placing style as in the link I wrote.

After you accomplish those, you should be proficient at least on indentation and formatting topics, which is a skill too.
 
I'm still in control here. LOOK at this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic