try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
str = br.readLine();
i = Integer.parseInt(str);
} catch(NumberFormatException e) {
System.out.Println("enter a valid input");
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译此代码时,它会抛出一个编译错误,ioexception正在发生我应该抓住它.
因此我必须添加一个catch(IOException e)语句,但发生的异常是java.lang库的数字格式异常,所以我为什么要捕获ioException.
我有一个Class数组Customer.我想按字母顺序重新排序Class数组中Customer基于所调用字段的元素name.
这是我正在使用的代码:
int j;
boolean flag = true; // will determine when the sort is finished
Customer temp;
while ( flag )
{
flag = false;
for ( j = 0; j < count_customers; j++ )
{
if ( customers[ j ].getName().toString().compareToIgnoreCase( customers[ j + 1 ].getName().toString() ) > 0 )
{
temp = customers[ j ];
customers[ j ] = customers[ j+1 ]; // swapping
customers[ j+1 ] = temp;
flag = true; …Run Code Online (Sandbox Code Playgroud) 我有一个程序,它具有余弦值.我想要使用这个值来找到它对应的角度.然而,acos实际上并没有给我一个角度值(arcos通常就是这样).相反,我得到了这个(见下文).为什么会发生这种情况,acos会这样吗?
double r,cos,sin,angVal;
String temp = real.getText().toString();
double a = Double.parseDouble(temp);
temp=imag.getText().toString();
double b =Double.parseDouble(temp);
r=Math.sqrt(a*a + b*b);
cos = a/r;
sin = b/r;
angVal = Math.acos(cos);
ans.setText("r = "+r+"\ncos = "+cos+"\nsin = "+sin+"\nThe angle = "+angVal+"");
Run Code Online (Sandbox Code Playgroud)

我有以下代码:
3.times do |n|
"project#{n}" = FactoryGirl.create(:project, :title => "Project #{n}")
end
Run Code Online (Sandbox Code Playgroud)
这显然不起作用...有谁知道如何制作循环,我可以使用'本地循环变量'来改变变量名称,以生成很多变量,如project1,project2,projekt3?
我正在使用异步任务来运行JSON下载器:(删节)
public class JSONDownloader extends AsyncTask<Object, Object, Object>{
@Override
protected Object doInBackground(Object... params) {
if(JSONstate == false){
try {
final URL url = new URL([REDACTED]);
final URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
urlConnection.connect();
final InputStream inputStream = urlConnection.getInputStream();
final StringBuilder sb = new StringBuilder();
while (inputStream.available() > 0) {
sb.append((char) inputStream.read());
}
String result = sb.toString();
JSONObject jsonOrg = new JSONObject(result);
String ok = "ok";
Response = jsonOrg.getString("response");
System.out.println(Response);
if(Response.equals(ok)){
Settingsresponse = true;
orgName = jsonOrg.getString("orgName");
System.out.println("orgName" + orgName); …Run Code Online (Sandbox Code Playgroud) 我下载了一个java程序,它包含两个文件夹src和分别包含源文件和类文件的类.现在,src和classes文件夹包含几个嵌套的子文件夹,其中最后一个子文件夹分别包含源文件和类文件.更确切地说,源文件和类文件的路径是src/edu/univ/.java和classes/edu/univ/.class.鉴于包含main函数的文件是Main.java,如何从命令行运行该程序.
我试过了:
java src/edu/univ/Main but I get Exception in thread "main" java.lang.NoClassDefFoundError: src/edu/univ/Main
Run Code Online (Sandbox Code Playgroud)
我也尝试过:java src.edu.univ.Main但我遇到了类似的错误
请考虑以下代码:
public Fingerprint(HashMap<String, Integer> measurements) {
this();
mMeasurements = measurements;
}
public Fingerprint(HashMap<String, Integer> measurements, String map) {
this(measurements);
mMap = map;
}
public Fingerprint(int id, String map, PointF location) {
this();
mLocation = location;
}
public Fingerprint(int id, String map, PointF location, HashMap<String, Integer> measurements) {
this(id, map, location);
mMeasurements = measurements;
}
Run Code Online (Sandbox Code Playgroud)
这样做的目的是什么(); 在这种背景下?因为我认为"this"指的是当前对象的字段.这是相同的定义吗?
试图采用数字的arraylist并打印出以下内容......
MOST POPULAR NUMBERS
The following numbers were picked 263 times: 41
LEAST POPULAR NUMBERS
The following numbers were picked 198 times: 20
AVERAGE
The Average was 228.545455 times.
The following numbers were picked 228 times: 5 22
The following numbers were picked 229 times: 2 7 12 40
Run Code Online (Sandbox Code Playgroud)
我的代码......
import java.util.*;
import java.io.*;
import java.util.Arrays;
import java.util.Collections;
public class Hmwk {
public static void main(String[] args) throws FileNotFoundException {
Scanner input=new Scanner (new File ("input.txt"));
int counter = 0; …Run Code Online (Sandbox Code Playgroud) 在Java中是否可以创建HashMap使用引用相等而不是equals()方法?
我的团队决定使用Struts 2.x,但我很困惑,因为Struts 1.x动作是单例,像servlet这样的动作是多线程的.Struts 2.x在每个请求上创建新实例,这会增加堆内存使用量.
使用Struts 2.x需要更多内存吗?
java ×8
android ×2
bubble-sort ×1
collections ×1
for-loop ×1
java-7 ×1
output ×1
printf ×1
ruby ×1
sorting ×1
struts-1 ×1
struts2 ×1
web ×1
while-loop ×1