EvilZone

Programming and Scripting => Java => Topic started by: Kulverstukas on September 20, 2011, 09:19:39 PM

Title: [Java] vCard reader
Post by: Kulverstukas on September 20, 2011, 09:19:39 PM
Another Script from me... yeah, useless, but I have to learn Java somehow ya know :D

This script supposed to read the VCF (vCard 2.1) file such as when you export your contacts in the SIM card on Android phones.
Script will read name (FN:) and phone number (next line), put them into an array and you can view the record by it's index.

Code: http://newage.ql.lt/projects/java/vCardReader/CLI/vCardReader.java (http://newage.ql.lt/projects/java/vCardReader/CLI/vCardReader.java)
Sample vCard: http://newage.ql.lt/projects/java/vCardReader/CLI/vCard.vcf (http://newage.ql.lt/projects/java/vCardReader/CLI/vCard.vcf)

Code: [Select]
package vCardReader;

import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;

public class vCardReader {

/**
*
* Author: Kulverstukas
* Written on: 2011.09.20
* http://newage.ql.lt/
* Evilzone.org
* ----
* Program that reads a vCard file, such as one
* you get when exporting contacts from SIM card
* with Android phones.
*
*/

public static void PrntHlp() {
System.out.println(" Usage:\n" +
           "\tvCardReader PATH\n" +
           "\tWhere PATH is full path name+file name\n" +
           "\tto your vCard file.\n");
}

public static void main(String[] args) {
PrntHlp();
if (args.length == 0) { System.out.println("No args were given!"); }
else if (args.length >= 1) {
File vCardPath = new File(args[0]);
if (vCardPath.isFile() == false) { System.out.println("Given argument is not a file\nor it does not exist!"); }
else {
try {
String Comp = "";
String Comp1 = "";
Scanner vCardFile = new Scanner(new File(args[0]));
ArrayList<String> Contacts = new ArrayList<String>();
while (vCardFile.hasNext()) {
Comp = vCardFile.nextLine();
if (Comp.contains("FN:")) {
Comp1 = vCardFile.nextLine();
Contacts.add(Comp.substring(3)+":"+Comp1.substring(14));
}
}
Scanner In = new Scanner(System.in);
System.out.print("Found "+(Contacts.size()-1)+" people in the vCard.\n" +
                 "Enter bigger number than there are people in the list to break\n"+
         "Enter a number from 0 to "+(Contacts.size()-1)+" to see the phone number: ");
int Index = 0;
while (Index <= Contacts.size()-1) {
Index = In.nextInt();
if (Index > Contacts.size()-1) { System.out.println("Entered bigger number than there are people. Terminating."); break; }
System.out.println("\nSelected #"+Index+". Info:\n" +
           "Name: "+Contacts.get(Index).split(":")[0]+"\n" +
           "Phone: "+Contacts.get(Index).split(":")[1]+"\n");
System.out.print("Pick new contact: ");
}
} catch (FileNotFoundException e) {}
finally {}

}
}

}

}
Title: Re: [Java] vCard reader
Post by: Huntondoom on September 20, 2011, 10:43:15 PM
somebody is going to the develop the Evilzone Mobile app :P? *ugh*you*ugh*
Title: Re: [Java] vCard reader
Post by: Deque on September 21, 2011, 10:35:55 AM
I have the feeling you have to learn Java, but don't really want to. xor gave a lot of good advices and you ignored most of them. But maybe you wrote this script (and the number generator one) before you read his comment, I don't know exactly. Anyway, just read xor's comment again if you want to improve your Java: http://evilzone.org/other/%28java%29-countmyfiles/ (http://evilzone.org/other/%28java%29-countmyfiles/)

In addition:
Title: Re: [Java] vCard reader
Post by: Kulverstukas on September 21, 2011, 10:55:56 AM
@Deque:
Seems to me that you are coding in Java, but not well enough to "remember" that you must write try-finally statement with exception catching, else it will give you errors and won't compile.
Yeah my code is ugly, just learning the syntax and code logic first...

Can't get rid of some habits (if you can call them like that...) from Python and Delphi, like variable naming. Who said that Java MUST have variable names starting with first lowercase letter. Doesn't matter to the compiler or the code flow so why bother?
Title: Re: [Java] vCard reader
Post by: Deque on September 21, 2011, 12:08:11 PM
@Deque:
Seems to me that you are coding in Java, but not well enough to "remember" that you must write try-finally statement with exception catching, else it will give you errors and won't compile.
You should try it out, before you claim I am not right. You don't need a finally if you have a catch.
The catch should at least print the stackstrace. Another possibility is to throw the error by putting throws FileNotFoundException to your method declaration. Then you don't even need a try-catch and it is still better than having an empty catch-block.

Can't get rid of some habits (if you can call them like that...) from Python and Delphi, like variable naming. Who said that Java MUST have variable names starting with first lowercase letter. Doesn't matter to the compiler or the code flow so why bother?
The Java code conventions say that.
You don't need to bother as long as you are coding for yourself. But it is important if you publish your code. A lot of the conventions are there to prevent you from making errors, not only for readability. But readability is very important too, especially if others have to deal with your code, don't underestimate it.
Title: Re: [Java] vCard reader
Post by: xor on September 21, 2011, 06:18:08 PM
It's nice to have another java programmer on the forum who knows what they're talking about! :)