Reputation: 41
Basically I am looking for what my question asks. My ABook
class looks like this:
import java.io.*;
import java.util.*;
public class ABook
{
public static void main (String args[])
{
LinkedList addressBook = new LinkedList();
Scanner input = new Scanner(System.in);
System.out.println("Would you like to add a friend? (Say Y or N)");
String reply = input.nextLine();
if(reply.equals("Y"))
{
System.out.println("What is the name of your friend?");
String name = input.nextLine();
System.out.println("What is the age of your friend?");
int age = input.nextInt();
Friend newFriend = new Friend(name,age);
addressBook.add("Name: " + newFriend.name + "; " + "Age: " + newFriend.age);
System.out.println("This is your Address Book so far: " + addressBook);
}
else if(reply.equals("N")){
System.out.println("Thank you for your time");
}
}
}
And in case if you need the Friend
class that I use here, this is it:
public class Friend
{
public String name;
public int age;
public Friend(String n, int a)
{
name = n;
age = a;
}
}
I am very unsure whether or not I use a "do" or "while" loop or how to use it. It would be awesome if you explained why or how what loop would work over the other.
Thank you.
Edit: I did not realize how active this community was before I posted this question, and I did not think that I would get as many answers as I did in such a short period of time. So, I figured out my own way to loop it before I saw your replies by using a do-while loop that looked like this.
import java.io.*;
import java.util.*;
public class ABook {
public static void main(String args[]) {
LinkedList addressBook = new LinkedList();
Scanner input = new Scanner(System.in);
int n = 0;
do {
System.out.println("Would you like to add a friend? (Say Y or N)");
String reply = input.nextLine();
if (reply.equals("Y")) {
System.out.println("What is the name of your friend?");
String name = input.nextLine();
System.out.println("What is the age of your friend?");
int age = input.nextInt();
Friend newFriend = new Friend(name, age);
addressBook.add("Name: " + newFriend.name + "; " + "Age: " + newFriend.age);
System.out.println("This is your Address Book so far: " + addressBook);
n++;
} else if (reply.equals("N")) {
System.out.println("Thank you for your time");
System.out.println("Would you like to know who is in your Address Book? (Say Y or N)");
String userinput = input.nextLine();
if (reply.equals("Y")) {
System.out.println("This is your Address Book so far: " + addressBook);
System.out.println("Goodbye!");
} else if (reply.equals("N")) {
System.out.println("Okay! Goodbye!");
}
n = 101;
}
} while (n < 100);
}
}
It works well, and I even got to fit another if - else statement in this. What I am trying to do now is to sort the linkedlist by the integer/age of the "friend", so it can be in order from youngest to oldest when I print the linkedlist. If anyone could point me in the right direction, that'd be awesome!
Anyways, Thank you for all who came to help me, and I hope this can help others that come across looking for answers.
Upvotes: 1
Views: 94
Reputation: 2571
As stated already the difference between a "do while" and "while" loop is that a do-while loop executes the condition at the bottom at the loop, which in turn means that there will be at least one execution of the code.
If you want to read up more on that, this is a good place to go.
As far as your code is concerned. You should use a "while" loop, with a boolean value as a flag for whether the user wants to continue to add or not. Your code will look something like this at the end:
import java.io.*;
import java.io.BufferedReader;
import java.util.*;
public class ABook{
public static void main (String args[])
{
try
{
LinkedList addressBook = new LinkedList();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in) );
System.out.println("Would you like to add a friend? (Say Y or N)");
String reply = br.readLine();
boolean addMore = true;
if(reply.equals("Y")){
addMore = true;
}else if(reply.equals("N")){
addMore = false;
}
while(addMore)
{
System.out.println("What is the name of your friend?");
String name = br.readLine();
System.out.println("What is the age of your friend?");
int age = Integer.parseInt(br.readLine());
Friend newFriend = new Friend(name,age);
addressBook.add("Name: " + newFriend.name + "; " + "Age: " + newFriend.age);
System.out.println("This is your Address Book so far: " + addressBook);
System.out.println("Would you like to add a friend? (Say Y or N)");
reply = br.readLine();
if(reply.equals("Y")){
addMore = true;
}else if(reply.equals("N")){
System.out.println("Thank you for your time");
addMore = false;
}
}
System.out.println("Thank you for your time");
}catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
That should solve and answer your question. Please note that the "Scanner" function doesn't allow for a pause for input, so it will not work for what you need it for.
All the best.
And please do let me know of the outcome :)
Good luck!
Upvotes: 2
Reputation: 489
Using while loop seems to be the best option here although you could use do-while as well. But if you use do-while it would run the loop block atleast once even if the first input for the reply variable is anything other than "Y".
String reply = input.nextLine();
while(reply.equals("Y"))
{
System.out.println("What is the name of your friend?");
String name = input.nextLine();
System.out.println("What is the age of your friend?");
int age = input.nextInt();
Friend newFriend = new Friend(name,age);
addressBook.add("Name: " + newFriend.name + "; " + "Age: " + newFriend.age);
System.out.println("This is your Address Book so far: " + addressBook);
System.out.println("Would you like to add a friend? (Say Y or N)");
String reply = input.nextLine();
}
Explanation: If the first input for the reply variable is "Y" the control will enter the while loop. The last line again prompts for input before checking the condition for the while again. The code inside the loop will execute as many times as "Y" is entered for the reply variable.
Upvotes: 1
Reputation: 11163
Use do-while
loop when you want to do the task at least once no matter whether the condition is true or false. The structure of do-while
loop is-
do{
//do something
}while(condition);
Use while
loop otherwise - if the condition true then the loop executed. The structure of while
loop is-
while(condition){
//do something
}
Upvotes: 3