Hi!I'm new in java so I really need your help.
The problem is how do I return a value if I made another choices again and again..

here's the expected output:

Enter size of class:
Choose Action:
[1]Enter a Student
[2]Enter list of student
[3]Exit
Enter choice[1-3]


I created some of the code but not yet finished:(

import java.util.Scanner;
public class studentlist {
	public static void main(String []args){
		Scanner sc=new Scanner(System.in);
		String student[]=new String[50];
		
		int size,choice,x,i;
		
		
		System.out.println("Enter size of class");
		size=sc.nextInt();
		
		System.out.println("Choose Action:");
		System.out.println("[1]Enter a Student");
		System.out.println("[2]Enter list of Student");
		System.out.println("[3]Exit");
		
	
		System.out.println("Enter Choice[1-3]" + "");
		choice=sc.nextInt();
		
		if(choice==1)
		{
			System.out.println("Enter a Student:");
			for(x=0;x<size;x++)
			{
				student[x]=sc.next();
				
			}
			
			
		}

Recommended Answers

All 12 Replies

how do I return a value if I made another choices again and again..

How do the additional choices effect what value is returned? Do you return one value for each choice or one value after all the choices have been made?

Normally you talk about returning a value when talking about a method. I don't see any methods in your code.

My prof is a mess.He makes projects for us without even teaching anything except for HISTORY!!I'm into method by myself so if you're gonna asked me about where I studied then I'll be glad to answer you on "YOUTUBE UNIVERSITY." Thanks for a tip. had a little idea

Should return one value after all the choices been made.

Member Avatar for ztini
import java.util.HashSet;
import java.util.Scanner;

public class StudentList {

	private static HashSet<String> students = new HashSet<String>();
	private static Scanner in = new Scanner(System.in);
	
	public static void addStudent() {
		System.out.print("Enter Student: ");
		students.add(in.next());
	}
	
	public static void addStudentList() {
		System.out.print("Student List Size: ");
		int size = in.nextInt();
		for (int i = 0; i < size; addStudent(), i++);
	}
	
	public static void viewStudentList() {
		for (String student : students)
			System.out.println(student);
	}
	
	public static void displayMenu() {
		char selection = 0;
		while (selection != 'Q' && selection != 'q') {
			System.out.print(
					"[1] Enter Student" + "\n" +
					"[2] Enter List of Students" + "\n" +
					"[3] View All Students" + "\n" +
					"[Q] Exit" + "\n\n" +
					"Choose Action: ");
			switch (selection = in.next().charAt(0)) {
				case '1': addStudent(); break;
				case '2': addStudentList(); break;
				case '3': viewStudentList(); break;
			}
			System.out.println();
		}
		System.out.println("Good-Bye");
	}
	
	public static void main(String[] args) {
		displayMenu();		
	}
}

That should probably do it. I used a HashSet instead of an Array. This keeps duplicates out and you don't have to worry about size.

commented: Don't just hand students homework solutions. -3

that'ssss great.
but why not allow him to learn something? don't just hand out code, provide him with tips. not to mention that you're comparing chars in your sollution, where the job description would rather suggest an integer.

since the OP has trouble printing lines and checking for the right return, IMHO using HashSets and such is a bit out of his/her grasp, and will only make it harder for him/her to understand.

Member Avatar for ztini

since the OP has trouble printing lines and checking for the right return, IMHO using HashSets and such is a bit out of his/her grasp, and will only make it harder for him/her to understand.

That's your opinion.

At what point is it acceptable to teach someone HashSets? Perhaps you have a guide for how and when people can learn new concepts? ...

Where in your code do you document how and why to use a HashSet? In fact where do you document anything in your code?

And how about this line of code? Is this a good technique to teach:
students.add(in.next());

Read a token and stuff it into the collection with no edit or testing if the name already existed in the collection. I'd call that a sloppy technique.

Member Avatar for ztini

Read a token and stuff it into the collection with no edit or testing if the name already existed in the collection. I'd call that a sloppy technique.

This isn't some enterprise system, its a student assignment. Its fair to assume good data in. If not, encapsulate some exceptions if you like. Instead of just belly-aching, why not show the OP how to sterilize data in.

I'm not the one posting code for an example. I'd say the same to a student if he posted code like yours. I'd expect better from an experienced programmer.

You could have commented your code with:
// Its fair to assume good data in

to show that normally this would not be a good technique but a quick and dirty way for now

Member Avatar for ztini

Relax, this is simple stuff. You under estimate the OP's. The code I looked at never had comments and I still learned it (amazing!). Now as a professional, I never read comments...I read code. Its really not as important as you're making it out to be.

That's your opinion.
.

ehm.. that's basically what the OP said in the original post.

Now as a professional, I never read comments...I read code.

as a 'professional', if you were working at one of the companies I've worked for (up until now) you'd have been fired. if you know what comment do, I assume that since you call yourself a 'professional' you do... you should be able to understand the purpose and advantages.

after all, understanding code is a lot easier to learn (especially for someone who is not all to familiar with the language) if comments explaining what is expected and what the code does.

off course, providing code can help one to learn a language, but then we're talking about code snippets that show a functionality of a part, not a custom made piece of code with a 'hey, pass your course for free' ticket attached to it.

To prevent further discussion and going off topic on use of comments, there are always two sides of coin.

One which says that you should write comments for methods, classes and generally places when you think it would be beneficial to others.

The other side is intentional naming and coding in the way that reader understand from method name, provided variable names and return what is the purpose of the method (same applies for classes, packages, etc). If you want to read more on that pick up Implementation Patterns or start new discussion.

Now we have to wait if OP will actual comeback with something else to ask/say

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.