Can someone translate this code from java to swift or explain how it should look like in Swift (SHORT CODE) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone translate this code from java to swift or explain how it should look like in Swift (SHORT CODE)

import java.util.Scanner; public class RunLengthEncoding { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter input string: "); String s = input.nextLine(); for (int a = 0; a < s.length(); a++) { if ((s.charAt(a) < 'A' || s.charAt(a) > 'Z')) { System.out.print("Bad input"); System.exit(0); } } System.out.print("Enter flag character: "); char flag = input.nextLine().charAt(0); if (flag == '#' || flag == '

#x27; || flag == '*' || flag == '&') { int count = 0; for (int i = 1; i < s.length(); i++) { if(s.charAt(i)==s.charAt(i-1)); count++; if (count == 1) System.out.print(s.charAt(i)); if (count == 2) System.out.print(s.charAt(i) + s.charAt(i)); if (count == 3) System.out.print(s.charAt(i) + s.charAt(i) + s.charAt(i)); else System.out.print(flag + s.charAt(i) + (count + 1)); } } else System.out.print("Bad input"); } }

10th Apr 2021, 10:46 PM
Dzondzula
Dzondzula - avatar
4 Answers
+ 1
Yes, you can: 1) Complete the Swift course here in Sololearn. 2) Use Google/Bing to search to find answers to questions like 'if statement in Swift' I found it hard to find information on input is Swift so I will give you a head start: var message = readLine()! let sales = Int(readLine()!)!
14th Apr 2021, 6:39 AM
Paul K Sadler
Paul K Sadler - avatar
+ 1
The answer above me is correct however I can’t stress this enough: Force-unwrapping is Unsafe. Apple suggests you use more safe unwrapping techniques such as “if let” and “guard let” For example “guard let message = readLine() else { return }” Thanks.
12th Jun 2021, 1:28 PM
Pawel Zabinski
Pawel Zabinski - avatar
+ 1
I agree with Pawel Zabinski on this point. Here, on these small codes, in the Sololearn playground, is one thing. But real world robust error handling is the proper way to write production code.
12th Jun 2021, 1:32 PM
Paul K Sadler
Paul K Sadler - avatar
0
import Foundation print("Enter input string: ", terminator: "") if let s = readLine() { for a in s { if !("A"..."Z" ~= a) { print("Bad input") exit(0) } } print("Enter flag character: ", terminator: "") if let flag = readLine()?.first, ["#", "
quot;, "*", "&"].contains(flag) { var count = 0 var previousChar: Character? for i in s { if previousChar == nil { previousChar = i count += 1 } else if i == previousChar { count += 1 } else { if count == 1 { print(previousChar!, terminator: "") } else if count == 2 { print(String(repeating: previousChar!, count: 2), terminator: "") } else if count == 3 { print(String(repeating: previousChar!, count: 3), terminator: "") } else { print("\(flag)\(previousChar!)\(count)", terminator: "") } previousChar = i count = 1 } } if let previousChar = previousChar { if count == 1 { print(previousChar, terminator: "") } else if count == 2 { print(String(repeating: previousChar, count: 2), terminator: "") } else if count == 3 { print(String(repeating: previousChar, count: 3), terminator: "") } else { print("\(flag)\(previousChar)\(count)", terminator: "") } } } else { print("Bad input") } } The basic logic of the code remains the same, but there are some differences in syntax between Java and Swift. For example, in Swift, you need to use readLine() to read user input from the console, and you need to use optional to handle cases where there is no input. Additionally, you need to use exit(0) to exit the program in Swift. Finally, note that the loop for counting the c
22nd Apr 2023, 7:49 AM
Ali Akram Marufi
Ali Akram Marufi - avatar