在Eclipse(Indigo)中进行编码时,我不小心碰到了一个键组合,当我做一些快捷方式时,这个键就出现了.这个问题可能之前已经得到了回答,但由于我不知道酒吧谷歌搜索的确切名称,这个问题毫无结果.我花了两个小时试图解决它.所以任何人都知道如何摆脱下图中的这个栏?
我对代码感到困惑
public class StringReplaceWithEmptyString
{
public static void main(String[] args)
{
String s1 = "asdfgh";
System.out.println(s1);
s1 = s1.replace("", "1");
System.out.println(s1);
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
asdfgh
1a1s1d1f1g1h1
Run Code Online (Sandbox Code Playgroud)
所以我的第一个意见是String中的每个字符都在""
两边都有一个空字符串.但是,如果是'a'
(在字符串中)之后就应该有两个'1'
输入(第一行为'a',第二行为's').
现在我检查了String是否在这些链接中表示为char [] 在Java中,是一个字符串数组?而在Java中字符串表示我得到的答案是YES.
于是,我就一个空字符分配''
给一个char
变量,但它给我一个编译器错误,
字符常量无效
我尝试时,同样的过程给出了编译器错误 char[]
char[] c = {'','a','','s'}; // CTE
Run Code Online (Sandbox Code Playgroud)
所以我对三件事感到困惑.
对不起,如果我在问题的任何部分出错了.
这可能看起来像一个重复的问题,但我尝试了以下所有链接,无法得到正确的答案.
但我没有弄错.这是我的代码
DecimalFormat twoDForm = new DecimalFormat("#.##");
double externalmark = 1.86;
double internalmark = 4.0;
System.out.println(String.valueOf((externalmark*3+internalmark*1)/4));
String val = String.valueOf((externalmark*3+internalmark*1)/4);
String wgpa1=twoDForm.format(val); // gives exception
String wgpa2=twoDForm.format((externalmark*3+internalmark*1)/4)); // works fine
System.out.println(wgpa1);
Run Code Online (Sandbox Code Playgroud)
该format
方法采用Object类型参数,这就是我传递给出异常的String对象的原因
线程"main"中的异常java.lang.IllegalArgumentException:无法 将给定的Object格式化为数字.
但是当我将double值作为参数时,程序运行正常.但是如果方法是用Object
类型参数定义的,为什么我在传递时传递一个异常并且在传递时String
没有获得异常double
?
java overloading exception illegalargumentexception decimalformat
这是我的代码
import java.util.Date;
import java.text.DateFormat;
class DateTime {
public static void main(String[] args) {
String dt = DateFormat.getDateTimeInstance().format(new Date());
System.out.println(dt);
}
}
Run Code Online (Sandbox Code Playgroud)
当使用 Java 21 编译和执行时,对“format()”的调用返回一个UTF-16
包含无效字节的字符串,用问号表示:
2023 年 10 月 3 日 7:01:17?PM
还有其他人看到这个问题吗?谢谢。
我在分号后添加了一个额外的分号System.out.println
:
System.out.println();;
Run Code Online (Sandbox Code Playgroud)
这对Java编译器来说是合法的,所以我检查了其他语句,它们都是合法的.所以当我搜索并找到这些链接时:
我开始明白额外的分号意味着额外的空话.
但是当我在return
声明后添加一个额外的分号时,我得到了编译时错误.我得出的结论是,该return
声明被认为是执行流程中的最后一个声明,因此在声明之后添加一个额外的声明return
是非法的.
这个代码也发生了同样的事情:
if(a == b)
System.out.println();;
else
System.out.println();
Run Code Online (Sandbox Code Playgroud)
在if
语句内部System.out.println();;
给出了编译时错误,因为编译器期望elseif
或else
.我是对的还是还有其他原因吗?
我在调用非静态方法时感到困惑
class A {
void doThis() {}
public static void main(String... arg) {
A a1 = new A();
a1.doThis(); // method - 1
new A().doThis(); // method - 2
}
}
Run Code Online (Sandbox Code Playgroud)
我知道方法1和方法2都会调用doThis(),但有没有任何功能差异?
这是我们的代码
private IdentificationMaster validateIdentificationType(String idType) {
if(!StringUtils.isNotBlank(idType))
throw new IllegalArgumentException("Invalid idType");
Optional<IdentificationMaster> op1 = specRepo.findById(idType); //testing purpose
Optional<IdentificationMaster> op2 = specRepo.findByIdentificationType(idType); //testing purpose
return specRepo.findById(idType)
.orElse(specRepo.findByIdentificationType(idType)
.orElseThrow(() -> new ResourceNotFoundException("Id Type Not Found " + idType)));
}
Run Code Online (Sandbox Code Playgroud)
因为idType
我们期待两个值,它可以是主键 id或其对应的identificationType
. 表只有两列id
和identificationType
。问题是ResourceNotFoundException
即使op1
或op2
不为空它也会抛出。现在如果我像这样改变我的回报
return specRepo.findByIdentificationType(idType)
.orElse(specRepo.findById(idType)
.orElseThrow(() -> new ResourceNotFoundException("Id Type Not Found " + idType)));
Run Code Online (Sandbox Code Playgroud)
它再次抛出相同的异常!
存储库
@Repository
public interface IdentificationSpecRepository extends CrudRepository<IdentificationMaster, String>{
Optional<IdentificationMaster> findByIdentificationType(String …
Run Code Online (Sandbox Code Playgroud) toNanos
这是类中的方法Duration
public long toNanos() {
long tempSeconds = seconds;
long tempNanos = nanos;
// TODO: Here makes me confused
if (tempSeconds < 0) {
// change the seconds and nano value to
// handle Long.MIN_VALUE case
tempSeconds = tempSeconds + 1;
tempNanos = tempNanos - NANOS_PER_SECOND;
}
long totalNanos = Math.multiplyExact(tempSeconds, NANOS_PER_SECOND);
totalNanos = Math.addExact(totalNanos, tempNanos);
return totalNanos;
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么当秒为负数时需要做额外的工作。
正数的 max 为2^63-1
,负数的 min 为2^63
,看起来是转移-2^63s,-1ns
到-2^63+1s,1000_000_000-1ns
,但最终还是要参与计算。在我看来这是没有意义的,因为当数字溢出时Math.multiplyExact
会Math.addExact
抛出异常,无论判断是否存在,它都不会改变
我继承了一些代码,原来的开发人员已经没有人留下了。该代码大量使用了CompletableFuture
,这是我第一次使用它,所以我仍在尝试理解它。据我了解, a(Completable)Future
通常与某种多线程机制一起使用,该机制允许我们在执行耗时的任务时执行其他操作,然后只需通过 Future 获取其结果。正如javadoc中所示:
interface ArchiveSearcher { String search(String target); }
class App {
ExecutorService executor = ...
ArchiveSearcher searcher = ...
void showSearch(final String target) throws InterruptedException {
Future<String> future = executor.submit(new Callable<String>() {
public String call() {
return searcher.search(target);
}});
displayOtherThings(); // do other things while searching
try {
displayText(future.get()); // use future
} catch (ExecutionException ex) { cleanup(); return; }
}
}
Run Code Online (Sandbox Code Playgroud)
然而,在我继承的这个应用程序中,以下不使用任何多线程的模式多次出现:
public Object serve(Object input) throws ExecutionException, InterruptedException { …
Run Code Online (Sandbox Code Playgroud) 问题是检查随机数n
可以是2个随机素数的总和.例如,
如果n = 34,则可能是(3 + 31),(5 + 29),(17 + 17)......
到目前为止,我已设法将素数保存到数组中,但我不知道如何检查,如果n
是2个素数的总和.
这是我的代码的一部分:
public static void primeNumbers(int n) {
int i = 0, candidate = 2, countArray = 0, countPrime = 0;
boolean flag = true;
while (candidate <= n) {
flag = true;
for (i = 2; i < candidate; i++) {
if ((candidate % i) == 0) {
flag = false;
break;
}
}
if (flag) {
countPrime++;
}
candidate++;
}
int[] primeNumbers = new …
Run Code Online (Sandbox Code Playgroud) java ×9
arrays ×2
asynchronous ×1
breadcrumbs ×1
char ×1
datetime ×1
duration ×1
eclipse ×1
exception ×1
findby ×1
formatting ×1
future ×1
instance ×1
java-21 ×1
optional ×1
overloading ×1
primes ×1
return ×1
spring-boot ×1
string ×1