Sid Minas
Sid Minas

Reputation: 21

How to create a Java Program that accepts user inputs?

I'm solving multiple exercises online and I cant answer this one. The task is to create a program that accepts an input with format of TITLE-CHARACTER-YEAR and prints out the character's name and the manga's year category as indicated below

year less than 2000 and print "90s"
2000 less than or equal to year but less than 2006 and print "early 2000s"
2006 less than or equal to year and print "latest"

I tried coding it but I'm lacking of logical thinking on how can I run it right. Tried searching syntaxes but failed.

public class HelloWorld {

    public static void main(String[] args) {
        String title1 = "Yuyu Hakusho";
        String title2 = "Bleach-Ichigo";
        String title3 = "Bakuman";
        String name1 = "Eugene";
        String name2 = "Ichigo Kurosaki";
        String name3 = "Moritaka Mashiro";
        int year1 = 1994;
        int year2 = 2004;
        int year3 = 2008;


        if (year1 < 2000);
        System.out.println(name1 + " 90s");    
    }
}

Upvotes: 0

Views: 522

Answers (3)

practice2perfect
practice2perfect

Reputation: 507

Try this one.

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print(" please tell input with format of TITLE-CHARACTER-YEAR ");
    String input = scanner.nextLine(); //reads the input from console 
    String arr[] = new String[3]; // size ur wish
    arr=input.split("-"); //splits the input with the - delimiter into array of strings
    String name=arr[1]; //contains character
    int year=Integer.parseInt(arr[2]); //contains year
    if(year<2000)
        System.out.println( name + " 90's");
    else if(year>=2000 && year<2006)
        System.out.println(name + " early 2000's");
    else if(year>=2006)
        System.out.println(name + " latest");
}

There are also many other ways to do it, the simple and easy to understand is this

For codingbat.com check out this one.

public String methodName(String input){
     String arr[] = new String[3]; // size ur wish
    arr=input.split("-"); //splits the input with the - delimiter into array of strings
    String name=arr[1]; //contains character
    int year=Integer.parseInt(arr[2]); //contains year
    if(year<2000)
       return  name + " 90's";
    else if(year>=2000 && year<2006)
        return name + " early 2000's";
    else if(year>=2006)
       return name + " latest";
    else 
       return "wrong format";
 }

Upvotes: 0

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

To allow your application reading from user input you have several ways. The easiest would be using Scanner class. Here's an example:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Hello, please tell me your name: ");
    String name = Scanner.nextLine();
    System.out.println("Hello " + name);
}

There are several methods that will help you parsing the user input like nextInt and nextLine. For more info about them, check the proper Javadoc linked at the beginning of this post.


Apart of that, be careful when writing your block statements, like your if:

if (year1 < 2000);

Above means that there's nothing to do in case the int variable year1 is less than 2000.

Upvotes: 2

Maroun
Maroun

Reputation: 95948

if (year1 < 2000);
   System.out.println(name1 + " 90s");

Is equivalent to:

if (year1 < 2000) { }
System.out.println(name1 + " 90s"); //will be always executed

Remove the redundant ; after the if statement:

if (year1 < 2000); 
                 ↑

Now the other thing you need to have in your code is a Scanner. Go through the docs to understand how to use it.

Upvotes: 2

Related Questions