Step 0 - intro
Comments in code are (generally) a code smell (=small red flag). If you need comments to explain you did something wrong, code needs to read like a story. The same goes for needing dashed lines to seperate code visually.
The entry functions (eg where your code 'starts') should contain very little code and a lot of descriptive functions. Only 'deeper' functions should have the details.
The fact that you needed dashes to get your code 'more' easy to understand is a variant of this code smell.. After step 2 things become more clear, lets work our way to that:
Step 1 - Assessment
I have copied your code and added my own comments what I think as a senior developer. Please read through this first before reading the rest of my answer :) Please read all my added comments.
//package declaration
//## Duh, this is always on top, why does this have docs?
package word_builder; // declares the package to be used in testPackage
//## ^^^--- if a name requires a comment, its a bad name
//----------------------------------------------------------------------------------------------------------------------
//## this line above is a waste of space
//imports
import java.util.Scanner;
//----------------------------------------------------------------------------------------------------------------------
//## this line above is a waste of space
public class rootSuffixCombine{ // creates the rootSuffix class to contain rootSuffix method
// ^-- If you dont know what a class declaration is, please quit
//----------------------------------------------------------------------------------------------------------------------
//## this line above is a waste of space
//methods
public static void rootSuffix(){ // method for combining just a root and a suffix
// ^-- If you dont know what a method is, please quit
//----------------------------------------------------------------------------------------------------------------------
//## this line above is a waste of space
// initial prompt and scanner creation
//## Yes, I can see that? Why did you make my read that line?
System.out.println("Do you want to make a word?");
Scanner partTaker = new Scanner(System.in);
String answer = partTaker.nextLine();
//----------------------------------------------------------------------------------------------------------------------
//## this line above is a waste of space
//variable initialization to start loops
//## Yes, I can see that? Why did you make my read that line?
int x = 1;
int y = 1;
//----------------------------------------------------------------------------------------------------------------------
//## this line above is a waste of space
// outer while loop for taking user input to either continue with program or end it
while (x == 1){
//----------------------------------------------------------------------------------------------------------------------
//## this line above is a waste of space
// affirmative case (continues program, prints results, then asks if you want to make another)
if (answer.equalsIgnoreCase("yes")){ // sets answer and ignores case
//----------------------------------------------------------------------------------------------------------------------
//## this line above is a waste of space
System.out.print("please input \"root\": "); // takes the root
//## ^^-- Yes, I can see that? Why did you make my read that line?
//## ^^-- also: No it doesnt, it prints
String root = partTaker.nextLine();
System.out.print("please input \"suffix\": "); // takes the suffix
//## ^^-- Yes, I can see that? Why did you make my read that line?
//## ^^-- also: No it doesnt, it prints String suffix = partTaker.nextLine();
//----------------------------------------------------------------------------------------------------------------------
//## this line above is a waste of space
// main output
System.out.println("The word you created is: " + "\"" + root.concat(suffix) + "\"");
//----------------------------------------------------------------------------------------------------------------------
//## this line above is a waste of space
//resets loop in case user wants to make another word
// ##############################
// ##############################
// ## I HAVE NOW SPEND ALL MY FOCUS TRYING TO UNDERSTAND, from now on my review would be less proper.
// ##############################
// ##############################
y = 1;
//----------------------------------------------------------------------------------------------------------------------
//## this line above is a waste of space
// second while loop for creating more words
while (y == 1){
//----------------------------------------------------------------------------------------------------------------------
//## this line above is a waste of space
// prompts for second word
System.out.println("Do you want to make another word?");
answer = partTaker.nextLine();
//----------------------------------------------------------------------------------------------------------------------
//## this line above is a waste of space
//affirmative case
if (answer.equalsIgnoreCase("yes")) {
//ends second loop
y = 0;}
//----------------------------------------------------------------------------------------------------------------------
//## this line above is a waste of space
// negative case
else if (answer.equalsIgnoreCase("no")) { //ignores case
// closes second loop
break;}
//----------------------------------------------------------------------------------------------------------------------
//## this line above is a waste of space
//handling non-standard answers
else{
System.out.println("sorry, i dont understand");
//resets loop
x = 1;
y = 1;}}}
// end of second loop
//----------------------------------------------------------------------------------------------------------------------
//## this line above is a waste of space
// negative case (main loop)
else if (answer.equalsIgnoreCase("no")) {
System.out.println("OK, shutting down.");
break;} //ends loop
//----------------------------------------------------------------------------------------------------------------------
//## this line above is a waste of space
// catches non-standard response and repeats initial question until "yes" or "no" is given
else{
System.out.println("I dont understand, try again.");
answer = partTaker.nextLine();}}}
//----------------------------------------------------------------------------------------------------------------------
//## this line above is a waste of space
// main, which runs the method(S)
public static void main(String[] args){
rootSuffix();}}
//----------------------------------------------------------------------------------------------------------------------
//## this line above is a waste of space
//END
If you read all my comments (until I didnt wanted to add anymore), you have read a lot, but didnt really get any wiser and it probally was a chore (at least I hope so, I'm trying to prove a point). You just wasted a lot of mental compute and you havent even got to the detail of the code yet.
Note: This is how your comments feel to us (more experienced users)
Step 2 - Remove clogging
Often less is more in coding. You dont always want a lot of info, you want to understand the code quickly. Now the same code, no comments:
package word_builder;
import java.util.Scanner;
public class rootSuffixCombine
{
public static void rootSuffix(){
System.out.println("Do you want to make a word?");
Scanner partTaker = new Scanner(System.in);
String answer = partTaker.nextLine();
int x = 1;
int y = 1;
while (x == 1){
if (answer.equalsIgnoreCase("yes")) {
System.out.print("please input \"root\": ");
String root = partTaker.nextLine();
System.out.print("please input \"suffix\": ");
System.out.println("The word you created is: " + "\"" + root.concat(suffix) + "\"");
y = 1;
while (y == 1){
System.out.println("Do you want to make another word?");
answer = partTaker.nextLine();
if (answer.equalsIgnoreCase("yes")) {
y = 0;
}
else if (answer.equalsIgnoreCase("no")) {
break;
}
else{
System.out.println("sorry, i dont understand");
x = 1;
y = 1;
}
}
}
else if (answer.equalsIgnoreCase("no")) {
System.out.println("OK, shutting down.");
break;
}
else{
System.out.println("I dont understand, try again.");
answer = partTaker.nextLine();
}
}
}
public static void main(String[] args){
rootSuffix();
}
}
This is a lot smaller! A lot less data to parse, now I actually see what is going on. And now I can see what the real issue is: Code format/styling/single responsibility!
You correctly identified an issue, but solved it incorrectly. What I mean is that your code is not very easy to read. You applied the comments-solution to remedy that, but that wasnt the correct solution.
Step 3 - Move code to dedicated methods
Apply Single responsibility. Well, a part of it. A function should only have one task. Thats a bit of a broad description, but another aproach is "one reason for change". Or a rule I like to use is: Describe what this is doing and if you say 'AND', its wrong.
Also, you are repeating yourself! Everything exists twice?! You added code to prevent issues, but that created a mayor factor for issues: Duplicate code. The follow code does (about) the same as yours!
package word_builder;
import java.util.Scanner;
public class rootSuffixCombine
{
public static void rootSuffix(){
Scanner partTaker = new Scanner(System.in);
while(askIfWantsWord(partTaker)){
askWord(partTaker)
}
}
private Boolean askIfWantsWord(Scanner partTaker){
System.out.println("Do you want to make a word?");
String answer = partTaker.nextLine();
while(true){
if (answer.equalsIgnoreCase("yes")) {
return true;
else if (answer.equalsIgnoreCase("no")) {
System.out.println("OK, shutting down.");
return false;
}
else{
System.out.println("I dont understand, try again.");
answer = partTaker.nextLine();
}
}
}
private Boolean askWord(Scanner partTaker){
System.out.print("please input \"root\": ");
String root = partTaker.nextLine();
System.out.print("please input \"suffix\": ");
System.out.println("The word you created is: " + "\"" + root.concat(suffix) + "\"");
}
public static void main(String[] args){
rootSuffix();
}
}
Please note that I'm not a Java programmer, I might be off a bit.
The mayor improvement is the rootSuffix
method. Its short and super easy to understand. You dont know HOW it works, but you do instantly know what is going on. In 2sec you understand what this does, instead of 2minutes of your code. I dont care about the details at first. I only want details if I care. If I want to know how we ask for a word I only need to check that code then.
Step 4 - Distill more repeated code
There is still a lot of duplicate code: The print-question+partTaker. Distill that into an own method and turn the if/elseif/else into a case:
package word_builder;
import java.util.Scanner;
public class rootSuffixCombine
{
public static void rootSuffix(){
Scanner partTaker = new Scanner(System.in);
while(askIfWantsWord(partTaker)){
askWord(partTaker)
}
}
private Boolean askIfWantsWord(Scanner partTaker){
String answer = getAnswerForQuestion("Do you want to make a word?");
while(true){
switch (answer.toLowerCase()) {
case "yes" -> return true;
case "no" -> return false;
default -> answer = getAnswerForQuestion("I dont understand, do you want to make a word?");
};
}
}
private Boolean askWord(Scanner partTaker){
String root = getAnswerForQuestion("please input \"root\": ");
String suffix = getAnswerForQuestion("please input \"suffix\": ");
System.out.println("The word you created is: " + "\"" + root.concat(suffix) + "\"");
}
private String getAnswerForQuestion(String question){
System.out.print(question);
return partTaker.nextLine();
}
public static void main(String[] args){
rootSuffix();
}
}
This last version reads a lot easier. Each function is easily understandable. You dont need comments because the code now speaks for itself. You dont need to always know the details, only when you care.
Bonus: This now has an easy way to add 'y/n' als legit answer too
Note: The steps above are done a bit...crude, I could've done a better job naming and thinking about it (but its just a quick review for now). I've tried to show an example, but the actual theory generally is a bit more nuanced. You should consider this as a introduction.
This final result might be a bit intimidating, but that is mostly (likely) because this is new to you. This way of programming becomes normal very easily with some practice and from that point on this will read a lot faster than any comment could.