2

I have a CSV file that is like:

  • Orbit,a,e,i
  • LEO,7168000,0,90
  • MEO,20200000,0,54

What I want is that using scanner, I will be able to choose one of the two orbits I have in the CSV file. Let's say for example, using scanner if I put 1 in the console, it chooses the row of the CSV file: LEO,7168000,0,90 and if I put 2 the line: MEO,20200000,0,54.

After that from the row chosen it saves each parameter in a variable skipping the name (LEO,MEO). For example if I choose LEO orbit, it saves the variables like:

  • double a = 7168000;
  • double e = 0;
  • double i = 90;

So in the end I can use those parameters in my program. Thank you for your answers.

2
  • If you are reading csv then I suggest using a csvReader Commented Mar 13, 2019 at 2:34
  • And how would it be coded? I am sorry but I am quite new using Java. Commented Mar 13, 2019 at 14:43

1 Answer 1

1

Csv file is a plain text file and separates with , character for each cell and \n for each row.

The easy way, you only need using FileInputStream to read and split \n and , character for using.

File file = new File("file.csv");
FileInputStream fis = null;
String dataStr = "";
try {
    fis = new FileInputStream(file);
    int content;
    while ((content = fis.read()) != -1) {
        dataStr += (char) content;
    }
} catch (IOException e) {
    e.printStackTrace();
}
// convert to array rows string
String[] dataRows = dataStr.split("\n");

// loop rows to get cells string
for (int i = 0; i < dataRows.length; i++) {
    String[] dataCells = rowData[i].split(",");
    //do what ever you want with dataCells
}

Thanks for read.

Sign up to request clarification or add additional context in comments.

6 Comments

Not so fast. Sepatator may be "," tab ";" or something else. Strings may be contains sepatators and must be escaped, etc.
Thank you for your answer. But how can I do the scanner part to for example from the dataCells choose only one of the options because if I print dataCells[1] I get: a 7168000 20200000 And I only want to get one of the values.
And also the dataCells are String and I want each value as double as I will use it later on the code as parameter.
@Raúl you can use Double Array to save data for easy get data like a matrix of string String[][], And if you need to use data with double type, you can use method Double.parseDouble(string).
data with double type, you can use method Double.parseDouble(string). another way, you create the class that defines all attribute you need Orbit,a,e,i. Then define the list of the class defined and set it for each value of dataCells after split.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.