Tag: int

为什么Integer.parseInt(“1”)++不能在Java中工作?

我有以下代码行: suffix = suffix.isEmpty() ? “1” : Integer.toString(Integer.parseInt(suffix)+1); 在一个块中,后缀已被声明为空字符串( “” )。 该块正在查找重复的文件名并在任何重复项上添加一个数字,因此它们不再具有相同的名称。 上面的代码行编译得很好,但是如果我改成它, suffix = suffix.isEmpty() ? “1” : Integer.toString(Integer.parseInt(suffix)++); 我得到Invalid argument to operation ++/– 。 由于Integer.parseInt()返回和int,为什么我不能使用++运算符?

将用空格分隔的整数读入int 数组

我读了线 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); reader.readLine(); 示例输入是 1 4 6 32 5 读取输入并将其放入整数数组int[]的最快方法是什么? 如果可能的话,我也在寻找一些单线解决方案。

主键类型:int vs long

我知道有些软件商店已经通过使用int类型作为持久化类的主键而被烧毁。 话虽这么说,并非所有的表都增长了20亿。 事实上,大多数人没有。 那么,你们是否只为那些映射到潜在大表的类或者每个持久类只使用一致的类来使用long类型? 什么是行业共识? 我会暂时搁置这个问题,以便您与我们分享您的成功/恐怖故事。

读取文本文件始终返回0 – Java

我正在尝试读取文本文件以获取版本号但由于某种原因,无论我放在文本文件中它总是返回0(零)。 文本文件名为version.txt,它不包含空格或字母,只包含1个字符作为数字。 我需要它来返回那个号码。 关于为什么这不起作用的任何想法? static int i; public static void main(String[] args) { String strFilePath = “/version.txt”; try { FileInputStream fin = new FileInputStream(strFilePath); DataInputStream din = new DataInputStream(fin); i = din.readInt(); System.out.println(“int : ” + i); din.close(); } catch(FileNotFoundException fe) { System.out.println(“FileNotFoundException : ” + fe); } catch(IOException ioe) { System.out.println(“IOException : ” + […]

Java:InputStream read()返回一个大于127的字节?

我有这段代码: InputStream is = socket.getInputStream(); int b; while ((b = is.read()) != -1) { System.out.println(b); } 一个字节,其范围是-128直到+127 。 但其中一个打印字节是210 。 这是将读取byte转换为int吗? (这样negatif byte变成了一个positif int ) 如果是这样,我可以通过将int转换为byte来做同样的事情(使用OutputStream )吗? 谢谢, 马亭

从double到int的可能有损转换

为什么我得到Possible lossy conversion from double to int错误,我该如何解决? public class BinSearch { public static void main(String [] args) { double set[] = {-3,10,5,24,45.3,10.5}; double l = set.length; double i, j, first, temp; System.out.print(“Before it can be searched, this set of numbers must be sorted: “); for (i = l-1; i>0; i–) { first=0; for(j=1; j<=i; j++) […]

十进制补码算术实现中的ArrayIndexOutOfBoundsException

我的代码试图实现一个算法 从控制台获取两个整数和操作数+或-用户输入 将这些数字逐位存储在int[50] ,以十进制补码格式表示负数, 实现(十进制)逐位加/减操作, 以十进制格式打印结果而不带前导零。 但是,在我目前的实施中,存在两个问题 添加99 + 9999时,打印结果为01098而不是预期的010098。 当减去99 – 9999时,我得到一个ArrayIndexOutOfBoundsException: 50而不是预期的结果-09900。 import java.util.*; public class Program9 { public static String getOperand() { Scanner scan = new Scanner(System.in); String stringOfInteger; System.out.print(“Please enter an integer up to 50 numbers: “); stringOfInteger = scan.nextLine(); return stringOfInteger; } public static int[] convert(String operand) { int[] integer […]

设置双格式,带2位小数

我有一个简短的等式: double compute,computed2; compute=getminutes/60; 其中getminutes是int ,我想设置与2位小数相等的compute 。 0.00如何格式化等效2个小数位? 例: compute=45/60 it should be 0.75 这是我的工作: DecimalFormat df2 = new DecimalFormat(“00.00000”); double computed,computed2 = 00.000; computed=60/getitbyminutes; df2.format(computed); computed2=computed-8; df2.format(computed2); System.out.printf(“%1$.2f”,computed); System.out.println(); System.out.printf(“%1$.2f”,computed2); 输出将如下: 1.00 7.00

Java将2个整数与equals或==进行比较?

我是非常非常新的Java,我想知道如何比较2个整数? 我知道==完成工作……但是等于什么? 这可以比较2个整数吗? (当我说整数时,我的意思是“int”而非“整数”)。 我的代码是: import java.lang.*; import java.util.Scanner; //i read 2 integers the first_int and second_int //Code above if(first_int.equals(second_int)){ //do smth } //Other Code 但由于某种原因,这不起作用..我的意思是Netbeans给了我一个错误:“int不能被解除引用”为什么?

究竟什么是双重意味着在java?

我是Java的新手,只想确认Double是什么? 它与Float或Int类似吗? 任何帮助,将不胜感激。 我有时也看到大写的Double和其他时候的小写double 。 如果有人能澄清这意味着什么是伟大的!