Question

My teacher gave me this question:

Write a program that does the following:

  • Enter your first name: Peter
  • Enter your last name: Opunga
  • Enter your birth year: 1992
  • Hi Peter Opunga, you will be 20 by the end of this year.
  • Bonus 1: Proper commenting. (10%)
  • Bonus 2: Create only 2 Strings in the entire program. (10%)
  • Bonus 3: Use exactly 4 System.out.print. (10%)

Now I am completely new to Java. I have just been introduced to it a little over a week ago and this is what I came up with:

import java.io.*;

public class A1Chuah {

    public static void main(String[] args) throws IOException {

        BufferedReader in = new BufferedReader (new InputStreamReader (System.in));

        //Prints instructions for user to enter first name.
        System.out.println("Enter first name: ");
        //Obtains user input for first name.
        String first = in.readLine ();
        //Prints instructions for user to enter last name.
        System.out.print ("Enter last name: ");
        //Obtains user input for last name.
        String last = in.readLine();
        //Prints instructions for user to enter birth year.
        System.out.print ("Enter your birth year: ");
        //Obtains user input for birth year.
        String year = in.readLine ();
        //Converts String year to int useryr
        int useryr = Integer.parseInt(year);
        //Sets int oriyr to value of 2012
        int oriyr = 2012;
        //Sets int outyr as oriyr - useryr
        int outyr = oriyr - useryr;
        //Prints out information.
        System.out.println("Hi" + " " + " " + last + "," + " " + "you will be" +  " " + outyr + " " + "by the end of this year");

I've managed to complete Bonus 1 and 3 but can't seem to figure out Bonus 2. Please help! P.S. He said that I can get help as long as I don't try to pass it off as my own idea.

Was it helpful?

Solution

You can use one string to get the full name and one for the year.. you can do it this way

    System.out.println("Enter first name: ");

    String name = in.readLine();

    System.out.print ("Enter last name: ");

    name+= " " + in.readLine();

    System.out.println(name); // should print first and last

OTHER TIPS

You can reuse strings, so String last = in.readLine(); can be replace with first = in.readLine ();

Now, if you only need one string to read in from, and only need one string for the answer... you should be able to figure out from there how to only use two strings

(Hint http://docs.oracle.com/javase/tutorial/java/data/strings.html)

EDIT: Sorry I thought you were using the java.util.Scanner class so the following will not be of help unless you feel like switching to that instead.

The following was written under the assumption you were using:

java.util.Scanner in = new java.util.Scanner(System.in);
in.nextLine();

Previous post:

I think you are looking for the in.nextInt(); method instead of parsing a returned string for year of birth.

Also be aware Scanner has way more capability such as scanning for patterns (RegEx) and tokenizing string input.

In the future you may want to use an IDE like Netbeans which allows you to quickly browse through possible methods of a class with the auto-complete. If you are allowed to do so that is.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top