1

My problem, I have a txt file which look like this :

CA-ADAPT-BAT18AH;CABLE ADAPTATION POUR CHARGER BATTERIE 18Ah

OP-CBL-PX738;CABLE TM 220V BUL 738 4.5M

OP-PAN-CABLE-Y;CABLE Y POUR PIV MINI & MAXI (1 FEM - 2 MAL)

OP-PAN-DATA;Câble datacom

OP-TMSSA-DATA;Câble datacom

OP-SOL-CABLE-1;CABLE POUR PANNEAU SOLAIRE : BOITIER VERS PIV

There is 2 information's per line, separated by an ";"

In my code java I have a var like "String[][] info = new String[25][2];"

I just want to read the whole file an put the 1st information of the 1st line in info[0][0] and the 2nd information of the 1st line in info[0][1]. And do the same process for the rest of the data of the file.

So here is my code :

public class Function {

public static String[][] getCable() throws FileNotFoundException {

    String delim = ";";
    String line = null;
    String[][] info = new String[25][2];
    String[] temp; 
    int i = 0,j = 0,a = 0,b = 0;

    String filePath = "C:/Users/ogh/Desktop/Appli Java/cable.txt";
    Scanner scanner = new Scanner(new File(filePath));

    for (i = 0; i < 25; i++){
        for (j = 0; j < 2; j++){
            if (j == 0) info[i][j] = "ID : "; 
            if (j == 1) info[i][j] = "Descr. : ";
        }
        j = 0;
    }

    while (scanner.hasNextLine()) {
        line = scanner.nextLine(); // get 1 line
        temp = line.split(delim); // split that line in 2 with ";"
        info[b][a] = info[b][a] + temp[a]; 
        info[b][a+1] = info[b][a+1] + temp[a+1];

        if (a >= 1) {
            a = 0;
            b++;
        }
    }

    scanner.close();    

    for (i = 0; i < 25; i++){
        System.out.println(i+ "-> Info :"+ info[i][0]+"\t"+info[i][1]);
    }

    return info;
}

    public static void main(String args[]) throws Throwable {
        getCable();
    }
 }

And the output I get : All the Id are in the info[0][0] and all the description are in the info[0][1]

Thanks.

2
  • 1
    I don't see any a++ in your code. Commented Feb 12, 2014 at 7:50
  • 1
    learn to use a debugger. Most IDE should have already bundled one and such problem is super easy to spot by yourself using debugger by looking at the change of variables when stepping through your code Commented Feb 12, 2014 at 7:55

4 Answers 4

1

You're not incrementing b. Your statement

if (a >= 1) {
    a = 0;
    b++;
}

will never be executed, because a is never >=1. You don't even need that if statement. Just replace it with b++ to go to the next row for each iteration of the while loop.

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

Comments

1

just do this

  while (scanner.hasNextLine()) {
            line = scanner.nextLine(); // get 1 line
            info[a]= line.split(delim); 
            a++;
    }

Comments

1

you can use

temp[a]=String.split(";");
a++;
for (i = 0; i < 25; i++){
    for (j = 0; j < 2; j++){
        if (j == 0) info[i][j] = temp[0]; 
        if (j == 1) info[i][j] = temp[1];
    }
    j = 0;
}

Comments

0

I would read the lines to a List first

List<String> lines = Files.readAllLines(Paths.get("1.txt"),
        StandardCharsets.UTF_8);

and then transform lines into array

String[][] a = new String[lines.size()][];
for(int i = 0; i < lines.size(); i++) {
    a[i] = lines.get(i).split(";");
}

Comments

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.