Algorithm (Interesting) Programming in Java

Hi,
I have a String concat = "ABC:DEF:GHIJ:KLMN". I need to parse this in such a way that I need to store the details in the hashtable as follows.
ht.put("ABC", DEF"); and ht.put("GHIJ", "KLMN"); I have written a code that works, but thats not efficient. I have another code that fails because of the last condition. I am enlosing the code that fails for the last condition. Could abybody provide me an efficient way to do this.
//Code that fails for last String.
String concat = "ABC:DEF:GHIJ:KLMN:OPQ:RST";
int length = concat.length();
int j;
for(; length > 0 ;)
     j = concat.indexOf(':');
String key = concat.substring(0, j);
     System.out.println("Key " + key);
     concat = concat.substring(j + 1, length);
     System.out.println("Concat " + concat);
     length = concat.length();
     System.out.println("length " + length);
     j = concat.indexOf(':');
     String value = concat.substring(0, j);
     System.out.println("Value " + value);
     concat = concat.substring(j + 1, length);
     System.out.println("Concat " + concat);
     length = concat.length();
     System.out.println("length " + length);
//Code that works but not effiecient
String concat = "ABC:DEF:GHIJ:KLMN";
          Hashtable ht = new Hashtable();
          StringTokenizer str = new StringTokenizer(concat, ":");
          int count = str.countTokens();
          String v=new String();
          String Key=new String();
          String val=new String();
          for(int i = 0; i < count; i++)
               v=str.nextToken();;
               if(i%2==0)
                    Key=v;
               else
                    val=v;
          if(i%2 != 0)
               System.out.println("Keys " + Key + "Val " + val);
               ht.put(Key, val);

Sorry, the right way is: (note that countTokens() should be a method, not a member variable.
String tst "ABCD:DEF:GHIJ:KLM";
StringTokenizer stok = new StringTokenizer(tst, ":");
while(stok.countTokens() > 1) {
    ht.put(stok.nextElement(), stok.nextElement());

Similar Messages

  • Interested in Learning Java Programming.

    Hello All,
    I a college student and I am interested in learning Java programming. Where would be a good place for me to start?
    Thanks

    Sun's basic Java tutorial
    Sun's New To Java Center. Includes an overview of what Java is, instructions for setting up Java, an intro to programming (that includes links to the above tutorial or to parts of it), quizzes, a list of resources, and info on certification and courses.
    http://javaalmanac.com. A couple dozen code examples that supplement The Java Developers Almanac.
    jGuru. A general Java resource site. Includes FAQs, forums, courses, more.
    JavaRanch. To quote the tagline on their homepage: "a friendly place for Java greenhorns." FAQs, forums (moderated, I believe), sample code, all kinds of goodies for newbies. From what I've heard, they live up to the "friendly" claim.
    Bruce Eckel's Thinking in Java (Available online.)
    Joshua Bloch's Effective Java
    Bert Bates and Kathy Sierra's Head First Java.

  • Tolerance Days in Interest Program

    We are having issues where interest is being calculated on items from prior months and years.  We're not sure why interest was not picked up in the period it should have. I noticed that on 10/24/05 config was changed for tolerance days from 37 days to 30.  We started experiencing these issues with the 10/31/05 interst run.  Would changing the tolernace days make the interest program go back to prior periods and years? thanks

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Iterator;
    import java.util.LinkedHashMap;
    import java.util.Map;
    import java.util.Set;
    * @author RG59153
    public class DayInYear {
    /** Creates a new instance of DayInYear */
    public DayInYear() {
    public static void main(String args[])
    int dat=0,year=0;
    String mon=null;
    BufferedReader br=null;
    LinkedHashMap hm=new LinkedHashMap();
    hm.put("january","31");
    hm.put("february","29");
    hm.put("march","31");
    hm.put("april","30");
    hm.put("may","31");
    hm.put("june","30");
    hm.put("july","31");
    hm.put("august","31");
    hm.put("september","30");
    hm.put("october","31");
    hm.put("november","30");
    hm.put("december","31");
    try {
    br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the Month in words:");
    mon=br.readLine();
    if(!hm.containsKey(mon.toLowerCase()))
    System.out.println("Enter the Month Name Correctly");
    return;
    System.out.println("Enter the Date:");
    dat=Integer.parseInt(br.readLine());
    System.out.println("Enter the Year:");
    year=Integer.parseInt(br.readLine());
    if(year%4!=0)
    hm.put("february","28");
    Set s=hm.keySet();
    Iterator iter=s.iterator();
    int finaloutput=0;
    while(iter.hasNext())
    String mon_name=(String)iter.next();
    if(mon_name.equalsIgnoreCase(mon))
    int noofdays=Integer.parseInt((String)hm.get(mon_name));
    if(noofdays<dat)
    System.out.println("No Such Date Found in the month.");
    return;
    finaloutput+=dat;
    break;
    else
    int noofdays=Integer.parseInt((String)hm.get(mon_name));
    finaloutput+=noofdays;
    System.out.println("The Day in the year is:"+finaloutput);
    } catch (NumberFormatException ex) {
    System.out.println("Invalid Input, Enter valid Number");
    } catch (IOException ex) {
    ex.printStackTrace();
    finally
    try {
    if(br!=null)
    br.close();
    } catch (IOException ex) {
    ex.printStackTrace();

  • How to program in java

    hey guys. i am an undergraduate student. graduating next spring 2007. i did all my programming languages which were required in my course in 2004 and since that i have not taken any programming classes. i got my internship as a database administrator so got into MSaccess and Sql and VbA scripting. since my graduation year is near i want to be strong in database and programming and i was just reading about the employers what kind of programming they prefer and most of the times out of 100 you can say 80% needs java. so i have realized if i have my skills good in java and database job wont be any problem. i would like you to tell me about books i can buy from amazon.com or resources on internet from which i can understand the concept of programming again and then once my concept is clear about all the data types and functions i can start learning java. i would like you to tell me good java books also and some advance. the only book i have is the second edition of Java how to program. Thanks alot for all your help.

    The perverbial List:
    Free Tutorials and Such
    Installation Notes - JDK 5.0 Microsoft Windows (32-bit)
    Your First Cup of Java
    The Java� Tutorial - A practical guide for programmers
    New to Java Center
    Java Programming Notes - Fred Swartz
    How To Think Like A Computer Scientist
    Introduction to Computer science using Java
    The Java Developers Almanac 1.4
    Object-Oriented Programming Concepts
    Object-oriented language basics
    Don't Fear the OOP
    Free Java Books
    Thinking in Java, by Bruce Eckel (Free online)
    Core Servlet Programming, by Merty Hall (Free Online)
    More Servlets, by Marty Hall (Free Online)
    A Java GUI Programmer's Primer
    Data Structures and Algorithms
    with Object-Oriented Design Patterns in Java, by Bruno R. Preiss
    Introduction to Programming Using Java, by David J. Eck
    Advanced Programming for the Java 2 Platform
    The Java Language Specification
    Books:
    Head First Java, by Bert Bates and Kathy Sierra
    Core Java, by Cay Horstmann and Gary Cornell
    Effective Java, by Joshua Bloch
    Enjoy
    JJ

  • Compression program in java

    hello all,
    any idea how to write a file compression program in java.
    any help would be appreciated.
    thanx

    The same way you would write a compression program in any other language.
    Open a stream for the file to be compressed
    Open a stream for the compressed file, add a filter-stream (according to your choice of algorithm) to the stream for the compressed file.
    Read from one stream, write to the other.
    For uncompressing, reverse the above. :)
    With java you have some small addenums that are useful, like the java.util.zip classes if it is basic zip-functionality that you want.
    Once all this is done, as sugar on the top, fix a nice gui involving JTable, JTree and other eminent swing-components of your choice.
    A fair bit of warning, if you want to use the JFileChooser for your logical file-view inside a compressed file, you are in for a ride. You will have to make your own FileView and File object. The java.io.File object is badly designed and it will be a long hassle to make it work, but it can be done.
    Yours sincerely
    Peter Norell

  • XMP Embedder program in java

    Hello everyone,
    I would like to introduce myself to the form. My name is Tuli and I am student at Colorado School of Mines. We are working with a team on a XMP embedded program. The program in short will:
    - Allow you to create schema and save them
    - Allow you to fill in the schema and create templates and save the templates
    - Batch processing of embedding templates (XMP data) into JPEG, GIF, TIFF and PNG
    -The program is going to be designed to be compatible with Photoshop
    This program as well as the source code will be freely available for download.
    We are writing the program in Java to work on windows 2000, XP, and OSX platforms. Any help on this project would be greatly appreciated as we develop it.
    Thanks,
    Tuli
    Colorado School of Mines

    Hi Tuli
    Iam introducing my self is a small web developer and rearsearchig on XMp i just join in forum and interesting ly i have seen many of friends there to help each other and this is very happy place to me to do my research my work and iam intesrested in checking tulis work and can you let me know where from i can doload sample application and some code , i would appreciate if you could help me in this regard.
    anchary

  • When i click on the Firefox icon I get an error message, C:\Program Files\Java\jre6\lib\deploy\jqs\ff\..\..\..\..\bin\jqsnotify\.exe cannot be found. What is it? and how do i fix it? in English

    When i click on the icon to open Firefox an error message box appears with the following message, C:\Program Files\Java\jre6\lib\deploy\jqs\ff\..\..\..\..\bin\jqsnotify\.exe cannot be found. It then advises me to use the Search function, but the search function says it is unavailable.
    What is it? and how do I fix it? in English

    See:
    http://www.java.com/en/download/help/quickstarter.xml - What is Java Quick Starter (JQS)? What is the benefit of running JQS? - 6.0

  • How to execute external program in java?

    My question is how to execute an external program in java.
    I need to call a unix command in java.
    Thanks.

    it depends on what you are trying to do. Following are the two methods
    1. Runtime.exec() : this method allows you just to call an external program as a seperate process
    2. JNI (Native Interface) :- As of right now only C and C++ are supported by this method. This method allows you to directly call the C/C++ methods from JAVA

  • How Can I execute a java program using java code?

    {color:#000000}Hello,
    i am in great trouble someone please help me. i want to execute java program through java code i have compiled the java class using Compiler API now i want to execute this Class file through java code please help me, thanks in advance
    {color}

    Thanks Manko.
    i think my question was not clear enough.. actually i want to run this class using some java code . like any IDE would do i am making a text editor for java, as my term project i have been able to complie the code usign compiler api and it genertaes the class file but now i want to run this class file "THROUGH JAVA CODE" instead of using Java command. I want to achive it programatically. do you have any idea about it.. thanks in advance

  • How to create a  schedule program in java

    Hello Friends ,
    Can any one provide me with an example how to create a schedule program using java.util.timer etc . I am in need of a program which should run as a schedule job that searches for a file in a directory and read the latest files from that directory in every minute of time as a schedule job .
    Thanks
    mahesh

    I don't feel like writing my own example, but google will be happy to provide you with an example.

  • New "Windows Programming Using Java" Website

    Our newly created "Windows Programming Using Java" website (http://fivedots.coe.psu.ac.th/~ad/winJava/) is for programmers who want to extend Java's capabilities on Windows XP and/or Vista, but aren't sure where to start. One of the drawbacks of Java's portability is that many Java programmers have a rather sketchy knowledge of Windows-specific programming.
    We plan to explain how Java applications can utilize Windows application software, OS features, and hardware beyond the reach of Java's standard libraries. A variety of Java/Windows programming techniques will be explained, including:
    * Java's employment of the Win32 API via C, JNI, and J/Invoke.
    * Java's utilization of Window's Command Line Interface (CLI) and batch files, accessed through Java's Runtime, ProcessBuilder, and Process classes.
    * Java and Windows object-based scripting, centered around the use of VBScript, Windows Script Host (WSH), and Windows Management Instrumentation (WMI).
    * Java interoperability with COM, including hosting of ActiveX controls in Swing containers using jacoZoom.
    This website is a work in progress, with four chapters available for download at the moment. We'll be adding more regularly, and would love feedback on what we're doing.
    Thanks,
    Gayathri Singh and Andrew Davison
    [email protected] and [email protected]
    Edited by: AndrewDavison on Jun 20, 2008 1:36 AM

    Hi, I am looking the similar kind of requirement that you had done.. Do you have any more details on the code that is working for you . All i need is if the user logs in successfully in windows, the the app should be accessible. Please let me know if you have the code with you on this problem.
    Thanks in advance
    "what i meant is the we b applications running in my system, when i access the application iam able to to login automatically with windows authentication and now in case of other users following process happened:"

  • Asynchronous socket programming in Java?

    I am a C++ programmer, I just wonder how do I do asynchronous programming in Java?
    In VC++, you can hook an event handler to the socket and the system will fire an event whenever there is data coming in to the socket. The event handler can then spawn a thread and handle the network message.
    In Unix, you can use the select() function. This function will return whenever the indicated socket fires events. If nothing happens within the timeout period, the function simply returns.
    What about in Java? I don't see any methods that allows you to hook an event handler to it. Is there anything like select() function in Unix?
    Thanks so much.

    There is something very much like C's select: selectors. I don't immediately know where to read more about them but google for things like "java selector example", "java nio tutorial", etc (NIO = new I/O, the library where selectors live.)
    Another way to do async sockets in Java is by threading. That is the traditional way before New I/O. It's still a perfectly good and easy way, thanks to Java's easy threading. Downside is that it scales to only maybe 100-200 simultaneous connections per server (will depend on your operating system). For thread-based sockets check Sun's Java socket tutorial (first item when you google "java socket tutorial").

  • How Hard is it to program in Java.Swing

    Hi
    I wanted to program using java.swing component but i don't have any idea where i should start.
    I have been programming in java using java.Awt
    and i can do simple array programing.
    So how hard is it, and Where should i start learning java.swing. Ps I don't know anything about Swing, I don't even know what it is.
    Thanks you

    Hi
    As the previous guy said it is quite easy, and that link should teach you the basics. What I want to remind you of is that Swing uses up more memory when run. But it also have a few nicer extra goodies that make coding a little bit easier. Have fun with it.
    Here are a few more sites
    <http://manning.spindoczine.com/sbe/>
    <http://java.sun.com/docs/books/tutorial/uiswing/>
    Ciao

  • Can i create an exetutable (.exe) program with java???????

    I would like to learn how i can create an exetutable (.exe) program with java.
    Is there any additional package which i should download or buy it?
    Also i would like to write files into CD's direct from my application.
    Is there any library which i must download or buy?
    Thanks!

    check out the free open source GNU gcj native compiler for Java to see if it will do what you want.
    http://gcc.gnu.org/java/
    for writing data files you could use java.io.BufferedWriter. For sound files, look in the javax.sound package

  • C:\Program Files\Java\jre7\bin

    I have java installed on my sytem.but when i try to run sql developer it says j2se sdk not found in the path C:\Program Files\Java\jre7\bin
    C:\Windows\system32>java -version
    java version "1.7.0"
    Java(TM) SE Runtime Environment (build 1.7.0-b147)
    Java HotSpot(TM) 64-Bit Server VM (build 21.0-b17, mixed mode)
    (code}
    how should i get the path of j2se sdk                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    >
    java version "1.7.0"
    >
    Don't use Java 1.7 with sql developer. There have been many problem reports (search the forum) and it is recommended that you use no later than a 1.6 Java version.

Maybe you are looking for

  • Ipod is not recognized by computer or itunes and does not charge

    Hi, so for a few months I've been having a problem with my iPod Touch 4th gen where when i connect the ipod to the computer it does not connect. My Computer says "USB device not recognized" and also I've recognized that my ipod doesnt charge either.

  • How to Use SQL Query having IN Clause With DB Adapter

    Hi, I am using 11.1.1.5 want to find out how to Use SQL Query having IN Clause With DB Adapter. I want to pass the IN values dynamically. Any ideas. Thanks

  • MacBook Pro Application Sigsev ERRORS! How to stop?

    AHHH i get about 5 errors every day from random applications about SIGSEV and SIGBUS here are some pictures: http://img216.imageshack.us/img216/3954/picture2nxw.png http://img216.imageshack.us/img216/1746/picture5p.png They quit out of the applicatio

  • PO seriel number as per plant wise & Document type wise

    Hi, Purchase order seriel numbers should be based on plant wise & Document type wise Example: Plant    Document Type              Seriel number 1000       NB                                4500000000 -  4599999999 1000       UB                       

  • Accessing Forte from Voice Response System

    We are looking at writing a Forte application that is accessible from a telephone voice response system. Has anyone tried this? If so, how do you set up the interface between our Forte server (running on AIX) and the voice response system. The voice