Exception in thread "main" java.util.NoSuchElementException


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Exception in thread "main" java.util.NoSuchElementException

Hybrid View

  1. #1
    Join Date
    Mar 2007
    Posts
    3

    Exception in thread "main" java.util.NoSuchElementException

    I am reading a file "encodings.txt" into a two dimensional array, this file contains url encodings, it works perfectly fine down the half of the fiel but then i get a weired kind of exception i.e."Exception in thread "main" java.util.NoSuchElementException", i personally dont think that this is coz of tokenizer running out of tockens, since a new token is createn each time loop traverses. I someone knows the problem plz let me know.

    the sample code and the encodings.txt (see attachment) file are given

    package fielreader;
    import java.io.*;
    import java.util.*;

    public class Main {
    public static void main(String[] args) throws IOException {
    ArrayList line = new ArrayList();
    String tempStr = null;
    BufferedReader br=null;
    try
    {
    br = new BufferedReader(new FileReader("C:/WebRoot/encodings.txt"));
    while ((tempStr = br.readLine()) != null)
    line.add(tempStr);
    }
    catch (FileNotFoundException ex) {
    ex.printStackTrace();
    }

    String [][] map = new String[line.size()][2];

    for (int i = 0; i < map.length; i++)
    {
    tempStr = (String) line.get(i);
    StringTokenizer st = new StringTokenizer(tempStr);
    while (st.hasMoreTokens()){
    map[i][0]=st.nextToken();
    map[i][1]=st.nextToken();
    }
    System.out.println(map[i][0]+" " + " "+map[i][1]);
    }//End Of While Loop
    }

    }
    Attached Files Attached Files

  2. #2
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173

    Try this one

    Code:
    import java.io.*;
    import java.util.*;
    
    public class Main {
    public static void main(String[] args) throws IOException {
    ArrayList line = new ArrayList();
    String tempStr = null; 
    BufferedReader br=null;
    try
    {
    br = new BufferedReader(new FileReader("C:/WebRoot/encodings.txt")); 
    while ((tempStr = br.readLine()) != null)
    line.add(tempStr);
    }
    catch (FileNotFoundException ex) {
    ex.printStackTrace();
    }
    
    String [][] map = new String[line.size()][2];
    
    for (int i = 0; i < map.length; i++) 
    {
    tempStr = (String) line.get(i);
    StringTokenizer st = new StringTokenizer(tempStr);
    while (st.hasMoreTokens()){
    map[i][0]=st.nextToken();
    //map[i][1]=st.nextToken();
    }
    System.out.println(map[i][0]+" " + " "+map[i][1]);
    }//End Of While Loop
    }
    
    }

  3. #3
    Join Date
    Mar 2007
    Posts
    3
    Although this does work without any weired exception but this is not actually what i am trying to achieve what i wana do is to get the character in the first index of array and its corresponding encoding in the next index of same array and so on, just like as its given in the file but in my case i want to make it into an 2D array.
    just like this

    map[i][0] map[i][1]

    ! %21
    " %22
    # %23
    $ %24
    % %25
    & %26
    ' %27
    ( %28
    ) %29
    * %2A
    + %2B
    , %2C

    I hope u understand what i mean,

  4. #4
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173

    Modify your file

    Please try to use this file instead of yours.
    I have deleted two lines after %AB
    Now it works for me.
    You have to use your old java file.
    Good luck,
    Ahmad

    backspace %08
    tab %09
    linefeed %0A
    creturn %0D
    space %20
    ! %21
    " %22
    # %23
    $ %24
    % %25
    & %26
    ' %27
    ( %28
    ) %29
    * %2A
    + %2B
    , %2C
    - %2D
    . %2E
    / %2F
    0 %30
    1 %31
    2 %32
    3 %33
    4 %34
    5 %35
    6 %36
    7 %37
    8 %38
    9 %39
    : %3A
    ; %3B
    < %3C
    = %3D
    > %3E
    ? %3F
    @ %40
    A %41
    B %42
    C %43
    D %44
    E %45
    F %46
    G %47
    H %48
    I %49
    J %4A
    K %4B
    L %4C
    M %4D
    N %4E
    O %4F
    P %50
    Q %51
    R %52
    S %53
    T %54
    U %55
    V %56
    W %57
    X %58
    Y %59
    Z %5A
    [ %5B
    \ %5C
    ] %5D
    ^ %5E
    _ %5F
    ` %60
    a %61
    b %62
    c %63
    d %64
    e %65
    f %66
    g %67
    h %68
    i %69
    j %6A
    k %6B
    l %6C
    m %6D
    n %6E
    o %6F
    p %70
    q %71
    r %72
    s %73
    t %74
    u %75
    v %76
    w %77
    x %78
    y %79
    z %7A
    { %7B
    | %7C
    } %7D
    ~ %7E
    %A2
    %A3
    %A5
    | %A6
    %A7
    %AB
    %B0
    %B1
    %B2
    , %B4
    %B5
    %BB
    %BC
    %BD
    %BF
    %C0
    %C1
    %C2
    %C3
    %C4
    %C5
    %C6
    %C7
    %C8
    %C9
    %CA
    %CB
    %CC
    %CD
    %CE
    %CF
    %D0
    %D1
    %D2
    %D3
    %D4
    %D5
    %D6
    %D8
    %D9
    %DA
    %DB
    %DC
    %DD
    %DE
    %DF
    %E0
    %E1
    %E2
    %E3
    %E4
    %E5
    %E6
    %E7
    %E8
    %E9
    %EA
    %EB
    %EC
    %ED
    %EE
    %EF
    %F0
    %F1
    %F2
    %F3
    %F4
    %F5
    %F6
    %F7
    %F8
    %F9
    %FA
    %FB
    %FC
    %FD
    %FE
    %FF

  5. #5
    Join Date
    Mar 2007
    Posts
    3

    Problem solved

    thanx buddy it really worked but i had to delete just one line %AC this had no character in place so doing problems. once again thanx

    ragards
    Waqar

Similar Threads

  1. Replies: 4
    Last Post: 12-29-2008, 08:25 AM
  2. Exception in thread "main"
    By SeanB in forum Java
    Replies: 3
    Last Post: 04-02-2007, 05:51 PM
  3. Getting a thread to throw an exception
    By jjamesis in forum Java
    Replies: 4
    Last Post: 04-02-2007, 05:37 PM
  4. Replies: 7
    Last Post: 04-02-2007, 05:35 PM
  5. Exception in thread "main"
    By Sean in forum Java
    Replies: 0
    Last Post: 03-29-2002, 07:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center