在Java 8流API中,调用chars()任何String对象都会返回IntStream包含所有字符的对象.
将返回的IntStream对象转换回String?的正确方法是什么?调用toArray()会给我一个int[],任何String构造函数都不接受.
我试图在Java 8中使用新的Stream API生成随机的整数数组.但我还没有清楚地理解这个API.所以我需要帮助.这是我的代码.
Random random = new Random();
IntStream intStream = random.ints(low, high);
int[] array = intStream.limit(limit) // Limit amount of elements
.boxed() // cast to Integer
.toArray();
Run Code Online (Sandbox Code Playgroud)
但是这段代码返回了一些对象.这有什么问题?
所以我试图找到用户输入的字符串中的所有大写字母但我不断收到此运行时错误:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: 4
at java.lang.String.charAt(String.java:686)
at P43.main(P43.java:13)
Run Code Online (Sandbox Code Playgroud)
我觉得很愚蠢,但我无法弄清楚这一点,oracle甚至在页面上谈到关于java.lang.StringIndexOutOfBoundsException的 charAt
这是我的代码,用于查找大写字母并打印它们:
import java.io.*;
import java.util.*;
public class P43{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
//Uppercase
String isUp = "";
System.out.print("Please give a string: ");
String x = in.next();
int z = x.length();
for(int y = 0; y <= z; y++){
if(Character.isUpperCase(x.charAt(y))){
char w = x.charAt(y);
isUp = isUp + w + " ";
}
}
System.out.println("The uppercase …Run Code Online (Sandbox Code Playgroud) 我有一个任意字符串"hello my name is timothy"和另一个任意'关键字'字符串"ham".我想创建一个方法,通过反复重复其字符,同时保留空格,使第二个字符串与第一个字符串的长度相同,结构相同.结果将是:"hamha mh amha mh amhamha.到目前为止,这是我的代码:
public String makeStringsEqual(String str, String keyword)
{
if (str.length() > keyword.length())
{
for(int i = 0; i < str.length(); i++)
{
if (str.charAt(i) != ' ')
{
keyword += keyword.charAt(i);
}
else
keyword += " ";
}
}
return keyword;
}
Run Code Online (Sandbox Code Playgroud)
返回前一个示例的代码hamhamha ha ha.我怎样才能解决这个问题?
如何在Java 8中转换Stream<Character>成A String?Collectors.joining()期望CharSequence因此它给编译错误。