在Java中,十六进制数可以存储在原始整数类型中.
private static volatile final synchronized int x = 0x2FE;
Run Code Online (Sandbox Code Playgroud)
但是,使用Scanner类的nextInt()方法以十六进制读取会引发输入不匹配异常.如何在不将十六进制转换为另一个基数(例如两个或十个或其他)的情况下读取十六进制数字.谢谢.
编辑:
此代码抛出相同的异常.我在这做错了什么:
import java.util.Scanner;
public class NewClass {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//scan.useRadix(16);
int[] input = new int[10];
for (int i = 0; i < 10; i++) {
//input[i] = scan.nextInt(16);
System.out.println(input[i]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
再次感谢.
当我尝试在我的程序上使用scanner.hasNextLine()时,它只是坐在那里并挂起.我想知道是否有任何方法可以解决这个问题或我可以使用的任何替代方案.编辑:这背后的原因是我可以从扫描仪中删除所有内容(扫描仪只有一行).因此,如果有办法从扫描仪中删除所有内容,那将更容易.
我正在尝试使用Java Scanner从文本文件(File.txt)中读取20位长.
java.util.Scanner filereader = new java.util.Scanner(new File("File.txt"));
longNumber = (long) filereader.nextLong();
Run Code Online (Sandbox Code Playgroud)
这将返回以下错误:
Exception in thread "main" java.util.InputMismatchException: For input string: "37107287533902102798"
at java.util.Scanner.nextLong(Scanner.java:2271)
at java.util.Scanner.nextLong(Scanner.java:2225)
at scanner.Scanner.main(Scanner.java:14)
Run Code Online (Sandbox Code Playgroud)
当我将数字的长度减少到19位或更少时,它运行得很好.有人可以解释我如何使用20+数字吗?
我正在为uni编写一个实验室任务的程序,代码将使得相当明显的是什么,但是当它要求第一行时,即循环中的第一个1,并且插入一个字符串并按Enter键,它会自动直接跳到循环中的最后一个增量(5)任何想法?
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
public class limmerickWriter {
public static void main(String[] args)throws Exception {
Scanner limScan = new Scanner(System.in); //scanner to read user input
System.out.println("please enter the name of the file");
String fileName;
fileName = limScan.next(); //filename for the text file
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileName))); //declaring a new file writer
for (int i = 1; i <= 5; i++) //loop to get 5 seperate lines from the user
{
System.out.println("please enter …Run Code Online (Sandbox Code Playgroud) 我正在尝试编辑matlab文件并在某些特定行中替换一些编码部分init.但是,使用下面的格式进行更改它根本不会更改行上下文.(它将打印相同的旧行).知道我做错了什么吗?'replaceAll'不适合用行中的其他单词替换某些单词?
提前致谢.
try {
PrintWriter out = new PrintWriter(new FileWriter(filenew, true));
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.contains("stream.Values(strmatch('Test',stream.Components,'exact'))") {
String newline = line.replaceAll("stream.Values(strmatch('Test',stream.Components,'exact'))", "New Data");
out.println(newline);
System.out.println(newline);
} else {
out.write(line);
out.write("\n");
}
} // while loop
out.flush();
out.close();
scanner.close();
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud) 如何使用扫描仪类从用户端获取数组输入?任何其他方法也将不胜感激.例如,如果我们需要创建一个数组然后从用户端获取输入.谢谢!!
我是java的新手,我正在尝试创建一个程序,首先要求用户输入一些数字,然后输出一个卷,区域等等.另外我想显示一个矩形,我不知道怎么做,因为我的程序运行正常,它不会显示矩形.我能做什么?
package testchap3;
import java.util.*;
import javax.swing.JApplet;
import java.awt.*;
public class Chapter_3 extends JApplet
{
public void paint(Graphics page)
{
page.drawRect(50,50,60,60);
}
public static void main(String[] args)
{
int lenght,width,height,volume,Area,Perimeter;
Scanner scan = new Scanner(System.in);
System.out.println("What is the lenght:");
lenght = scan.nextInt();
System.out.println("What is the height:");
height = scan.nextInt();
System.out.println("What is the width:");
width= scan.nextInt();
volume = (lenght*height*width);
Area= volume/height;
Perimeter= lenght+width+lenght+width;
System.out.println("Your volume is:"+volume);
System.out.println("Your Area is:"+ Area);
System.out.println("Your perimeter is:"+Perimeter);
}
}
Run Code Online (Sandbox Code Playgroud) import java.io.*;
import java.util.*;
public class Solution {
public static final int n = 26;
public int check(String arr) {
if (arr.length() < n) {
return -1;
}
for (char c = 'A'; c <= 'Z'; c++) {
if ((arr.indexOf(c) < 0) && (arr.indexOf((char)(c + 32)) < 0)) {
return -1;
}
}
return 1;
}
}
public static void main(String[] args) {
Scanner s1 = new Scanner(System.in);
String s = s1.next();
Solution obj = new Solution();
int d = …Run Code Online (Sandbox Code Playgroud) 我想从如下文本文件中读取单词:
"A","ABILITY","ABLE","ABOUT","ABOVE","ABSENCE","ABSOLUTELY","ACADEMIC","ACCEPT","ACCESS","ACCIDENT","ACCOMPANY", ...
Run Code Online (Sandbox Code Playgroud)
我使用读取单词,split("\",\"")因此将它们放在矩阵中。不幸的是,我不能跳过读取第一个引号的操作,该引号会启动我的.txt文件,因此,在我的控制台中,我有:
"A
ABILITY
ABLE
ABOUT
ABOVE
Run Code Online (Sandbox Code Playgroud)
您知道如何跳过第一个引号吗?我都在尝试
Scanner in = new Scanner(file).useDelimiter("\"");
Run Code Online (Sandbox Code Playgroud)
和parts[0].replace("\"", "");,但不起作用。
package list_1;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class exercise {
public static void main(String[] args) throws FileNotFoundException{
File file = new File("slowa.txt");
Scanner in = new Scanner(file).useDelimiter("\""); //delimiter doesn't work!
String sentence = in.nextLine();
String[] parts = sentence.split("\",\"");
parts[0].replace("\"", ""); //it doesn't work!
for (int i=0; i<10 ; i++){
System.out.println(parts[i]);
}
}
}
Run Code Online (Sandbox Code Playgroud) 当我运行以下代码时:
class Startup(被称为main()):
import java.util.ArrayList;
public class Startup {
public void start() {
// Build rooms
final int WIDTH = 2;
final int HEIGHT = 2;
Room[][] room = new Room[WIDTH][HEIGHT];
Rooms.build(room, WIDTH, HEIGHT);
int x = 0;
int y = 0;
// Print starting room description
Rooms.print(room, x, y);
// Start game loop
boolean playing = true;
while (playing) {
// Get user input
String input = Input.getInput();
System.out.println(input);
// Movement commands
if (input.equals("n")) {
if …Run Code Online (Sandbox Code Playgroud) java ×10
user-input ×2
file-io ×1
hex ×1
iteration ×1
long-integer ×1
loops ×1
pangram ×1
printwriter ×1
replaceall ×1
string ×1
swing ×1
text ×1