小编Dav*_*ton的帖子

为什么在numberformatexception发生时声明ioexception?

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.

java

0
推荐指数
1
解决办法
1204
查看次数

类的数组的字母顺序

我有一个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)

java sorting bubble-sort java-7

0
推荐指数
1
解决办法
77
查看次数

acos不给角度的值?

我有一个程序,它具有余弦值.我想要使​​用这个值来找到它对应的角度.然而,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)

java android

0
推荐指数
1
解决办法
521
查看次数

创建从#{}命名的变量?

我有以下代码:

3.times do |n|
    "project#{n}" = FactoryGirl.create(:project, :title => "Project #{n}")
end
Run Code Online (Sandbox Code Playgroud)

这显然不起作用...有谁知道如何制作循环,我可以使用'本地循环变量'来改变变量名称,以生成很多变量,如project1,project2,projekt3?

ruby

0
推荐指数
1
解决办法
96
查看次数

无法从onPostExecute调用方法

我正在使用异步任务来运行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 android

0
推荐指数
1
解决办法
139
查看次数

从命令行运行Java程序

我下载了一个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但我遇到了类似的错误

java

0
推荐指数
1
解决办法
255
查看次数

这样做的目的是什么(); 在这种背景下

请考虑以下代码:

    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"指的是当前对象的字段.这是相同的定义吗?

java

0
推荐指数
1
解决办法
1522
查看次数

Printf问题或程序问题?

试图采用数字的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 printf for-loop while-loop output

0
推荐指数
1
解决办法
135
查看次数

使用引用相等的集合

在Java中是否可以创建HashMap使用引用相等而不是equals()方法?

java collections referenceequals

0
推荐指数
1
解决办法
199
查看次数

为什么Struts 2.x是多圈的?与Struts 1.x相比有哪些优势?

我的团队决定使用Struts 2.x,但我很困惑,因为Struts 1.x动作是单例,像servlet这样的动作是多线程的.Struts 2.x在每个请求上创建新实例,这会增加堆内存使用量.

使用Struts 2.x需要更多内存吗?

struts2 struts-1 web

0
推荐指数
1
解决办法
144
查看次数