1

I am in trouble to solve problem in java. My input is : "Spring 2014" / "Summer 2012" / "Fall 2011" Output will be:

"141" for "Spring 2014" "122" for "Summer 2012" "113" for "Fall 2011"

That means last two digits of year and semester number will concatenated ( Spring =1 , Summer = 2 , Fall = 3 )

5
  • 1
    What part are you struggling with? Could you show us some code? Commented Mar 5, 2014 at 9:54
  • 4
    And what have you tried so far? And how doesn't it work? Commented Mar 5, 2014 at 9:54
  • your example doesn't fit to the definition: Fall 2011 should be "113" Commented Mar 5, 2014 at 9:56
  • @ashikha, yes ...sorry for mistake Commented Mar 5, 2014 at 9:58
  • "I am in trouble to solve problem in java." What problem you are getting ? Commented Mar 5, 2014 at 10:04

2 Answers 2

1

import java.util.Scanner;

public class Token {

static String[] r;
private static Scanner input;


public static void main(String[] args) {
    // TODO Auto-generated method stub
     String token;

     String sem1 = null,sem2 = null;
     input = new Scanner(System.in);
     System.out.println("Enter an Word");
     token = input.nextLine();
     r = token.split("\\ ");

     for(int i=0;i<r.length;i++){

         System.out.print("\nSplit value is"+r[i]);

         if(r[i].equals("Spring")){
             sem1="1";
         }
         else if(r[i].equals("Summer")){
             sem1="2";
         }else if(r[i].equals("Fall")){
             sem1="3";
         }else{
             sem2=r[i].substring(2,4);
         }

     }

     System.out.print("\nSemester no "+sem1);
     System.out.print("\nYear is :"+sem2);
     String semester_id=sem2+sem1;
     System.out.print("\nFinal Semester id is:"+semester_id);



}

}

Thanks all ... I have solved this :)

Output: Enter an Word Fall 2011

Split value isFall

Split value is2011

Semester no 3

Year is :11

Final Semester id is:113

Enter an Word

Spring 2014

Split value isSpring

Split value is2014

Semester no 1

Year is :14

Final Semester id is:141

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

Comments

0

Try this dont copy and paste,

        String input = "\"Spring 2014\" / \"Summer 2012\" / \"Fall 2011\"";
        String[] splittedValues = input.split("/");

        StringBuilder stringBuilder = new StringBuilder();

        for(String value : splittedValues)
        {
            String temp = value.replaceAll("[\"\"]", "").trim();
            temp = temp.substring(temp.length() - 2, temp.length());
            stringBuilder.append("\"");
            if(value.contains("Spring"))
            {
                stringBuilder.append(temp).append("1\"");
            }
            else if(value.contains("Summer"))
            {
                stringBuilder.append(temp).append("2\"");
            }
            else
            {
                stringBuilder.append(temp).append("3\"");
            }

            stringBuilder.append(" for ").append(value);


        }
        System.out.println("stringBuilder : "+stringBuilder.toString());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.