gpt4 book ai didi

java - java中类之间传递字符串数组的方法

转载 作者:行者123 更新时间:2023-12-02 06:41:27 25 4
gpt4 key购买 nike

我正在尝试将 csv 批量加载到 neo4j 中,但在将字符串数组从 csv 加载器类传递到数据库创建器类时遇到问题。我在 csvloader 和 return 语句中都遇到错误(方法的返回类型丢失和语法错误)。在 NodeLoader 中,当我尝试调用该方法时,无法解析 CellInput。我想我已经盯着这个太久了。

这是我的 .csv 阅读器:

package com.Neo4J;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class CsvLoader {

public CellInput(String[] args){

String fileName = "data.csv";
File file = new File(fileName);
try {
Scanner inputStream = new Scanner(file);
inputStream.next(); //ignore first line
while (inputStream.hasNext()){
String data = inputStream.next();
String[] value = data.split(",");
return String[] value;

}
inputStream.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

这是数据库创建者:

package com.Neo4J;

import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;

public class nodeLoader {

// This is the path for Neo4J
private static final String Neo4J_DBPath = "/Program Files (x86)/neo4j-community-1.9.3";

Node one;
Node two;
Node three;
Node four;
Node five;
Node six;
Node seven;

Relationship relation;
GraphDatabaseService graphDataService;

// list of relationships first knows second
private static enum RelTypes implements RelationshipType {
CONNECTS
}

public static void main(String[] values) {
// TODO Auto-generated method stub
nodeLoader nodes = new nodeLoader();
nodes.createDatabase(values);
nodes.removeData();
nodes.shutDown();

}

void createDatabase(String[] values) {
// GraphDatabaseService
graphDataService = new GraphDatabaseFactory()
.newEmbeddedDatabase(Neo4J_DBPath);

// Begin Transaction
Transaction transaction = graphDataService.beginTx();
CsvLoader Loader= new CsvLoader();

try {
// Create Node & set properties
one = graphDataService.createNode();
one.setProperty("Source IP", Loader.CellInput[0]);
two = graphDataService.createNode();
two.setProperty("Target Port", Loader.CellInput[1]);
three = graphDataService.createNode();
three.setProperty("Protocol", Loader.CellInput[2]);
four = graphDataService.createNode();
four.setProperty("Reports", Loader.CellInput[3]);
five = graphDataService.createNode();
five.setProperty("Targets", Loader.CellInput[4]);
six = graphDataService.createNode();
six.setProperty("First Seen", Loader.CellInput[5]);
seven = graphDataService.createNode();
seven.setProperty("Last Seen", Loader.CellInput[6]);

// Relationship
relation = one.createRelationshipTo(two, RelTypes.CONNECTS);
relation = two.createRelationshipTo(three, RelTypes.CONNECTS);
relation = three.createRelationshipTo(four, RelTypes.CONNECTS);
relation = four.createRelationshipTo(five, RelTypes.CONNECTS);
relation = five.createRelationshipTo(six, RelTypes.CONNECTS);
relation = six.createRelationshipTo(seven, RelTypes.CONNECTS);
relation = seven.createRelationshipTo(one, RelTypes.CONNECTS);

relation.setProperty("relationship-type", "CONNECTS");
System.out.println(one.getProperty("Source IP").toString()
+ relation.getProperty("relationship-type").toString()
+ two.getProperty("Target Port").toString()
+ three.getProperty("Protocol").toString()
+ four.getProperty("Reports").toString()
+ five.getProperty("Targets").toString()
+ six.getProperty("First Seen").toString()
+ seven.getProperty("Last Seen").toString());

// Success Transaction
transaction.success();
} finally {
// finish the transaction
transaction.finish();
}
}

void removeData() {
Transaction transaction = graphDataService.beginTx();
try {
// delete
one.getSingleRelationship(RelTypes.CONNECTS, Direction.OUTGOING).delete();
System.out.println("Nodes are removed");

// delete nodes
one.delete();
two.delete();
three.delete();
four.delete();
five.delete();
six.delete();
seven.delete();

transaction.success();
} finally {
// finish the transaction
transaction.finish();

}
}

void shutDown() {
// shut down graphDataService
graphDataService.shutdown();
System.out.println("Neo4J database is shut down");
}

}

最佳答案

您收到的错误中列出了部分(但不是全部)问题:

1) CellInput 方法缺少返回类型。我从代码中假设您想要返回一个 String[],在这种情况下,方法签名将是:

public String[] CellInput(String[] args){

2) 同一方法中的return 语句格式错误。 value 已经是一个 String[],所以你要做的就是

return value;

3) 您从 while 循环内部返回,因此您只会获得一行。这是故意的吗?

4) 你需要在 try/catch 之外有一个 return 语句:编译器会正确地注意到,如果 try/catch 内的任何语句抛出异常,你将无法到达 return。

这应该足以帮助您继续前进。

关于java - java中类之间传递字符串数组的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19095217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com