Welcome to Dream.In.Code
Become a Java Expert!

Join 149,510 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,372 people online right now. Registration is fast and FREE... Join Now!




noSuchElement exception

 
Reply to this topicStart new topic

noSuchElement exception

futura91
16 Aug, 2007 - 11:06 PM
Post #1

New D.I.C Head
*

Joined: 27 Jul, 2007
Posts: 12


My Contributions
i've got another assignment and it goes like this:

Bill is trying to compactly represent sequences of capital alphabetic characters from ‘A’ to ‘Z’ by folding repeating subsequences inside them. For example, one way to represent a sequence ‘AAAAAAAAAABABABCCD’ is ‘10(A)2(BA)B2©D’. He formally defines folded sequences of characters along with the unfolding transformation for them in the following way:
o A sequence that contains a single character from ‘A’ to ‘Z’ is considered to be a folded sequence. Unfolding of this sequence produces the same sequence of a single character itself.
o If S and Q are folded sequences, then SQ is also a folded sequence. If S unfolds to S’ and Q unfolds to Q’, then SQ unfolds to S’Q’.
o If S is a folded sequence, then X(S) is also a folded sequence, where X is a decimal representation of an integer number greater than 1. If S unfolds to S’, then X(S) unfolds to S’ repeated X times.
According to this definition it is easy to unfold any given folded sequence. However, Bill is much more interested in the reverse transformation. He wants to fold the given sequence in such a way that the resulting folded sequence contains the least possible number of characters.
Input
Input file contains several test cases, one per line. Each of them contains a single line of characters from ‘A’ to ‘Z’ with at least 1 and at most 100 characters.
Output
For each input case write a different output line. This must be a single line that contains the shortest possible folded sequence that unfolds to the sequence that is given in the input file. If there are many such sequences then write any one of them.

Sample Input
AAAAAAAAAABABABCCD
NEERCYESYESYESNEERCYESYESYES
Sample Output
9(A)3(AB)CCD
2(NEERC3(YES))

here is my code:

CODE
import java.io.*;
import java.util.*;

public class MP3{
    public static void main(String[] args)
        throws IOException{
            String fileName = "e:\\inMP3.txt";
            String str="";
            FileReader fr = new FileReader(fileName);
            BufferedReader br = new BufferedReader(fr);
            Boolean repeat = true;
            StringTokenizer st;
            String a, b;
            int x;
            System.out.println( "Output:" );
            while(repeat) {
                str = br.readLine();
                if(str==null) {
                    repeat=false;
                    break;
                }
                else{
                    st = new StringTokenizer(str);
                    x = st.countTokens();
                    a = st.nextToken();
                    b = st.nextToken();
                    if(a.equals(b)){
                                                for(int i=0; i<x; i++){
                                    int count=0;
                                count++;
                                System.out.print(count+"("+a+")");
                                                }
                    }
                    else{
                        break;
                    }
                }
            }
            fr.close();
        }
}


please check it. it throws a noSuchElement exception when i execute it. and i don't think it would count a repeating group of characters.

again, i'm sorry for my terrible code... but please, i need help.

This post has been edited by jayman9: 17 Aug, 2007 - 02:10 AM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: NoSuchElement Exception
17 Aug, 2007 - 12:25 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well first off, the problem you have here is related to these lines...

CODE

st = new StringTokenizer(str);
x = st.countTokens();
a = st.nextToken();
b = st.nextToken();


To start it is recommend that you try and stay away from StringTokenizer() because it is deprecated in newer implementations. If you need this functionality, use the split() method. Since you are using it here, might as well show you what is going on.

First off the StringTokenizer() object will split the string into segments or "tokens". Without specifying a delimiter (that is a character which separates the chunks and where the tokenizer knows where to break the string at) it assumes the space character (character 32).

As you can imagine, each line you are reading doesn't have a space so StringTokenizer is returning 1 token, the original string. So "x" is going to equal 1. This is also the reason why the problem isn't showing up on the line where you set "a" because it is return the first (and only) token in that line. Now when you call the nextToken() again to set your variable "b" there are no more tokens left, which throws your error.

The idea is that once you tokenize a string, you should always throw it into a loop where you can check the method "hasTokens()" and determine if there is another token available. That way you are never using nextToken without there being one.

I had mentioned earlier that you should start using split(). This would be a benefit to you because you can actually supply the split() method with a regular expression (used for pattern matching) which would allow you to match your multiple characters and group the strings into the proper counts. Be sure to check it out.

Java 2 Platform pages - String page (check out split method towards the bottom.. 2 versions)

This will solve your problem. Enjoy!

This post has been edited by Martyr2: 17 Aug, 2007 - 12:28 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 07:33PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month