Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Thor is a senior developer of Microsoft. In curiosity, he wants to know the In-Out cycle of
other employees.
An employee comes into the office and after that, if he goes out, he completed an
InOut Cycle. Every Microsoft employee needs to be Punched In and Punched Out for
coming in and going out of the office. Thor can take the info of an employee’s entry
and exit history from the Punching Database. But he faces some problems. Some
employees punched in/out multiple times for a single entry/exit.
Thor gives you the entry and exit history of an employee and he wants you to calculate
the InOut Cycle for him.
Input
The first line of the input contains a single number "N"(1≤N≤100) donates the number
of Punch In/Out pressed by an employee. Second-line contains the entry and exit
history of that employee.
Output
The output will be the number of InOut Cycles for the given employee.
Samples
Input Output
6
In Out In Out In Out
3
Input Output
7
In In In Out Out Out Out
1
Input Output
5
In Out In Out Out
2


What I have tried:

package hello;
import java.util.Scanner;
public class Hello
{
	
	public static void main(String args[])
	{
		
		Scanner sc=new Scanner (System.in);
		//int n=sc.nextInt();
		//sc.close();
		String[] string = new String [sc.nextInt()];      
		sc.nextLine();   
		for (int i = 0; i < string.length; i++)   
		{  
		string[i] = sc.nextLine();  
		}  
		
		
				
		
		
	}
	



}
Posted
Updated 29-Jun-22 23:06pm
v2

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

Just posting your homework question with no effort to solve it isn't going to get you anywhere.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
In order to correctly gather the input strings, replace
Quote:
string[i] = sc.nextLine();
with
Java
string[i] = sc.next();

Afterwards, it is just a matter of counting the
In->Out
transitions. This could be easily implemented setting a variable, say cur equal to string[0], then iterating i form 1 to string.length-1. At each iteration, check if cur equals "In" and string[i] equals "Out": if this condition holds true then increment the count of found cycles. At the end of the iteration update cur (cur = string[i];).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900