• 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

exercise for my school with methods

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!I am John!Nice to meet you all!
i am 16 years old and i am new to Java.i started studying it 1 month ago at school.Until now i liked it but i think its the first time i have problem with an exercise.It may be a simple exercise but i think i am lost!

The exercise is a simple one..it's a school with students and courses.i must write a program in which a user must insert data on Students class and Courses class as many times as  he wants.In the main class should be a menu to give you a choice...
i am at school now..i will try to upload later what i have done until now and tell you where my problem is.

The only requirement my teacher wants is to use methods...

I am soory for the big post..i will return later with my code and thanks for your patience!
if you can help me it would be awesome!
 
Marshal
Posts: 7264
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, John!

Whenever you are ready.
 
Saloon Keeper
Posts: 6416
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you do get around to posting your code here, please UseCodeTags (<== that's a link). Also, if you are getting any error messages, cut and paste the *entire* error message into your post.

Welcome aboard.
 
Johny Sougiospitos
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the delay..Today was a very difficult day at school.I had exams!And it became harder since my teacher added some other things to this exercise..Anyway i want to keep it as simple as possible so i can solve my queries..
what i hava study in Java until now is  how to create variables-scanner-arrays and arraylists (some things) and these days we domsetters/getters and constructors (i think they are difficult) and methods.The simpler version of this exercise was to have 3 classes School,Students,Teachers.The last 2 Classes with variables etc and what the exercise wants was the user to make entries in these classes and print them as list..And in the main class should be a menu.
What i have done until now is that i created the classes and the scanner part,i will show you the code for one of the classes since i believe it;s the same for the other one too...

so the main class is,
Student class..

 
Carey Brown
Saloon Keeper
Posts: 6416
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good start. For the most part, excellent formatting. Watch your use of braces though, you need to be consistent. Choose one style or the other.
K&R style
Alman style
Often Alman style is recommended for beginners because it is easier to spot brace mismatches.
 
Marshal
Posts: 66189
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Johny Sougiospitos wrote:. . . so the main class is . . .

Don't get the notion that there is such a thing as a “main class”. Don't think that one class is more important than all the others; that isn't true.
Don't start coding from the centre of the application. Start coding from the periphery. Get your individual classes working first, before you put them together. For each class create an XXXDemo class which executes a single class and tests its methods. When you have it working, you can be confident you can use your class anywhere else.
 
Johny Sougiospitos
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i put my student class too..
i created the class with the variables and the setters/getters and i have 2 problems..
the first one is the Date--i am working on it
the second one is the one the exercise asks..How am i supposed to save the inputs and where to create the list?What can i use??That;s where i am confused!

Any help would be awesome!

As you saw the menu has methods since my teacher told us to use them..so the method insertStudentInfo() should run the scanner in the Student class (i hope so)


 
Johny Sougiospitos
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


for the date of birth can i use something like this inside scanner?




And i still don't understand what to do to save a list with all students names...
 
Johny Sougiospitos
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
?
 
Carey Brown
Saloon Keeper
Posts: 6416
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Johny Sougiospitos wrote:

A few things wrong with this.
On line 2, what are you making the Scanner from. Presumably you wanted System.in.
On line 2 you declare a variable 'date'.
On line 4 you are using posh quotes instead of basic quotes.
On line 6 you declare 'date' again.
 
Carey Brown
Saloon Keeper
Posts: 6416
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One Scanner gotcha is that you should only ever open a single instance of a Scanner using System.in IN THE ENTIRE PROJECT. The easiest way to guaranty this is to create it as a static constant in one of the classes in your project and then have everywhere else reference that constant if it needs to.

And when you need it, some variation of
Also, never ever close a scanner made from System.in, even if the compiler warning messages tell you to.
 
Johny Sougiospitos
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the original Scanner for Student should be like this:


i forgot to write it all.Sorry!it's just that i am trying to solve this exercise many hours now and i can't find a solution.Perhaps i am stupid and i can't do anything...I am tired now but i must solve it somehow
 
Carey Brown
Saloon Keeper
Posts: 6416
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't say what's wrong. Are you getting an error message? Does it not behave the way you want?
 
Carey Brown
Saloon Keeper
Posts: 6416
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't give parse() a formatter then it will expect 'yyyy-MM-dd' which is not what your prompt is asking for. Please read the javadocs for the parse method.
 
Johny Sougiospitos
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i will look again for date..
What about the list of students?
one silly question...right now the menu (as it is) runs once.Is there a way not to stop running?What i mean?If the user want to make a second entry how can i change the menu so the user can continue ?
 
Carey Brown
Saloon Keeper
Posts: 6416
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make a menu choice, e.g. '0' that is exit, if the user selects anything else then do that thing and loop back around. If they select exit then return.

In one place you use 'input' in others you use 'keyboard', which is it? You should not have both. And in 'Students' you have yet another 'key'. You should only have one per entire project.
 
Johny Sougiospitos
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok..i will correct them tomorrow morning.I am like a zombie now..I do not understand what i am doing.SO,goodnight and i hope tomorrow is a better day for all of us (and i pray to solve this exercise)
 
Johny Sougiospitos
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
goodmorning!
i wrote the menu for Student but it doesn't run.It throws error at date:


i get the error :

Exception in thread "main" java.time.format.DateTimeParseException: Text '1985-6-23' could not be parsed at index 5
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDate.parse(LocalDate.java:400)
at java.time.LocalDate.parse(LocalDate.java:385)
at asxeto.Asxeto.main(Asxeto.java:22)
 
Johny Sougiospitos
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Did you try: 1985-06-23


The specific date is working..nothing else
 
Johny Sougiospitos
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Where is 's' defined and set?


i have also Student Class
 
Carey Brown
Saloon Keeper
Posts: 6416
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
      System.out.println("FirstName: "+s.getFirstName()+" LastName: "+s.getLastName()+" DateOfBirth: "+s.getDateOfBirth()+" Fees: "+s.getFees());
 
Johny Sougiospitos
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:       System.out.println("FirstName: "+s.getFirstName()+" LastName: "+s.getLastName()+" DateOfBirth: "+s.getDateOfBirth()+" Fees: "+s.getFees());



ok..i fixed the code..you can check it
 
Carey Brown
Saloon Keeper
Posts: 6416
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


 
Johny Sougiospitos
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so it's working for you??
i am still getting the error here.
one question though..
when the user put the date can i check if it is valid?At least for months and days..can i write for example


Also,i have 2 problems now...As it is now..thw scanner works once...if the user wants to put a second entry how can i do that?
and the problem that still remains..how i can i save all these inputs so i can print a list of them?
 
Carey Brown
Saloon Keeper
Posts: 6416
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Johny Sougiospitos wrote:so it's working for you??
i am still getting the error here.

I provided some missing code, could that be it? Can you post your session input/output?
 
Carey Brown
Saloon Keeper
Posts: 6416
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:One Scanner gotcha is that you should only ever open a single instance of a Scanner using System.in IN THE ENTIRE PROJECT. The easiest way to guaranty this is to create it as a static constant in one of the classes in your project and then have everywhere else reference that constant if it needs to.

And when you need it, some variation of
Also, never ever close a scanner made from System.in, even if the compiler warning messages tell you to.


In this way you only open one instance of the Scanner.
 
Johny Sougiospitos
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Johny Sougiospitos wrote:so it's working for you??
i am still getting the error here.

I provided some missing code, could that be it? Can you post your session input/output?



here is what i get:
][/code]
 
