简体   繁体   English

给定字符串长度,如何将输入作为字符串

[英]How to take input as String given the String Length

How to take input from User as String given the String Length 给定字符串长度,如何从用户输入作为字符串

The problem link: 问题链接:

http://www.practice.geeksforgeeks.org/problem-page.php?pid=295 http://www.practice.geeksforgeeks.org/problem-page.php?pid=295

MyApproach: 我的方法:

I used Scanner for the task to take the number of testcases and then took the String length as input: 我使用Scanner完成任务以获取测试用例的数量,然后将String长度作为输入:

But I am confused how to take String of specified length.My search found that there is no method or constructor which can take string of specified length. 但是我很困惑如何采用指定长度的字符串。我的搜索发现没有方法或构造函数可以采用指定长度的字符串。

Can Anyone guide? 有人可以指导吗?

Below is my code: 下面是我的代码:

 Scanner sc=new Scanner(System.in);
 int T=sc.nextInt();
 for(int i=1;i<=T;i++)
  {
    int Strlength=sc.nextInt();
    String strnew=sc.next(); //How to take Stringofspecified character
    ..........
     .........
   }

You can't do that, you can simply force the user to reinsert the String if the length of the String does not match the given input. 您无法做到这一点,如果String的长度与给定的输入不匹配,您可以简单地迫使用户重新插入String。

 Scanner sc=new Scanner(System.in);
 int T=sc.nextInt();
 for(int i=1;i<=T;i++)
  {
    int Strlength=sc.nextInt();
    String strnew = null;
    while (strnew == null || strnew.size() != Strlength) {
      strnew = sc.next(); 
    }
    ..........
     .........
   }

A way to do this is inserting some kind of test, eg: 一种方法是插入某种测试,例如:

String strnew;
do{
   strnew=sc.next(); //How to take Stringofspecified character
}
while(strnew.length() != Strlength);

I figured out a way to do this. 我想出了一种方法。

Scanner sc = new Scanner(System.in);
int strLength = sc.nextInt();
sc.nextLine(); 
String strInput = sc.nextLine();
if(strInput.length()>strLength)
{
    str = str.substring(0, strlength);
}
// Now str length is equal to the desired string length
    String S = "";
    Scanner sc = new Scanner(System.in);
    int D = sc.nextInt();
    for(int i=0;i<D;i++) {
         S = S + sc.next();
    }
    System.out.println(S);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改输入以便输入可以采用任何长度的字符串,输出问题 - Changing input so in can take any length of string, problems with output 如何确保加密长度与给定的字符串相同 - How to ensure the encrypted length are same as the given string 获取任何给定输入字符串中带有不同字符的最长子字符串的长度 - Get the length of longest substring with distinct characters in any given input string 如何从Java中的控制台获取String输入? - How to take String input from console in Java? 如何在Java中输入编辑过的字符串? - How to take edited string input in Java? 将字符串字符分组为给定长度 - Group string characters into a given length 如何递归获取输入字符串并返回每个字符重复的字符串? - How to recursively take an input string and return a string with each character repeated? 如何使用 java 在给定字符串中查找最后一个字的长度? - how to find Length of Last Word in the given string using java? Java服务器-如何从InputStreamReader获取输入并将其转换为String? - Java Server - How to take input from InputStreamReader and convert to a String? 给定数字n作为输入,返回长度为n的新字符串数组,其中包含字符串“ 0”,“ 1”,“ 2”,依此类推,直到n-1 - Given a number n as input, return a new string array of length n, containing the strings “0”, “1”, “2” so on till n-1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM