如何在JAVA中获得所需的输出

huangapple go评论79阅读模式
英文:

How to get the desired output in JAVA

问题

// Given Inputs
String input = "abcdaa/efgh/hidjk/lmno/pqrs/tuvw";
int slashCounter = 3;

// Split the input string by slashes
String[] parts = input.split("/");

// Initialize StringBuilder to build the desired output
StringBuilder outputBuilder = new StringBuilder();

// Loop through the parts and append to the outputBuilder based on slashCounter
for (int i = 0; i < Math.min(slashCounter, parts.length); i++) {
    outputBuilder.append(parts[i]);
    if (i < slashCounter - 1) {
        outputBuilder.append("/");
    }
}

// Final desired output
String desiredOutput = outputBuilder.toString();

This Java code takes a given input string and an integer slashCounter. It splits the input string into parts using the slash delimiter and then constructs the desired output by appending the desired number of parts together along with the slashes. The number of parts included is determined by the slashCounter value.

英文:

If there is a given
Inputs
String "abcdaa/efgh/hidjk/lmno/pqrs/tuvw" and if
int slashCounter=3,
The desired Output should be -
Output: abcdaa/efgh/hidjk/lmno

(Basically if slashCounter=3 only alplabets upto 4th '/' is allowed. From fourth '/'everything is ignored. Below is the few Input and Output. (There may be any number of alphabets between '/' to '/'). Below is few more inputs

Input:
String aaabcd/efgh/hidjk/lmno/pqrs/tuvw
if int slashCounter=2
Output: aaabcd/efgh/hidjk

Input:
String aaabcd/efgh/hidjk/lmno/pqrs/tuvw
if int slashCounter=4
Output: aaabcd/efgh/hidjk/lmno/pqrs

Could someone help me with the logic of this in JAVA. Thanks in advance.

答案1

得分: -1

这就是操作方法。

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        // 我们想要的字符串长度
        int display = sc.nextInt();
        // 遇到"/"时拆分字符串,然后将其存储在索引0中,然后增加索引
        String aplhabets[] = input.split("/");
        // 要添加的字符串
        StringBuilder sb = new StringBuilder();
        for(int i = 0 ; i <= display; i++) {
        	sb.append(aplhabets[i]+"/");
        }
        // 因为我们不想要字符串的最后一个"/",所以我们只打印从0到字符串长度-1的部分,这将删除最后一个"/"
        System.out.print(sb.substring(0, sb.length()-1));
    }

输出:

aaabcd/efgh/hidjk

String aplhabets[] = input.split("/") 这将在遇到/时拆分字符串并将其放入数组中。

sb.substring(0, sb.length()-1) 这是因为当我们在循环中追加字符串生成字符串构建器时,它会在末尾添加/
为了删除最后一个/,我们使用 sb.substring(0, sb.length()-1),其中第一个参数是起始索引,第二个参数是结束索引
substring(int start, int end)中,起始索引是包含的,而结束索引是排除的。

这就是如何解决这个问题。建议您学习如何使用数组和字符串操作,这肯定会对您有所帮助。

英文:

This is how you do.

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        // till how much we want the string
        int display = sc.nextInt();
        // splits the String when &quot;/&quot; is encounter and then stores it in 0 index and then increases the index
        String aplhabets[] = input.split(&quot;/&quot;);
        // to add the String we want 
        StringBuilder sb = new StringBuilder();
        for(int i = 0 ; i &lt;= display; i++) {
        	sb.append(aplhabets[i]+&quot;/&quot;);
        }
        // as we dont want the last &quot;/&quot; of the String we just print from 0 to string length - 1, this will remove teh last &quot;/&quot;
        System.out.print(sb.substring(0, sb.length()-1));
    }

Output:

aaabcd/efgh/hidjk/lmno/pqrs/tuvw
2
aaabcd/efgh/hidjk

String aplhabets[] = input.split(&quot;/&quot;) this splits the String and puts it in the array whenever / is encounter.<br>

sb.substring(0, sb.length()-1) this cause when we were appending String builder from he loop it's adding / in the end.<br>
So in order to remove last / we do sb.substring(0,sb.length-1) where first parameter is start index and second index is end index<br>
in substring(int start, int end) start in inclusive and end is exclusive.

this is how you do the problem. Suggest you learn how to use arrays and String manipulation. this will surely benefit you.

huangapple
  • 本文由 发表于 2020年9月28日 14:23:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64096934.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定