Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

1-D Arrays  RSS feed

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please look at the attachment to help me with this code, My code is not working. The only thing it does correctly is ask the user to input 10 last names. The instructions will tell you how my code is supposed to work. I'm new to coderanch so if anyone could help me, it would be much appreciated!


lab.PNG
[Thumbnail for lab.PNG]
 
Bartender
Posts: 5319
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Yo wrote:

You should have gotten some compile errors. Please cut and paste them into your next post.

Your convertNames() method says that it returns a String[] but nowhere are you doing that.

On line 31 you should remove 'new'. convertNames() is a static method so to call it you only need the class name in front of it, which you have.

convertNames() is returning a String[] not EmployeeNames as your usage suggests.
 
Carey Brown
Bartender
Posts: 5319
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My last name is "Brown". How are you supposed to deduce my first and middle initials from that?
 
Sheriff
Posts: 23970
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:My last name is "Brown". How are you supposed to deduce my first and middle initials from that?



You'd have to be "N. W. Brown" according to the specifications.
 
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A assignment like that should never be given. I feel like it teaches you a lot of bad practises
What are you trying to do on line 10?
Actualy it's not that bad of an attempt.
Allthough to get the initials I would use charAt instead of a substring
 
Ana Yo
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know how to do this code? I’m so confused:(
 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You were good on your way. Just take a look at line 10 of your code
 
Marshal
Posts: 62263
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and Welcome to the Ranch

Have you worked out what DD meant about line 10?
 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:
...
convertNames() is returning a String[] not EmployeeNames as your usage suggests.



According the asignment the method shouldn't return a string but the formated array.
You also will need a forEach at the end of your code to print the formated names.
 
Ana Yo
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone help me with the code and make it work? I’ve doing this for hours.
 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lol everyone posting on this topic could, but what would you learn of it?
Show us what you got right now and I'll see if I can give you a nudge in the right direction
 
Carey Brown
Bartender
Posts: 5319
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This shouldn't compile. Did you change it? Did you get it to compile without errors? Re-post your current code.

For the time being just get this one class to compile. I've already mentioned what's wrong with it.
 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carrey has given you several usefull tips, so have I and all the rest that posted...
the return value of your method should be an array, you still need a forEach or regular for loop at the end of your tester class to print the array and they're is something wrong on line 10 of your code.
As I sayed I would have used charAt, but even if you use substring, have a look at the documentation to see how it's used and what kind of parameters it takes
 
Ana Yo
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But idk how to do a for loop to print the elements out. Also do you know how to do the extra credit?
 
Ana Yo
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The extra credit wants me to substitute the kissing last names with N/A if the user did not input 10 last names.
Ex.
Lau
Smith
Manny
Kay
Wim
N/A
N/A
N/A
N/A
 
Ana Yo
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Yo wrote:The extra credit wants me to substitute the missing last names with N/A if the user did not input 10 last names.
Ex.
Lau
Smith
Manny
Kay
Wim
N/A
N/A
N/A
N/A

 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That one I'm prepared to give you, allthough basted on what you have written so far, you should be able to.



to check for Empty values you can in your for loop to assign the names assing the scanner value to a temp variable and then execute a while loop that checks if temp isn't empty, as long it is the console asks the user to put in an other name.
after the while loop you assign temp to yourArray[index]
 
Ana Yo
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that how you would print out the elements in main?
 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That piece of code is a forEach to print the names, if you havent learned forEaches you can use a regular forLoop too.
I updated my previous answer telling you to check for empty vallues too
 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It dosn't say you have to replace the empty values with NA, it say's you have to address the situation when there is one, two ways to go, eather like you said with NA or null or whatever or like I sugested by prompting the user that he has to give in a name, I guess it depends how important it would be that there are 10 names
 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But that all in your main, did you manage to get the formatting done?
 
Carey Brown
Bartender
Posts: 5319
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Yo wrote:But idk how to do a for loop to print the elements out. Also do you know how to do the extra credit?


You didn't respond to my post on compile errors, why are you wasting time thinking about extra credit?
 
Ana Yo
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm getting this as a compile time errors:

EmployeeNames.java:6: error: cannot find symbol
     for(int i=0;i<lastNames.length;i++)
                        ^
 symbol:   variable lastNames
 location: class EmployeeNames

EmployeeNames.java:8: error: cannot find symbol
        String firstInitial = lastNames[i].substring(lastName[i].length()-1);
                                   ^
 symbol:   variable lastNames
 location: class EmployeeNames

EmployeeNames.java:9: error: cannot find symbol
        String middleInitial = lastNames[i].substring(lastName[i].length()-2,length()-1);
                                                                             ^
 symbol:   method length()
 location: class EmployeeNames

EmployeeNames.java:9: error: cannot find symbol
        String middleInitial = lastNames[i].substring(lastName[i].length()-2,length()-1);
                                       ^
 symbol:   variable lastNames
 location: class EmployeeNames

EmployeeNames.java:10: error: cannot find symbol
        formattedNames[i]=firstInitial + "." +middleInitial+"."+lastNames[i];
                                                                                         ^
 symbol:   variable lastNames
 location: class EmployeeNames
5 errors

 
Ana Yo
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Ana Yo wrote:But idk how to do a for loop to print the elements out. Also do you know how to do the extra credit?


You didn't respond to my post on compile errors, why are you wasting time thinking about extra credit?


Sorry I wasn't home
 
Ana Yo
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Ana Yo wrote:

You should have gotten some compile errors. Please cut and paste them into your next post.

Your convertNames() method says that it returns a String[] but nowhere are you doing that.

On line 31 you should remove 'new'. convertNames() is a static method so to call it you only need the class name in front of it, which you have.

convertNames() is returning a String[] not EmployeeNames as your usage suggests.


I thought static methods don't need a return method? Also, I posted the compile time errors for the first class. If I do need to return a String how would I do it?
 
Ana Yo
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:A assignment like that should never be given. I feel like it teaches you a lot of bad practises
What are you trying to do on line 10?
Actualy it's not that bad of an attempt.
Allthough to get the initials I would use charAt instead of a substring


Our teacher only wants us to use substring, we're not allowed to use charAt:(
 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once again it isn't suposed to return a string, but the array of formated names.

This would be in your EmployeeNamesClass:


Your methodCall:


What is your teachers professional experience? The asignment is full of bad pratices and ambiguations, he's constantly refering to a list, while he actually means an array and so on
 
Ana Yo
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My compiler still says the same thing for lastNames as an error.
 
Ana Yo
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:Once again it isn't suposed to return a string, but the array of formated names.

This would be in your EmployeeNamesClass:


Your methodCall:


Is it possible if you can copy and paste my code and somwhow make it work. Java is so hard for me right now because I started this September.

 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
don't use lastname.lenght use:
Since you know your array should have 10 names in them, not that it should mather
 
Ana Yo
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Now I have these errors:
EmployeeNames.java:6: error: cannot find symbol
     for(int i=0;i<lastNames.length;i++)
                         ^
 symbol:   variable lastNames
 location: class EmployeeNames

EmployeeNames.java:8: error: cannot find symbol
        String firstInitial = lastNames[i].substring(lastName[i].length()-1);
                                    ^
 symbol:   variable lastNames
 location: class EmployeeNames

EmployeeNames.java:9: error: cannot find symbol
        String middleInitial = lastNames[i].substring(lastName[i].length()-2,length()-1);
                                                                             ^
 symbol:   method length()
 location: class EmployeeNames
EmployeeNames.java:9: error: cannot find symbol
        String middleInitial = lastNames[i].substring(lastName[i].length()-2,length()-1);
                                       ^
 symbol:   variable lastNames
 location: class EmployeeNames

EmployeeNames.java:10: error: cannot find symbol
        formattedNames[i]=firstInitial + "." +middleInitial+"."+lastNames[i];
                                                                                       ^
 symbol:   variable lastNames
 location: class EmployeeNames
5 errors


 
Ana Yo
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Current code:


 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since it's clear you're not gonna get there on your own, I'll post my code, but please don't just copy, try to understand where it difference from yours and if any questions ask them.
@Mods: please don't shoot me for this :

EmployeeClass:


EmployeeNamesTesterClass:
 
Ana Yo
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:Since it's clear you're not gonna get there on your own, I'll post my code, but please don't just copy, try to understand where it difference from yours and if any questions ask them.
@Mods: please don't shoot me for this :

EmployeeClass:


EmployeeNamesTesterClass:


I'm going to understand it, thank you! If I still have questions I will ask.
 
Ana Yo
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was the error I kept on getting before you helped me:
EmployeeNamesTester.java:13: error: cannot find symbol
        String temp = console.nextline();
                                        ^
 symbol:   method nextline()
 location: variable console of type Scanner

EmployeeNamesTester.java:22: error: incompatible types
     String formattedNames =  EmployeeNames.convertNames(lastNames);
                                                                                         ^
 required: String
 found:    String[]

EmployeeNamesTester.java:23: error: for-each not applicable to expression type
     for(String name : formattedNames)
                              ^
 required: array or java.lang.Iterable
 found:    String
3 errors
 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Yo wrote:This was the error I kept on getting before you helped me:
EmployeeNamesTester.java:13: error: cannot find symbol
        String temp = console.nextline();
                                        ^
 symbol:   method nextline()
 location: variable console of type Scanner


Where that error comes from is a mystery to me, no where in your code I see you assign the scanner.nextLine to temp

Ana Yo wrote:
EmployeeNamesTester.java:22: error: incompatible types
     String formattedNames =  EmployeeNames.convertNames(lastNames);
                                                                                         ^
 required: String
 found:    String[]


That part I have tried to explain several times, the return value of your method is an array, but you were tryong to assign it to a string.

Ana Yo wrote:
EmployeeNamesTester.java:23: error: for-each not applicable to expression type

     for(String name : formattedNames)
                              ^
 required: array or java.lang.Iterable
 found:    String
3 errors


That one is easy, your array of formated names is called  formatedNames, but your iterating through formatedList, which not exist
 
Ana Yo
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry didn't post current code:



 
Ana Yo
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is where my errors are from in the current code.
 
Ana Yo
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Errors:
EmployeeNames.java:8: error: cannot find symbol
        String firstInitial = lastNames[i].substring(lastName[i].length()-1);
                              ^
 symbol:   variable lastNames
 location: class EmployeeNames
EmployeeNames.java:9: error: cannot find symbol
        String middleInitial = lastNames[i].substring(lastName[i].length()-2,lastName[i].length()-1);
                               ^
 symbol:   variable lastNames
 location: class EmployeeNames
EmployeeNames.java:10: error: cannot find symbol
        formattedNames[i]=firstInitial + "." +middleInitial+"."+lastNames[i];
                                                                ^
 symbol:   variable lastNames
 location: class EmployeeNames
3 errors
 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah it's because your scanner is already empty, you assigned it's value to temp, so it would be: not
 
Everybody! Do the Funky Monkey! Like this tiny ad!
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!