-2

Okay, so I'm in java Object Oriented Programming and I'm stuck on one little thing on a project.

I have to create a class that holds a student name, calculates the total score and calculates the average score. But what's holding me up is that I need to create an object, that is called by the name that is given to me from input from the scanner.

I also am not 100% sure how to get the information from the program to the class, I think I just put them in the variable name from the name, but if I'm wrong, please tell me.

What I have so far is:

public class Prog2 { public static void main(String[] args) {

Scanner input = new Scanner (System.in);

Student name = new Student();


System.out.println("Please enter the name of the student.");    
String theName = input.nextLine();
name.setName(theName);
System.out.println();

System.out.printf("Name of the object is", name.getName());


}

}

Right now I want to see I I can get the name in there. I also need to name the project the same name as the name that's given to me.

Zoroark73
  • 1
  • 1
  • Why not create an array of the objects, then you can loop through to get what you want. Stay away abit okay and get things done. – Young Emil Sep 24 '16 at 00:41
  • 1
    It's unclear what you're asking, but it sounds like you need to start by writing the `Student` class, based on the requirements stated in the assignment. Also, have a look at Java tutorials on [creating objects](https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html) (and around). Ask another question when you have some code to show. – Mick Mnemonic Sep 24 '16 at 00:42
  • 1
    Objects don't have names. Unless you mean its student name field. – Sotirios Delimanolis Sep 24 '16 at 00:43
  • 1
    So, if I'm right, you need to create a Student object from data received from the command line/user input. You'll want to use `Scanner`, `Keyboard`, or the `String[]` argument of the `main` method to receive input, and to put that data into a student object, you'll need to transfer it either through a method, or through the constructor. If "constructor" and "method" have no meaning for you, check out [the official tutorials](https://docs.oracle.com/javase/tutorial/), because those (and fields and variables) are some of the most basic parts of Java. – Socratic Phoenix Sep 24 '16 at 00:47
  • **Be specific!** As you can tell from the comments and answers, it's not clear what you're asking. Re-read your post, looking for pronouns and vague nouns, then [edit] your question so it's clear what you want. (Your post has a recurring problem with "name" and "the name" --- name of what? Did you mean `theName`, or something else? Also "class" --- a Java class, or a school class the student's scores came from?) Also, **show us your code**. For starters, what does that `Student` class look like? Even if it's just `public class Student {}`, it must be defined _somewhere_. – Kevin J. Chase Sep 24 '16 at 03:09

1 Answers1

0

It's not very clear what you are asking, you are asking how to create a student object and set that objects name field to the input given from the scanner?

One way to pass information from the program to the class is to create a constructor that sets the fields value upon object creation:

Student thisStudentObject = new Student(inputName, inputGrade)

Of course this is dependant upon the Student class having a contructor that matches those data types (in that order). for more information on constructors, see https://stackoverflow.com/a/19941847/4064652

Community
  • 1
  • 1
Dallas S
  • 11
  • 3
  • While this isn't a _bad_ answer --- and it might even help the OP --- be warned that answering questions as vague as this one is often a waste of your time. The OP might edit his or her question into something mostly unlike what you tried to answer, or the question could be downvoted, closed, and eventually deleted... taking your answer with it. You're betting your spare time that the question, should it ever become clear, will be the one your answer predicted (which _does_ occasionally work out). – Kevin J. Chase Sep 24 '16 at 03:24