• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble with If statements (also having String/int Scanner input troubles)  RSS feed

 
Benjamin Lee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to everyone. Short intro: I'm completely new to Java. The last foray I had in programming was 10 years ago with Visual Basic 6. I was so terrible that I ran away from coding. Now I want to learn how to code in Java, so that I can transition to writing Android app software. I'm really excited to be here, because every thread seems filled with helpful people and valuable insight.

Please go easy on me, my only knowledge of Java is about 5 to 8 hours of java tutorials on YouTube, and about fifty bookmarks and twenty open tabs. I do plan on getting a couple of books, soon.
Also, I did use the forum search function, but I could really use help tailored more towards the project that I'm working on; I think it would help me greatly!

Right now, my code compiles and runs, despite protests from Eclipse. I'm having trouble when the user inputs integers instead of strings. Maybe there is a function that I am not aware of, so that when the user types in an int instead of a String, I can tell the user to try again and loop have the code loop back to the original question. In fact, one of my biggest issues is getting the logic to loop back to an original question, without breaking everything. I tried researching switch and cases, but that was daunting. My code is, I'm sure, wildly inefficient. It definitely lacks logic.

Basically this program creates some "celebrities" and then asks you to find out which one you match with.


Without further ado, here is my code:






Thanks again!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

That code looks as if you had tried to do too much all at once, I am afraid. I think you are going to have to take it apart into small pieces and start again. For the time being forget about the Scanner. You have go so much else to think about, that trying to do all those things simultaneously will simply confuse you.
I suggest you start by getting your Person class into proper object‑oriented form. All fields private. All fields set up in the constructor (possibly using the this.name = name; idiom). All fields accessible only via getXXX methods. Details available via the toString method. Of course that means you will have to write the main method again because none of that code will work any more.

Also please tell us which you tube videos you have been watching; there is some very good stuff on youtube and some very bad stuff and beginners may not recognise the difference.
 
Benjamin Lee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice

https://youtu.be/WPvGqX-TXP0?list=PLGLfVvz_LVvSX7fVd4OUFp_ODd86H0ZIY - Derek Banas "30 minute intro to Java"

https://www.udemy.com/java-tutorial/#/ - John Purcell "Java Tutorial for Complete Beginners"

These, especially Purcell's tutorials, are what I've been using most.

Some of my questions have been answered by:
- https://docs.oracle.com/javase/tutorial/
- http://www.dotnetperls.com/keyword-java


Looking back at some of Purcell's tutorials, it looks like I was trying to put the cart before the horse.

I'll get to work on a new rendition.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Benjamin Lee wrote: . . .
- http://www.dotnetperls.com/keyword-java
. . ..
That contains several errors. And an incomplete list of keywords is worse than useless. If you use that list, you will never understand why writing short doesn't compile. There is a complete (but not quite alphabetical) list of keywords in the Java® Language Specification. I note that site suggests you learn about classes like Optional. That means the site is up to date. But I think the examples of the recent code given are difficult to understand because there isn't enough explanation. Some of the code shown is poor style.
I haven't looked at the Udemy tutorials, but Udemy has a good reputation.
The Derek Banas video is too fast for beginners to follow, and it has some minor errors, e.g. use of the constructor, meaning of the protected keyword. I think it proceeds too quickly to be any good, I am afraid.

Anyway, what have you managed with your Person class?
 
Benjamin Lee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ritchie, thanks for the replies, I've been trying to figure out how to apply your recommendations, but I am terribly confused.

So, in lieu of posting every itty bit of code I've managed to eke out by slamming my face into my keyboard, here is what I have for my Person class and the main method:




I'm not sure what fields are supposed to be private. I'm not sure why you want me to use the toString method. I've watched multiple tutorials on getters and setters and I can't figure out how they work or why I need them.

I'm confused as to what you wanted me to do Am I on the right track, at all?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All fields should be private, in all classes, except those used as global constants, e.g. this.
Create getXXX methods for every field.
The reason for a toString method is that you can write
System.out.println(p);
…where p is a reference to a Person object. Since the Object class has a toString method, you can print out every object, but if you don't override the toString method your output will be confusing. I shall give you a start.Don't add a line end character to the end of your String returned.

And that code is a great improvement over what you had before
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use the this. idiom, you can neaten your constructor like this:- That has the advantage that you are “exposing” the better names for those variables which you chose for inside the class. If you use the same names and don't use this. it will &helliip;

I shall let you find out for yourself.
 
Benjamin Lee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my new code.



Seriously Ritchie, I really appreciate your help and guidance. I can finally wrap my head around the getters, setters, and even toString.

Is there anything I need to clean up, as far as formatting, or maybe I did something inefficiently?
Also, I was wondering what you think I should try to implement next?

I'm thinking I should:
import the scanner
create a method for the user to input a new celebrity
create a method for the user to ask the system to list all of the celebrities stored (including the new ones)

Or am I getting ahead of myself?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do with a few blank lines between successive methods, and a bit of tidying up of the identation, but isn't that a lot better That is exactly what I meant.

Have you tested the toString method? It will work but will give you a surprise.
Have you come across Formatter#format, System.out.format, System.out.printf and String#format methods. They all work similarly. You can read a bit about them in the Java™ Tutorials. There are two sections: 1 2 maybe more. You can write this sort of thing for the toString method:-The format of the arguments to String#format as the same as you would use for System.out.printf or System.out.format. Look at the 2nd link I posted.

Try to return one line if possible without a %n at its end.
 
Benjamin Lee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my updated code. I'm not sure what better indentations would look like, though I am definitely NOT a fan of how it currently displays.
I ran across printf and formatting earlier in my tutorials, but my memory was vague until I read through those links. Thanks for those

Anyways, here is my updated code.


This is a fun project, so far I just wanted to come up with something that would allow me to upgrade it as I learned, and so here we are.

I've got some stuff to look up, obviously.
Here are some ideas that I think I can implement into this program, after I scour Oracle's documentation:

1. Output the console to a .txt file (allowing me to see the list of celebrities)
2. Enable the user to input a new celebrity and then see that reflected in the console, and eventually on an exported .txt file. (Do I need to set up arrays for this?)
3. Import a list of celebrities from a .txt file
4. Enable the user to find their matching celebrity


If there are other things I should try to learn, or implement, I'm all ears! I am good clay, just waiting to be molded
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

(1) All instance variables declare right below the class, rather than above specific methods. (2) Then declare constructor. (3) Then methods, separated by an empty line after each
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider whether you want setXXX methods. You might not want to change those variables at all.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!