我有一个extension
方法,在Kotlin中将字符串转换为Date.
fun String.convertToDate() : Date {
var pattern: String = "dd-mm-yyyy"
val dateFormatter = SimpleDateFormat(pattern)
return dateFormatter.parse(this) // parse method throw ParseException
}
Run Code Online (Sandbox Code Playgroud)
这是我试图捕获可能的异常的代码.
try {
"22---2017".convertToDate()
} catch (ex: ParseException) {
// ParseException supposed to be caught in this block
logger.error("Parse exception occur")
} catch (ex: Exception) {
// ParseException caught in this block
logger.error("Exception occur")
}
Run Code Online (Sandbox Code Playgroud)
在ParseException
过去块就是抓住了Exception
被捕获.但它应该在ParseException块中捕获吗?我在这里错过了什么?
=== 更新 ===
我正在开发一个Spring MVC项目.我已经在简单的独立kotlin程序中运行代码,其中它的行为相应.但在我的春季项目中,它表现不同.我给出了完整的代码Controller
和Service
图层.
调节器
@PostMapping
@PreAuthorize("hasAnyRole('USER','ROLE_USER','ROLE_ADMIN','ADMIN')")
fun postAttendance(@RequestBody attendanceJson: …
Run Code Online (Sandbox Code Playgroud) 在C++中,如果我声明像std :: map m这样的地图
然后我可以用这种方式增加地图中特定键的值
m[key]++
Run Code Online (Sandbox Code Playgroud)
在Java中我声明了一个地图
Map<Integer, Integer> m = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)
并以这种方式增加特定键的值:
m.put(key, m.get(key).intValue() + 1)
Run Code Online (Sandbox Code Playgroud)
我的问题:有没有捷径或更好的方法来做到这一点?
我有三个实体.那些是:
@Entity
public class Organization {
@Id
private long id;
@Column
private String name;
}
Run Code Online (Sandbox Code Playgroud)
@Entity
public class Book {
@Id
private Long id;
@Column
private String name;
@ManyToOne
private Organization organization;
}
Run Code Online (Sandbox Code Playgroud)
@Entity
public class Account {
@Id
private Long id;
@Column
private String name;
@ManyToOne
private Book book;
}
Run Code Online (Sandbox Code Playgroud)
在这三个实体中,我想执行以下sql:
SELECT acc.name, acc.id
FROM account acc
JOIN book b on acc.book_id = b.id
JOIN organization org on b.organization_id = org.id
WHERE org.name = 'XYZ'
Run Code Online (Sandbox Code Playgroud)
在这种情况下,Account
实体与Organization …
我正在编写基本GUI的代码.我需要一个文本区域.但我无法使文本区域达到我想要的大小.我使用setPreferredSize
方法来设置文本区域的维度.但它没有用.我也试过setSize
方法但也没用.这是我的书面代码.
private void textArea() {
setTitle("TextArea");
setSize(700, 500);
setLayout(new BorderLayout());
JTextArea textArea = new JTextArea();
textArea.setPreferredSize(new Dimension(100,100));
System.out.println(textArea.getSize());
textArea.setBackground(Color.GREEN);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(false);
add(textArea,BorderLayout.CENTER);
}
Run Code Online (Sandbox Code Playgroud)
对不起,我的英语不好.提前致谢.
我遇到异常 -
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.muztaba.service.VerdictServiceImpl] is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:372)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:332)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1066)
at com.muztaba.service.App.task(App.java:35)
at com.muztaba.service.App.main(App.java:28)
Run Code Online (Sandbox Code Playgroud)
这是我得到异常的班级。
@Component
public class App {
QueueService<Submission> queue;
Compiler compiler;
VerdictService verdictService;
public static void main( String[] args ) {
new App().task();
}
private void task() {
AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
queue = context.getBean(QueueImpl.class);
compiler = context.getBean(CompilerImpl.class);
verdictService = context.getBean(VerdictServiceImpl.class); //here the exception thrown.
while (true) {
if (!queue.isEmpty()) {
Submission submission = queue.get();
compiler.submit(submission);
}
}
} …
Run Code Online (Sandbox Code Playgroud) 我写了一个实现类似接口的类.我使用@NotNull
注释来抑制方法参数中的waring.但它仍然显示了warning.The IDE自动导入这个包com.sun.istack.internal.NotNull
的@NotNull
.为什么这是在讨价还价?不使用此注释如何删除此警告?
我正在使用带有java 8 SE的Inteij Ultimate.这是我的代码片段.
谢谢.
在传统方式中,我已经编写了这个简单的java for循环
public String setString(String string) {
StringBuilder stringBuilder = new StringBuilder();
// This for loop I want to convert into stream.
for (char c : string.toCharArray()) {
if (Character.isSpaceChar(c))
stringBuilder.append(c);
else if (isBangla(c))
stringBuilder.append(get(c));
}
return stringBuilder.toString();
}
String get(int c) {
return keyMap.getMap((char) c); // Map<Character, String> keyMap
}
boolean isBangla(int codePoint) {
return ((codePoint >= BANGLA_CHAR_START && codePoint <= BANGLA_CHAR_END) &&
(codePoint <= BANGLA_NUMBER_START || codePoint >= BANGLA_NUMBER_END)));
}
Run Code Online (Sandbox Code Playgroud)
我试过这样的事情.
String str = string.chars()
.filter(i -> Character.isSpaceChar(i)) …
Run Code Online (Sandbox Code Playgroud)