Results 1 to 9 of 9
  1. #1
    carlodelmundo is offline Member
    Join Date
    Aug 2008
    Posts
    31
    Rep Power
    0

    Default Scanner Issues (User Input: Very Simple)

    Java Code:
    import java.util.*;
    
    public class AssignmentTest {
    	
    	public static void main (String[] args)
    	{
    		
    		Scanner sc = new Scanner(System.in);
    		
    		ArrayList<Assignment> carlosAssignment = new ArrayList<Assignment>();
    		
    		String response1, response2;
    		int response3; 
    		boolean userEscape = true;
    		
    		do
    		{
    		System.out.println("What would you like to do?");
    		System.out.println("If you want to add an assignment, say '1'");
    		System.out.println("If you want to view an assignment, say '2'");
    		System.out.println("If you want to remove an assignment, say '3'");
    		System.out.println("If you want to sort assignments by due date, say '4'");
    		System.out.println("If you want to sort assignments by the order they were assigned, say '5'");
    		System.out.println("If you want to quit, say '6'");
    
    		response3 = sc.nextInt();
    
    		
    		
    				System.out.println("Type the day the assignment is due.");
    
    				response1 = sc.nextLine();
    
    				
    				
    				System.out.println("Now type the name of the assignment");
    				response2 = sc.nextLine();
    
    		
    		
    		
    		}
    		while(userEscape = false);
    		
    	}
    }
    Hi. Basically, I want to make a program that will list, add, remove, and sort an assignment. The current problem is... I'm having a hard time with the Scanner class. Specifically, at the statement: response2 = sc.nextLine();

    I input a digit... for example, 1.

    Then, instead, of doing just: System.out.println("Type the day the assignment is due.");

    it does both of the printstreams. (Type the day the assignment is due and now type the name of the assignment).

    As a result, I can only type in the name of the assignment and not the day. How can I fix this? Thanks!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default

    Go to the API doc for Scanner and read carefully what readLine() does. Compare that to what next() does.

  3. #3
    carlodelmundo is offline Member
    Join Date
    Aug 2008
    Posts
    31
    Rep Power
    0

    Default

    Hahahaha! I didn't know it was that easy. Thanks Norm!

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default

    An old programming maxim:
    When in doubt read the manual!
    Or is it:
    When all else fails read the manual!

    Scanner will do a lot of things for you and has a lot of methods that you can use. You need to choose the right one for the job.

  5. #5
    carlodelmundo is offline Member
    Join Date
    Aug 2008
    Posts
    31
    Rep Power
    0

    Default

    Java Code:
    import java.util.*;
    
    public class AssignmentTest {
    	
    	public static void main (String[] args)
    	{
    		
    		Scanner sc = new Scanner(System.in);
    		
    		ArrayList<Assignment> carlosAssignment = new ArrayList<Assignment>();
    		
    		String response1;
    		String response2, response4 = "";
    		int response3; 
    		boolean userEscape = false;
    		
    		do
    		{
    		System.out.println("\nWhat would you like to do?");
    		System.out.println("If you want to add an assignment, say '1'");
    		System.out.println("If you want to view an assignment, say '2'");
    		System.out.println("If you want to remove an assignment, say '3'");
    		System.out.println("If you want to quit, say '4'");
    
    		response3 = sc.nextInt();
    		
    		
    		switch (response3)
    		{
    		case 1:
    		{
    			System.out.println("Type the day the assignment is due.");
    			response1 = sc.next();
    					
    			System.out.println("Now type the name of the assignment");
    			response2 = sc.next();
    			
    			carlosAssignment.add(new Assignment(response1, response2));
    			
    			System.out.println("You entered: \"" + response1 + "\", and \"" + response2 + "\".");
    			System.out.println("Assignment added.\n"); break;
    		}
    		case 2: 
    		{
    		System.out.println("Current Assignments:\n");
    		for ( Assignment temp : carlosAssignment)
    		System.out.println(temp); break;
    		}
    		case 3:
    		{
    			System.out.println("Are you finished with:");
    			for (int i = 0; i < carlosAssignment.size(); i++)
    			{
    				System.out.println(carlosAssignment.get(i) + "? (Y or N)");
    				response4 = sc.next();
    				if (response4.equals("Y"))
    					carlosAssignment.set(i, null);
    			}
    			break;
    		}
    		
    		case 4:
    		{
    			userEscape = true;
    		}
    		}
    		
    		
    		}
    		while(userEscape == false);
    		
    	}
    }
    Java Code:
    import java.util.*;
    
    public class Assignment {
    	private Date myDueDate;
    	private String myName;
    	
    	public Assignment (Date aDueDate, String aName)
    	{
    		this.myDueDate = aDueDate;
    		this.myName = aName;
    	}
    	
    	public String getName()
    	{
    		return myName;
    	}
    	
    	public Date getDueDate()
    	{
    		return myDueDate;
    	}
    	
    	public String toString()
    	{
    		return this.myDueDate + " - " + this.myName;
    	}
    }
    I have another problem. I'm trying to use the Date Class (and yes, Norm, I've browsed through the Date API) to assign the Date so I can use compareTo to compare the dates (and thus, I can sort ascending).

    To brief other users, here's what I'm trying to do:

    A program that can add assignments, remove assignments, show existing assignments, and sort assignments by date. I have no clue on how to sort assignments by date other than by somehow using the Java Date Class. I just have trouble on actually manipulating my program to include the date class.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default

    The Date class is a poor choice for create objects with dates because the Constructor you'd want to use is deprecated.
    Look at the Calendar and GregorianCalendar classes.
    What format are the dates you need to sort on?
    The Calendar class implements Comparable that could give you an idea on how to use or write a sort method.

  7. #7
    carlodelmundo is offline Member
    Join Date
    Aug 2008
    Posts
    31
    Rep Power
    0

    Default

    I was wondering why the Date Class wasn't working that well on Eclipse.

    The format of the dates are going to be just day and month.

    I'm experienced enough with the Comparable Interface to use it as a sort method.

    Another question Norm:

    Scratch my original Java program. How can I store a Date type and a String type into an ArrayList, of which can only have one data type? Like for example, I'd like to store the dates of the assignment, and the names of the assignment in an ArrayList. How would I construct the ArrayList? Do I have to provide a toString() method to compute the date and name of the assignment separately?

    Again, thank you for your immediate help.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default

    How can I store a Date type and a String type into an ArrayList,
    Create your own class to contain that data. Then you can put your class in the ArrayList

    Not sure what you mean about the toString() method and computing date and name.

  9. #9
    carlodelmundo is offline Member
    Join Date
    Aug 2008
    Posts
    31
    Rep Power
    0

    Default

    Yes. So what you're saying is similar to my assignments class with the instance fields Date and String as:

    Java Code:
    import java.util.*;
    
    public class Assignment {
    	private Date myDueDate;
    	private String myName;
    	
    	public Assignment (Date aDueDate, String aName)
    	{
    		this.myDueDate = aDueDate;
    		this.myName = aName;
    	}
    	
    	public String getName()
    	{
    		return myName;
    	}
    	
    	public Date getDueDate()
    	{
    		return myDueDate;
    	}
    	
    	public String toString()
    	{
    		return this.myDueDate + " - " + this.myName;
    	}
    }
    The only problem I get is when I call the ArrayList... actually by talking about it, I think I've solved my problem. Thanks haha

Similar Threads

  1. how to get input from User
    By Alvaro in forum New To Java
    Replies: 7
    Last Post: 01-15-2010, 11:02 PM
  2. who to take user input ,not by console...
    By Shyam Singh in forum New To Java
    Replies: 3
    Last Post: 06-13-2008, 10:09 PM
  3. Scanner input problem
    By slayer_azure in forum New To Java
    Replies: 3
    Last Post: 05-26-2008, 10:49 PM
  4. Prompting user input of a string.
    By apfroggy0408 in forum New To Java
    Replies: 3
    Last Post: 03-09-2008, 06:23 PM
  5. cant take input from user
    By new_1 in forum New To Java
    Replies: 6
    Last Post: 12-25-2007, 07:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •