• Post Reply Bookmark Topic Watch Topic
  • New Topic

Save three different inputs from the user on a single line?  RSS feed

 
Fenaj Gordon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I am trying to create an hashtable data structures that will be allow a user to insert, fetch, update, and delete student's information. It's need a lot of work, but the first question that I have is how can I save three different datatypes of input for the third prompt in my program. For example, how would I be able to save id, name, and gpa if they were to input all these answers on single line? Is this a tedious process? I know how to save one type of input such as id. You would do something like this id= input.nextInt(); the input object would come from the Scanner class. . Here is my work so far.





 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That's a way of doing it. I might suggest prompting for each individually.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you take the pattern that Carey passed to split() and pass it to your Scanner's useDelimiter() method, you should be able to call nextInt(), next(), and nextDouble() in whatever order you expect the user to enter the data. You might need to add a call to nextLine() after all those calls to get ready for the next line of input.
 
Fenaj Gordon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:If you take the pattern that Carey passed to split() and pass it to your Scanner's useDelimiter() method, you should be able to call nextInt(), next(), and nextDouble() in whatever order you expect the user to enter the data. You might need to add a call to nextLine() after all those calls to get ready for the next line of input.


@Junilu Lacar-Can you show me an example of what you really mean?
 
Fenaj Gordon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fenaj Gordon wrote:
Junilu Lacar wrote:If you take the pattern that Carey passed to split() and pass it to your Scanner's useDelimiter() method, you should be able to call nextInt(), next(), and nextDouble() in whatever order you expect the user to enter the data. You might need to add a call to nextLine() after all those calls to get ready for the next line of input.


@Junilu Lacar-Never mind. I just have broken them down into three different prompts. Like this then:

System.out.println("Enter a Student's ID: ");
       id= input.next();
     System.out.println("Enter a Student's Name: ");
       name= input2.nextLine();

     System.out.printIn("Enter a Student's GPA: ");
       gpa= input3.nextDouble();

My next question is how can I insert this starting student's information into  put() method inside of my do-while loop?  I know that I can take each variable like id, name, and gpa and print back out the information to them, but how will it work for the put() method. Do I have to actually implement a put method within my Hashtable class. I don't think so because isn't the put, remove, get methods all implicit of the Hashtable  class. So this means that I don't have to actually implement what I want to do inside of those methods, right? I can just go ahead and use them right? Please respond back to me. Because I will have more questions.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fenaj Gordon wrote:I just have broken them down into three different prompts. Like this then:

System.out.println("Enter a Student's ID: ");
       id= input.next();
     System.out.println("Enter a Student's Name: ");
       name= input2.nextLine();

     System.out.printIn("Enter a Student's GPA: ");
       gpa= input3.nextDouble();

My next question is how can I insert this starting student's information into  put() method inside of my do-while loop?  I know that I can take each variable like id, name, and gpa and print back out the information to them, but how will it work for the put() method. Do I have to actually implement a put method within my Hashtable class. I don't think so because isn't the put, remove, get methods all implicit of the Hashtable  class. So this means that I don't have to actually implement what I want to do inside of those methods, right? I can just go ahead and use them right? Please respond back to me. Because I will have more questions.

You have a class with those fields called "Listing" which I would have rather seen called "Student". Anyway, take those three parameters and construct a Listing object and then put() that into the hash table.
 
Fenaj Gordon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Carey Brown wrote:[You have a class with those fields called "Listing" which I would have rather seen called "Student". Anyway, take those three parameters and construct a Listing object and then put() that into the hash table.


Could you clarify what you mean by " . . . then put() that into the hash table?"

 
Fenaj Gordon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Carey Brown wrote:[You have a class with those fields called "Listing" which I would have rather seen called "Student". Anyway, take those three parameters and construct a Listing object and then put() that into the hash table.


Could you clarify what you mean by " . . . then put() that into the hash table?"

Do you mean something like this?

Listing obj1= new Listing(id, name, GPA);

obj1.get();



 
Fenaj Gordon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fenaj Gordon wrote:

Carey Brown wrote:[You have a class with those fields called "Listing" which I would have rather seen called "Student". Anyway, take those three parameters and construct a Listing object and then put() that into the hash table.


Could you clarify what you mean by " . . . then put() that into the hash table?"

Do you mean something like this?

Hashtable data= new Hashtable();
     Listing j;
     Listing l1 = new Listing(1234, "Jerry", 4.0);
     Listing l2 = new Listing(4533, "Marty", 2.3);
     Listing l3 = new Listing(3554, "Sean", 2.9);
   
  
   data.put(l1);
   data.get(l1);
   data.remove(l1);
   j = data.get(l1);



 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fenaj Gordon wrote:Could you clarify what you mean by " . . . then put() that into the hash table?"

I don't quite follow your HTable, but a Java HashMap takes a key and a value. In your case, the key would be the student id, and the value would be the whole student object.


 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!