Reputation: 11
I'm not sure why I can't get this type of concept down. I have to keep it as simple as possible. I am trying to get a list of students in a class, and then display them at the end when the user inputs 2.I've switched it around a few times, but end up at the same point every time. I tried doing if statements for a 1 and a 2 answer, but still didn't get anywhere.
Thank you everyone.
package peopleinclass;
import java.util.Scanner;
public class PeopleinClass {
static Scanner get=new Scanner(System.in);
static String yourName;
static String studentNames = "";
static int answer = 1;
public static void main(String[] args)
{
getNames();
while(true)
{
if(answer == 1 )
{
System.out.println("Would you like to add another student? 1 for yes 2 for no ");
answer=get.nextInt();
System.out.println("Please enter another students name: ");
yourName = get.nextLine();
get.nextLine();
yourName+;
break;
}
}
Display();
}
public static String getNames()
{
System.out.println("Please enter your students name: ");
yourName = get.nextLine();
return yourName;
}
public static void Display()
{
System.out.println("Below are the students in the class: ");
System.out.println(yourName);
}
}
Upvotes: 1
Views: 576
Reputation: 456
It looks like you may be struggling a bit with the problem and your understanding of programming so I have provided a solution below with verbose commenting. I urge you to read this whole post and the content in the links that I have provided underneath the code.
If this is a school/college/university project then do not just take the solution and use it without understanding how it works because you will just continue to struggle throughout your studies. I have provided this code to try and help you understand the problem and how it can be solved. If you need any further information then reply in the comment section below.
package peopleinclass;
import java.util.Scanner;
import java.util.ArrayList;
class PeopleinClass {
private static Scanner get = new Scanner(System.in);
private static int answer = 1;
private static String nextName = null;
public static void main(String[] args)
{
//Initialise an ArrayList of type String.
ArrayList<String> listOfNames = new ArrayList<String>();
//Request the first student name and store the value in
//the nextName variable which is initialised at the
//top of this class.
nextName = requestFirstStudentName();
//Add the value of nextName in to the ArrayList.
listOfNames.add(nextName);
//Create a do...while loop which executes all of the code
//inside the bracers at least once and then continues to
//execute the code based on the while (...) condition at
//the end of the bracers.
do {
System.out.println("Would you like to add another student? 1 for yes 2 for no ");
//Prompt the user to enter a value which we assume is of type
//int. If the value the user enters here is not of type int
//and is a String instead, the program will crash. Type
//checking should be done here to make sure it is an
//int but for simplicity I have left type checking out.
answer = get.nextInt();
//Clear any tokens that are left in the scanner.
get.nextLine();
//If the answer given when prompted for an int (above)
//is equal to 1.
if(answer == 1)
{
System.out.println("Please enter another students name: ");
//Prompt the user to enter another name.
nextName = get.nextLine();
//Add another entry to our ArrayList of type String.
listOfNames.add(nextName);
}
//While the user has given the answer of 1 (above when asked for int)
//go back to the top of the do...while loop and repeat the process
//again.
} while (answer == 1);
//Call the display(...) method, which accepts an ArrayList
//of type String and is defined in this class further
//down.
display(listOfNames);
}
public static String requestFirstStudentName()
{
System.out.println("Please enter your students name: ");
//Prompt the user to enter a name.
nextName = get.nextLine();
//Return the name back to where it was invoked.
return nextName;
}
public static void display(ArrayList<String> listOfNames)
{
System.out.println("Below are the students in the class: ");
//Create a for loop, initialising a variable of type
//int and set it to 0. The rest of the for loop can
//be read as - while i is less than the size of
//the ArrayList of type String, print the next
//String stored in the ArrayList and increment i.
for(int i = 0; i < listOfNames.size(); i++)
{
System.out.println(listOfNames.get(i));
}
}
}
As you can see, I made use of the ArrayList
data structure provided in the java.util.*
package that is provided with the Java language. Read more about ArrayList
and how it works here.
You will also see that I have made use of a do...while
loop. The do...while
loop is provided in quite a few different languages but is not very commonly used, however it fits for the scenario that you have presented. Find more about the do...while
loop here.
I also made use of a for
loop to iterate through the ArrayList
in the display()
method. Find out more about the for loop here.
In the comments I talk about reading the nextInt()
from the Scanner
class and how type checking should be done. The reason I say this is because you are relying on the user entering a number (users are generally idiots and decide to enter a String instead).
If a String
was entered instead of an int
then you would get a java.util.InputMismatchException
so the best approach here would be to ask for the nextLine()
instead and try and convert the line to an int
.
Converting from a String
to an int
can be found here. You will find the answer at that link shows the use of a try...catch
block where, if trying to convert from a String
to an int
doesn't work, it will go to the catch block where you can set the int
to a default value.
Upvotes: 1
Reputation: 1701
yourName+;
can't be compiledstudentNames
but you never assign any value to it.getNames()
method but you never use the return value.I would suggest the following changes:
getNames()
method to the studentNames
variable.yourName+;
line.Display()
method print studentNames
instead of yourName
get.nextLine()
and you are not using it's value. Also concatenate the return value with your studentNames
variable.This looks like a school homework to me, so I didn't post a solution just try to give you some hints.
Upvotes: 1