Johny Sougiospitos
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:expecting 2 digits for the month


That was the problem?
Oh my God!i was ready to hit my head!
 
Carey Brown
Saloon Keeper
Posts: 6416
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really need to define your own formatter then you can do "yyyy-M-d" which will take one or two digits.
 
Johny Sougiospitos
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One problem solved another is out....
The part where the user inputs the data must become a method.
so this method will be at Student class and will be called in the menu right?(Well,that was the original idea)
as you saw at scanner part i created the "Student s = new Student( firstName, lastName, dateOfBirth, fees );"

That phrase should be in the method or not?Should i just keep it in Student class and somehow through that sentence save them in a list?

I do not know why this exercise is so confusing
 
Johny Sougiospitos
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:You really need to define your own formatter then you can do "yyyy-M-d" which will take one or two digits.


Basically i do not know how to do that
 
Carey Brown
Saloon Keeper
Posts: 6416
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Johny Sougiospitos
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:My turn for sleep. ZZZZZZzzzzzzzz................


Goodnight and thanks for all the help
 
Johny Sougiospitos
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now,how the input can be saved??
 
Carey Brown
Saloon Keeper
Posts: 6416
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Now that you have the Students in a list, what do you want to do with them?
 
Johny Sougiospitos
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i put them in the list i want to print the list..the problem is i don't know the way to do it
 
Campbell Ritchie
Marshal
Posts: 66189
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start with this sort of thing:-Make sure that Student overrides toString().
 
Seriously Rick? Seriously? You might as well just read this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!