+ 2

Java code doesnt work (Safety Deposit Boxes)

Can somebody help me? Problem: You are robbing a bank, but you’re not taking everything. You are looking for a specific item in the safety deposit boxes and you are going to drill into each one in order to find your item. Once you find your item you can make your escape, but how long will it take you to get to that item? Task Determine the amount of time it will take you to find the item you are looking for if it takes you 5 minutes to drill into each box. Input Format A string that represent the items in each box that will be drilled in order (items are separated by a comma), and secondly, a string of which item you are looking for. Output Format An integer of the amount of time it will take for you to find your item. My Solution: in the comments

30th Nov 2021, 2:53 PM
Áron SzƱcs
Áron SzƱcs - avatar
2 RĂ©ponses
+ 2
Use split[x].equals(want) Instead of == operator. == compare references for object while equals() method check for content.
30th Nov 2021, 3:28 PM
Jayakrishna 🇼🇳
+ 2
import java.util.*; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String in = sc.nextLine(); String want = sc.nextLine(); int t = 0; String[] split = in.split(","); for (int x = 0; x < split.length; x++){ if(split[x] == want){ t+=5;, break; } else{ t+=5; } } System.out.println(t); } }
30th Nov 2021, 2:53 PM
Áron SzƱcs
Áron SzƱcs - avatar