0

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");    
    }
}
0

3 Answers 3

2
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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

Thanks guys! Forgot to tell you that the website has a built-in box where the user can insert the inputs.
0

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";
 }

14 Comments

Thanks guys! Forgot to tell you that the website has a built-in box where the user can insert the inputs.
@SidMinas then just pass the value obtained from the html to input variable in java through servlet by posting it from html
sir, I'm assuming that you're thinking that I'm using cmd, eclipse or netbeans. But the website has it's own terminal,a box for user's input, and a box where you can see the output.
can you tell which website you are mentioning.?
@SidMinas I am not a user of that website. but with what i observed from it, i am updating the answer. check if that works and accept the answer if it does so
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.