鉴于以下输入:
4534534534564657652349234230947234723947234234823048230957349573209483057
12324000123123
Run Code Online (Sandbox Code Playgroud)
我试图以BigInteger下列方式分配这些值.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
BigInteger num1 = BigInteger.valueOf(sc.nextLong());
sc.nextLine();
BigInteger num2 = BigInteger.valueOf(sc.nextLong());
BigInteger additionTotal = num1.add(num2);
BigInteger multiplyTotal = num1.multiply(num2);
System.out.println(additionTotal);
System.out.println(multiplyTotal);
}
Run Code Online (Sandbox Code Playgroud)
第一个值超出了Long数的边界,因此我得到以下异常:
线程"main"中的异常java.util.InputMismatchException:对于输入字符串:"4534534534564657652349234230947234723947234234823048230957349573209483057"
我假设的BigInteger需要一个长型与使用valueOf()方法(如说在这里).如何将超大数字传递给BigInteger?
尝试将数据库上的值与表单中传递的值匹配以检查用户是否存在时,我收到以下错误.
可捕获的致命错误:类PDOStatement的对象无法转换为字符串
这是我正在使用的代码:
//Check users login details
function match_login($username, $password){
//If the button has been clicked get the variables
try{
$dbh = new PDO("mysql:host=localhost;dbname=mjbox","root", "usbw");
} catch( PDOException $e ) {
echo $e->getMessage();
}
$stmt = $dbh->prepare("SELECT * FROM mjbox WHERE username=? AND password=?");
$stmt->bindParam(1, $username);
$stmt->bindParam(2, $password);
$stmt->execute();
$result = mysql_query($stmt);
if( mysql_num_rows($result) > 0 ){
echo 'There is a match!';
}else{
echo 'nooooo';
}
}
Run Code Online (Sandbox Code Playgroud) 给定以下 Java 8 流:
scheduleService.list().stream()
.filter(Schedule::getEnabled)
.filter(this::runnable)
.flatMap(s -> s.getJobs().stream())
// .doSomethingArbitrary(System.out.println("A single message. The total number of
// elements in the stream after filtering is " + this::count))
.forEach(this::invoke);
Run Code Online (Sandbox Code Playgroud)
将过滤应用于流并应用第一个终端操作后,如果流为空,我想记录一条调试消息,或者如果不是,则对流invoke中的每个元素调用该方法。这可能吗?
鉴于以下输入:
-100
50
0
56.6
90
Run Code Online (Sandbox Code Playgroud)
我已将每个值添加BigDecimal到列表中.
我希望能够从最高到最低值对列表进行排序.
我试图通过以下方式执行此操作:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<BigDecimal> list = new ArrayList<BigDecimal>();
while(sc.hasNext()){
list.add(new BigDecimal(sc.next()));
}
Collections.reverse(list);
for(BigDecimal d : list){
System.out.println(d);
}
}
Run Code Online (Sandbox Code Playgroud)
哪个输出:
90
56.6
0
50
-100
Run Code Online (Sandbox Code Playgroud)
在这种情况下,50应该是高于0的值.
考虑到十进制和非十进制值,如何正确地将BigDecimal列表从最高到最低排序?
我第一次尝试使用 Spring Boot 显示 Freemarker 视图,目前在浏览器中出现以下错误:
白标错误页面
此应用程序没有明确的 /error 映射,因此您将其视为后备。
Sun Feb 19 17:59:07 GMT 2017 出现意外错误(类型=未找到,状态=404)。没有可用的消息
我正在使用 Spring Boot 1.5。
我的文件结构:
登录控制器
package com.crm.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.crm.service.LoginService;
@Controller
public class LoginController {
@Autowired
private LoginService loginService;
@RequestMapping("/login")
public String authenticate() {
return "loginPage";
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序属性
spring.freemarker.template-loader-path: /
spring.freemarker.suffix: .ftl
Run Code Online (Sandbox Code Playgroud)
服务器
package com.crm;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Server{
public static void main(String[] args) {
SpringApplication.run(Server.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么 Spring 无法解析 …
我目前正在使用以下组合来记录消息:
private final Logger log = Logger.getLogger(this.getClass().getSimpleName());
[...]
if (log.isLoggable(Level.INFO)) {
log.log(Level.INFO, message);
}
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以在所有日志消息到达控制台之前拦截它们,以便我可以将它们保存在数据库中?
类似如何HandlerInterceptor拦截web请求?
我有一个Parent对象集合,每个对象都有一个Child元素集合,例如:
public class Parent {
private Collection<Child> children;
}
public class Child {
private String type;
}
Run Code Online (Sandbox Code Playgroud)
我如何使用Java 8函数式编程来过滤和收集Child类型等于'A' 的集合?
我尝试过以下方法:
Collection<Child> filteredChildren = parents.stream()
.forEach(p ->
filteredChildren.addAll(p.getChildren().stream()
.filter(c -> c.getType().equals("A"))
.collect(Collectors.toList()))
);
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
变量'filteredChildren'初始值设定项'parents.stream().forEach(p - > ...'冗余少......(⌘F1)此检查指出变量值在赋值后从未使用过的情况,即: - 赋值后永远不会读取变量OR - 在下一个变量读取OR之前,该值总是被另一个赋值覆盖 - 变量初始值设定项是冗余的(出于上述两个原因之一)
如何按类型过滤嵌套集合并收集它们?
我刚刚开始学习面向对象编程的概念,并将一个用于连接数据库,选择数据库和关闭数据库连接的类组合在一起.到目前为止,除了关闭与数据库的连接外,一切似乎都没问题.
class Database {
private $host, $username, $password;
public function __construct($ihost, $iusername, $ipassword){
$this->host = $ihost;
$this->username = $iusername;
$this->password = $ipassword;
}
public function connectdb(){
mysql_connect($this->host, $this->username, $this->password)
OR die("There was a problem connecting to the database.");
echo 'successfully connected to database<br />';
}
public function select($database){
mysql_select_db($database)
OR die("There was a problem selecting the database.");
echo 'successfully selected database<br />';
}
public function disconnectdb(){
mysql_close($this->connectdb())
OR die("There was a problem disconnecting from the database.");
}
}
$database = …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
int total = 6;
int perPage = 5;
double pages = total/perPage;
double ceilPages = Math.ceil(pages);
out.println(ceilPages);
Run Code Online (Sandbox Code Playgroud)
哪个输出1.0.
我认为应该输出2.0因为结果total/perPage是1.2.
为什么不向上舍入2.0?
给出以下代码:
[...]
public void testFormatDateString() throws ParseException {
String dateString = new java.util.Date().toString();
System.out.println(dateString);
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z YYYY", Locale.ENGLISH);
Date date = format.parse(dateString);
System.out.println(date.toString());
}
[...]
Run Code Online (Sandbox Code Playgroud)
之前:
Sat Aug 19 18:26:11 BST 2017
后:
Sat Jan 07 17:26:11 GMT 2017
为什么日期改变了?
java ×7
java-8 ×2
java-stream ×2
php ×2
spring ×2
spring-boot ×2
bigdecimal ×1
biginteger ×1
ceil ×1
class ×1
date ×1
formatting ×1
freemarker ×1
list ×1
logging ×1
math ×1
mysql ×1
oop ×1
pdo ×1
sorting ×1
spring-mvc ×1
value-of ×1