This forum is in archive mode. You will not be able to post new content.

Author Topic: [Java] vCard reader  (Read 4984 times)

0 Members and 3 Guests are viewing this topic.

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
[Java] vCard reader
« 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
Sample vCard: 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 {}

}
}

}

}

Offline Huntondoom

  • Baron
  • ****
  • Posts: 856
  • Cookies: 17
  • Visual C# programmer
    • View Profile
Re: [Java] vCard reader
« Reply #1 on: September 20, 2011, 10:43:15 PM »
somebody is going to the develop the Evilzone Mobile app :P? *ugh*you*ugh*
Aslong as you are connected to the internet, you'll have no privacy

Advanced Internet Search
Clean Up!

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: [Java] vCard reader
« Reply #2 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/

In addition:
  • An empty finally block doesn't make any sense.
  • Empty catch blocks are a bad idea in most cases. You won't recognize if something went wrong.
  • You should only import what you need and don't use a wildcard. The star can cause you serious problems if you have some conflicts with other classes that have the same name.
  • You could use keywords for your javadoc comment like @author
  • Read the code conventions: http://www.oracle.com/technetwork/java/codeconv-138413.html
  • You should catch errors that are caused by wrong user inputs like giving something that is not an integer in this line: Index = In.nextInt();
« Last Edit: September 21, 2011, 10:37:53 AM by Deque »

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: [Java] vCard reader
« Reply #3 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?
« Last Edit: September 21, 2011, 10:58:27 AM by Kulverstukas »

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: [Java] vCard reader
« Reply #4 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.
« Last Edit: September 21, 2011, 12:18:17 PM by Deque »

xor

  • Guest
Re: [Java] vCard reader
« Reply #5 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! :)

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.