小编Dan*_*son的帖子

如何在数组中只打印偶数或奇数字符?

让我们说我有字符串:'救火车'.然后我将该字符串分成单个字母并将它们放入一个名为T的数组中.所以现在T []看起来像{f,i,r,e,t,r,u,c,k}.我如何才能打印偶数个字符,所以我的print语句看起来像"frtuk"和奇看起来像"IERC".这是我到目前为止所得到的:

import java.util.Scanner;
import java.util.Arrays;
public class StringFun {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String even_odd = sc.next();
        char[] t = even_odd.toCharArray();
        System.out.println(Arrays.toString(t));

        //I can't get this next part to work.
        for(int i = t[0]; i < t.length; i = i + 2){
            System.out.println(Arrays.toString(t[i]));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

java arrays for-loop

2
推荐指数
1
解决办法
2万
查看次数

无论操作系统如何创建文件和文件夹

我写了一个程序,下面是它的一部分.当我在计算机上运行它时,它运行得很好.但是当我把它复制到我朋友的电脑上时,就会显示FileNotFoundException在第四行.

String d = System.getProperty("user.home");
String dir = d + "\\Documents\\CarPark";
new File(dir + "\\abnormal").mkdir();
PrintWriter restoreNo = new PrintWriter(new FileOutputStream(dir + "\\abnormal\\restoreNo.txt"));
Run Code Online (Sandbox Code Playgroud)

谁知道问题出在哪里?谢谢!

java file-io

2
推荐指数
1
解决办法
979
查看次数

如何使JavaFX Web浏览器显示警报并确认消息

我的Java Web浏览器不显示alert(“ message”); 并确认(“消息”);要么,我通常使用c#Web浏览器组件,它的工作原理很完美,但是我是新手。

public void  openPage(String url){
    JFXPanel jfxPanel = new JFXPanel();
    JFrame frame = new JFrame("");
    frame.add(jfxPanel);
    Platform.runLater(() -> {
        WebView webView = new WebView();
        jfxPanel.setScene(new Scene(webView));
        webView.getEngine().load(url);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    });
}
Run Code Online (Sandbox Code Playgroud)

java javafx webbrowser-control

2
推荐指数
1
解决办法
4835
查看次数

如何使用 Apache POI 为 docx 文件中的段落设置标题样式?

我正在尝试使用 poi 创建一个 docx 文件,但我无法为段落设置标题样式。

XWPFDocument document= new XWPFDocument(); 

//Write the Document in file system
FileOutputStream out = new FileOutputStream(new File("C:/Users/2/Desktop/RequirementModelDocument.docx"));

XWPFParagraph paragraph = document.createParagraph();
XWPFRun run=paragraph.createRun();


paragraph.setAlignment(ParagraphAlignment.LEFT);
paragraph.setStyle("Heading1");

run.setText(reqLevel.getName());
run.setBold(true);
run.setFontFamily("Calibri Light (Headings)");
Run Code Online (Sandbox Code Playgroud)

它就像忽略了这paragraph.setStyle("Heading1");条线。我看过 apache 的例子,但我看不到任何关于这个问题的例子。

java file docx apache-poi

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

如何提高Java计算的准确性

在某些情况下,我需要在Java中进行非常精确的计算,但它总是会出现一些意外错误.如何避免它们或将错误保持在可接受的范围内?

例如

public static void main(String[] args) throws Exception {
    double x = 0.0;
    while (x <= 1.0){
        System.out.println(x);
        x += 0.1;
        System.out.println("add 0.1");
    }
}



- the result will be 

   0.0 add 0.1
   0.1 add 0.1
   0.2 add 0.1
   0.30000000000000004 add 0.1
   0.4 add 0.1
   0.5 add 0.1
   0.6 add 0.1
   0.7 add 0.1
   0.7999999999999999 add 0.1
   0.8999999999999999 add 0.1
   0.9999999999999999 add 0.1
Run Code Online (Sandbox Code Playgroud)

这不是预期的.

提前致谢

java

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

如何在JAVA中进行REST Webservice调用?

目前我有一个在tomcat中运行的Web服务(http:// localhost:8080/myApp/getUsers).我的Web服务将接受json字符串,然后进行相应的处理.我的网络服务代码如下:

@Path("/getUsers")
public class UsersWS
{
    @POST
    public Response post(String theRequestJSON)
    {
        try
        {
            JSONObject aJsonObj = new JSONObject(theRequestJSON);
            String userID = aJsonObj.getString("userID");   
            System.out.println(userID);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所以,我的Web服务正在处理一个json字符串.所以现在,我需要从另一个JAVA类调用上面的Web服务(jsonObject在请求参数中具有userID).

怎么做?不久,我需要使用JSON对象作为请求参数从JAVA类进行Web服务调用.如何在请求调用中将json作为请求参数发送.

java rest web-services

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

使用 owl api 获取单个属性

我正在尝试读取存储在本体中的信息。XML 绑定(我正在处理的部分)是:

<!-- hasPrevious and hasNext are defined at the imported ontology -->
<owl:NamedIndividual   rdf:about="http://www.myexampledomain.com/myExample.owl#one_relationship">
    <rdf:type rdf:resource="http://www.myexampledomain.com/myExample.owl#typeA"/>
    <intui_PO:hasPrevious rdf:resource="http://www.myexampledomain.com/myExample.owl#element01"/>
    <intui_PO:hasNext rdf:resource="http://www.myexampledomain.com/myExample.owl#element02"/>
</owl:NamedIndividual>
Run Code Online (Sandbox Code Playgroud)

我使用以下 Java 代码:

//Create factories that will produce the objects
OWLReasonerFactory reasonerFactory = new StructuralReasonerFactory();
OWLDataFactory fac = man.getOWLDataFactory();

//Get a reasoner, to query the ontology
OWLReasonerConfiguration config = new SimpleConfiguration();
OWLReasoner reasoner = reasonerFactory.createReasoner(owlOnt, config);

//Get relations. Their properties are the related individuals
OWLClass myclass = fac.getOWLClass(IRI.create("http://www.myexampledomain.com/myExample.owl#RelationClass"));
NodeSet<OWLNamedIndividual> individualsRelationNodeSet = reasoner.getInstances(myclass,false);
Set<OWLNamedIndividual> relations = individualsRelationNodeSet.getFlattened();
Run Code Online (Sandbox Code Playgroud)

有了这个,我就找到了命名的关系。我想通过以下方式读取它们的属性:

Map<OWLObjectPropertyExpression,Set<OWLIndividual>> …
Run Code Online (Sandbox Code Playgroud)

java owl ontology

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

用于空输入的Java方法

我有一个java方法,它应该检查20个输入参数为空并返回一条消息

例:

public String CheckEmpty(String a,String b,String c, String d,String e, String f, String g, String i,String j, String k,.....) {
    String str="";
    if(a.isEmpty()){
        str= "a is empty";
    }
    if(b.isEmpty()){
        str= "b is empty";
    }
    return str;
} 
Run Code Online (Sandbox Code Playgroud)

我需要检查所有输入的if条件?有大约20个输入或是否有任何有效的方法在java中进行相同的检查?

请指教.

java methods java-7

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

遍历Java中的List &lt;Map &lt;String,String &gt;&gt;

我正在尝试List<Map<String, String>>用Java 进行迭代。但是,我无法正确地对其进行迭代。谁能指导我?

Iterator it = list.iterator();
while (it.hasNext()) {
    Map.Entry pairs = (Map.Entry) it.next();
    System.out.println(pairs.getKey() + " = " + pairs.getValue());
}
Run Code Online (Sandbox Code Playgroud)

谢谢,

java

-2
推荐指数
1
解决办法
2万
查看次数

为什么以下代码中存在NullPointerException?

下面的代码显示了NullPointerException

public class Q117d { 
    static int[] a;
    public static void main (String[] args) {
        a[-1] = 2;
    }
}
Run Code Online (Sandbox Code Playgroud)

下面的代码显示了ArrayIndexOutOfBound异常

public class Q117c{
    static int[] a;
    public static void main(String[] args) {
        a=new int[5];
        a[0]=2;
        a[2]=3;
        a[-1]=2;
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么两个代码都显示不同的异常,因为在[-1]中都应该为空?

java arrays

-12
推荐指数
1
解决办法
153
查看次数