Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Problems with input/output and getters/setters

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problems with input/output and getters/setters

    Hi Guys,

    I was just wondering if you could help me with any suggestions in implementing input from a user and outputting it to the console, I have getters and setters that the user needs to be able to use as a template for the type of data they need to enter, here's some of the code I have written up:

    This is some of the file that stores the variables,getters and setters..

    // Instance Variables
    	// ==================
    	private int upc;
    	private String title;
    	private int issueNumber;
    	private String publisher;
    	private String gradeCode;
    	private double currentValue;
    	public String determineCondition;
    	public double calcSellingPrice;
     
     
     
    	// Main Method
    	public static void main(String[] args) {
    		// Create instance of Student
    		ComicBook comicbook = new ComicBook (687788599, "Aquaman", 12, "Marvel", "MT", 20.00);
    		comicbook.setUpc(539684363);
    		}
     
     
    	// Create the Constructors
    	public ComicBook() {
    		setUpc(596060547);
    		setTitle("Wolverine");
    		setIssueNumber(20);
    		setPublisher("Marvel Comics");
    		setGradeCode("FR");
    		setCurrentValue(50.00);
    	}
    	public ComicBook(int upc, String title, int issueNumber, String publisher, String gradeCode, double currentValue){
    		setUpc(upc);
    		setTitle(title);
    		setIssueNumber(issueNumber);
    		setPublisher("Marvel Comics");
    		setGradeCode("FR");
    		setCurrentValue(50.00);
    	}
     
     
    	// Getters and Setters
    	// Upc getters and setters
    	public int getUpc() {
    		return upc;
    	}
    	public void setUpc(int upc) {
    		this.upc = upc;
    	}
    	// Title getters and setters
    	public String getTitle() {
    		return title;
    	}
    	public void setTitle(String title) {
    		this.title = title;
    	}

    This file needs to grab user input then display, the users need to be able to input into the getters and setters like they are creating a new item themself:

    public static void main(String[] args) {
    		Scanner scanner = new Scanner(System.in); //Initialize the scanner
    		BufferedReader keyboard = null;    // input stream
    		int numBooks = 0; //How many books are to be entered?
    		int whatNumBook = 0; //If this exceeds the value of numBooks, exit();
    		ComicBook c = new ComicBook(); //create new comic book
     
    		//Welcome message
    		System.out.println("Welcome to the Comic Book Manager!");
    		//Start running with request for no. of books
    		//Store value in numBooks for later use
    		System.out.print("How many books will you be entering today?  ");
            try {
            numBooks = scanner.nextInt();
            //Now wait for user to enter the number of books they are entering
            System.out.print("Please enter what number book you would like to start with?  ");
            whatNumBook = scanner.nextInt();
            //Once they are finished this will end the program
            if 	(whatNumBook > numBooks){
    		exit();
            }
     
            System.out.println("Upc: " + c.getUpc());
    		System.out.println("Title: " + c.getTitle());
    		System.out.println("Issue Number: " + c.getIssueNumber());
    		System.out.println("Publisher: " + c.getPublisher());
    		System.out.println("Grade Code: " + c.getGradeCode());
    		System.out.println("Current Value: " + c.getCurrentValue());
            }
            finally {
            	if (whatNumBook != 0) { 
                System.out.println("Closing Comic Book Manager");
                ComicBookManager.exit(); 
                }else { 
                System.out.println("Comic Book Manager not open");
                }

    Hope that makes sense, any help would be appreciated


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problems with input/output and getters/setters

    Do you have a specific question? The getting help link in my signature might help you lay out the problems you may be having to increase your chances of receiving help.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with input/output and getters/setters

    My main issue is I would like to know how users can input data through the console using the setters and getters that are stored in the first file?
    So for example, I need to display "enter the data for: upc, title, publisher" and so on. Once they have entered that data I will need to display what they have entered later on.

  4. #4
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default Re: Problems with input/output and getters/setters

    The best way to simplify your problem in this case would be creating two classes. One that will contain the main method and one that will contain all the members: - this will also contain the constructor. The problem you are asking if I understand it is how to fetch the information from the user then displaying them on the console: Well, after you have instantiated the object, just call the method that takes the parameter. That method should get the information and then you can use a output method to display it. You can combine the prompt method and the method you defined in the class definition. I hope this helps.

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with input/output and getters/setters

    Oh yes sorry I forgot to mention, the first piece of code does contain the main method, that's my first file, then the second piece of code i posted is the driver, my second file.

  6. #6
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Problems with input/output and getters/setters

    I wrote a class that simplifies user input of primitive types. You can get it here. The really nice thing about this class is you can specify exactly what the user has to enter. If they enter incorrect data it will inform them what they did wrong and ask them to try again and you can customize the prompt and error message all with a single line of code.

    Hope that helps.

  7. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with input/output and getters/setters

    Thanks for your help guys, I think I've fixed it..


    public static void main(String[] args) {
    		int numBooks = 0; //How many books are to be entered?
    		int whatNumBook = 0; //If this exceeds the value of numBooks, exit();
    		int num1 = 0;
    		Scanner scanner = new Scanner(System.in); //Initialize the scanner
    		ComicBook c = new ComicBook(); //create new comic book
     
     
    		//Welcome message
    		System.out.println("Welcome to the Comic Book Manager!");
    		//Start running with request for no. of books
    		//Store value in numBooks for later use
    		System.out.print("How many books will you be entering today?  ");
            try {
            numBooks = scanner.nextInt();
            //Now wait for user to enter the number of books they are entering
            System.out.print("Please enter what number book you would like to start with?  ");
            whatNumBook = scanner.nextInt();
            //Once they are finished this will end the program
            if 	(whatNumBook > numBooks){
    		exit();
            }
     
            System.out.println("Please begin entering the data");
            System.out.println("Upc: ");
            num1 = scanner.nextInt();
    		System.out.println("Title: "); 
    		scanner.next();
    		System.out.println("Issue Number: "); 
    		scanner.nextInt();
    		System.out.println("Publisher: ");
    		scanner.next();
    		System.out.println("Grade Code: ");
    		scanner.next();
    		System.out.println("Current Value: "); 
    		scanner.nextDouble();
            }
     
            finally {
            if (whatNumBook != numBooks) { 
            System.out.println("Closing Comic Book Manager");
            ComicBookManager.exit(); 
            }else { 
            System.out.println("Comic Book Manager not open");
            }
            } 
            }
    		private static void exit() {
    		System.out.println("Shutting down program");
    	}
    }

Similar Threads

  1. Input/Output help if possible
    By rossonomous in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 13th, 2011, 01:05 PM
  2. Input/Output Sequence
    By JoseGuad in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: August 30th, 2011, 06:50 AM
  3. Having problems with output to console....help!
    By toppcon in forum What's Wrong With My Code?
    Replies: 21
    Last Post: July 13th, 2011, 06:38 PM
  4. Input output file help
    By peteyfresh12 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 2nd, 2011, 07:44 AM
  5. Input/Output file help
    By Plural in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 25th, 2010, 08:34 PM