Results 1 to 7 of 7
  1. #1
    Asmicor is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Question Reading from text file errors ?!?!?!?!?!? Need Help

    I'm making a program in Visual Basic, but I want to use Java to convert information from a text file so that I can just copy and paste. That I know how to do, or at least I'm busy trying to do it.

    My problem I'm having now is that I'm having a read error and I don't know why ??????

    This is my code......
    I just want to mention the "In" class is a custom made one that uses stuff to read data :3

    Java Code:
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    
    public class Reader {
    	
    	static String[] TLine = new String[1000];	
    	static int num = 0;
    
    	public static void main(String[] args) throws FileNotFoundException {
    		
    		SerialReader();
    		SerialPrinter();
    		
    	}
    	public static void SerialReader() {
    		
    		In KReader = new In("GameSerials.txt");			
    		while (!KReader.isEmpty() && num < 1000) {
    			TLine[num] = KReader.readLine();
    			num = num + 1;
    		}
    		
    	}
    	public static void SerialPrinter() throws FileNotFoundException {
    		
    		PrintWriter save = new PrintWriter("VB_KeygenData.txt");
    		int count = 0;
    		int GCase = 0;
    		while (count < num) {
    			GCase = 0;
    			if (TLine[count] == "|-|") {
    				count = count + 1;
    				save.println("If ComboBox1.Text = " + TLine[count] +" Then");
    				save.println("Key = Int(Rnd() * 1)");
    				save.println("Select Case Key");
    			}
    			count = count + 1;
    			while (TLine[count] != "|-|") {
    				save.println("	Case " + GCase);
    				save.println("		TextBox1.Text = " + " \"\"  " + TLine[count] + " \"\" ");
    				count = count + 1;
    				GCase = GCase + 1;
    			}
    			save.println("End Select");
    			save.println("End If");
    		}
    		save.close();
    	}
    }
    So I'm getting this error

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1000
    at Reader.SerialPrinter(Reader.java:38)
    at Reader.main(Reader.java:12)

    And this is how the text document looks that it should read from...
    It is just a test file for now, but basic structure is there

    -------------------------------------------------------------

    |-|
    C&C 3
    1487-1544-812
    4545-5-151-55-46-4
    5-4-854-64-64-68-4
    5-45-415-456-485-4
    |-|
    C&C 4
    8485416-456-484
    5-4-4584-854-84-86-
    |-|

    ------------------------------------------------------------

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,936
    Rep Power
    4

    Default Re: Reading from text file errors ?!?!?!?!?!? Need Help

    In the SerialPrinter method you actually increment count, twice. The second time is just before the nested while loop. And that is where you are having problems.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Asmicor is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Question Re: Reading from text file errors ?!?!?!?!?!? Need Help

    jim829

    I just want to tell you that the increment of count twice there is on purpose. If I don't do that it wont work the way I want. But I still tested it without the second count and I still got the same error. I also thought that maybe my array was to big so I changed it to 10, but still the same problem.


    :Edit:
    OK I removed the extra count and just moved the one in the nested while up...

    Java Code:
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    
    public class Reader {
    	
    	static String[] TLine = new String[1000];	
    	static int num = 0;
    
    	public static void main(String[] args) throws FileNotFoundException {
    		
    		SerialReader();
    		SerialPrinter();
    		
    	}
    	public static void SerialReader() {
    		
    		In KReader = new In("GameSerials.txt");			
    		while (!KReader.isEmpty() && num < 1000) {
    			TLine[num] = KReader.readLine();
    			num = num + 1;
    		}
    		
    	}
    	public static void SerialPrinter() throws FileNotFoundException {
    		
    		PrintWriter save = new PrintWriter("VB_KeygenData.txt");
    		int count = 0;
    		int GCase = 0;
    		while (count < num) {
    			GCase = 0;
    			if (TLine[count] == "|-|") {
    				count = count + 1;
    				save.println("If ComboBox1.Text = " + TLine[count] +" Then");
    				save.println("Key = Int(Rnd() * 1)");
    				save.println("Select Case Key");
    			}
    			while (TLine[count] != "|-|") {
    				count = count + 1;
    				save.println("	Case " + GCase);
    				save.println("		TextBox1.Text = " + " \"\"  " + TLine[count] + " \"\" ");
    				GCase = GCase + 1;
    			}
    			save.println("End Select");
    			save.println("End If");
    		}
    		save.close();
    	}
    }
    I also would like to report that when I change...

    while (TLine[count] != "|-|") { to while (TLine[count] == "|-|") {

    the error seems to dissipear. But if I do this then my output text file will have the wrong imfo in it.
    Last edited by Asmicor; 06-26-2013 at 09:13 PM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Reading from text file errors ?!?!?!?!?!? Need Help

    Never use == or != to compare Strings or anyother objects. Use the /equals(...) method instead.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,936
    Rep Power
    4

    Default Re: Reading from text file errors ?!?!?!?!?!? Need Help

    Look at this code snippet.

    Java Code:
                while (TLine[count] != "|-|") {
                    count = count + 1;
                    save.println("  Case " + GCase);
                    save.println("      TextBox1.Text = " + " \"\"  " + TLine[count] + " \"\" ");
                    GCase = GCase + 1;
                }
    What happens if the count exceeds the array size before your condition is met? And as a side note (but very important one), don't use the relational operators to compare strings. Use equals or compareTo. Check the API for details.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    Asmicor is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default Re: Reading from text file errors ?!?!?!?!?!? Need Help

    OK, I used the equals method and it worked. I'm sorry if I seem dumb but I'm a newbie and I haven't work with strings before.
    So I ran the program and something very funny happened, look at the highlighted part, FILE SIZE. note it is a text document.
    Thank you for your help, but I think that I will be able to continue by myself for now.

    Reading from text file errors ?!?!?!?!?!? Need Help-untitled.png

    Is there a way to use a !equal or something??????
    Last edited by Asmicor; 06-26-2013 at 09:44 PM.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,936
    Rep Power
    4

    Default Re: Reading from text file errors ?!?!?!?!?!? Need Help

    Yep.
    Java Code:
    if (!str1.equals(str2)) {
      // do something
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Reading text file
    By FYou in forum New To Java
    Replies: 3
    Last Post: 02-25-2013, 10:45 AM
  2. Reading from text file
    By Zaadyn in forum New To Java
    Replies: 2
    Last Post: 02-02-2012, 10:00 PM
  3. Replies: 8
    Last Post: 07-17-2011, 02:38 PM
  4. Reading a text file
    By diegosened in forum New To Java
    Replies: 4
    Last Post: 01-15-2010, 11:32 PM
  5. Reading two text file and sum them up
    By matt_well in forum New To Java
    Replies: 36
    Last Post: 07-22-2008, 02:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •