Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Program to read .TXT file one line at a time

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Program to read .TXT file one line at a time

    Hi! I'm fairly new to Java, or even programming in general. I'm using Netbeans to help me find errors and everything, but when I try to run/debug my program, It says there is something wrong with my 'while' loop. Can I have some pointers?

    Main.java
    class Main{
     
        public static void main(String[] args){
     
            filereader fr = new filereader();
            fr.openFile();
            fr.readFile();
            fr.closeFile();
     
        }
     
    }

    filereader.java
    import java.io.*;
    import java.util.*;
     
    public class filereader{
     
    private Scanner scan;
     
    public void openFile(){
     
        File codes = new File("codes.txt");
     
        try{
            scan = new Scanner(codes);
        }
        catch(Exception e){
            System.out.println("Couldn't find file.");
        }
    }
     
    public void readFile(){
        while(scan.hasNext()){
            String num = scan.next();
            String code = scan.next();
     
            System.out.println(num + code);
            System.out.println();
            System.out.println("Press Enter to continue...");
     
            Scanner input = new Scanner(System.in);
            input.nextLine();
        }
    }
     
    public void closeFile(){
     
        scan.close();
     
    }
     
    }

    codes.txt
    1.		3RXPM3
    2.		26MYKD
    3.		5GATBA
    4.		PMF5R3
    5.		3SJUPQ
    6.		NBR5JT
    7.		PUR3VR
    8.		AJJCF2
    9.		NCM26A
    10.		MT2Y5G
    11.		FG43PK
    12.		GMBH87
    13.		T9HMHN
    14.		RCYMKW
    15.		36E9FU
    16.		FBRKJ2
    17.		YAPG32
    18.		MMR2X4
    19.		A7NAR9
    20.		N62VRX
    21.		NV4A65
    22.		KGFDT2
    23.		RFWMVE
    24.		F465F9
    25.		7KPU67
    26.		2E8V7U
    27.		CJWBD7
    28.		49E5P8
    29.		WUEUAU
    30.		TN45G2
    31.		5D7FY8
    32.		T9JT8V
    33.		Y4JEPT
    34.		84GYTY
    35.		265B3X
    36.		JA6G58
    37.		4YWTBU
    38.		Y8US2D
    39.		7794YU
    40.		TWDP3R
    41.		H7VDPY
    42.		KBJN38
    43.		QPT4Q
    44.		EKT6JJ
    45.		YWTBUC
    46.		TERKP9
    47.		HUDG6T
    48.		4CCS9S
    49.		UN4SRA
    50.		UQXSST
    51.		BPUK82
    52.		88D544
    53.		6C2T7S
    54.		T4GTGJ
    55.		HJ7PR5
    56.		7BCDV9
    57.		XRSVRA
    58.		P973MD
    59.		S5AYPJ
    60.		JC4STS
    61.		44MA2X
    62.		UY4QDJ
    63.		44DMRR
    64.		Q6AD5N
    65.		H3UGEG
    66.		F4VHWU
    67.		4689TU
    68.		JR8EHW
    69.		FVGAJP
    70.		86KF9N
    71.		5KA8FM
    72.		U6B37E
    73.		9W8PEQ
    74.		CV949U
    75.		BX7AXS
    76.		8JVG2F
    77.		2HAJ9X
    78.		3AS3RP
    79.		XWEQ4Q
    80.		GPN77T
    81.		RD36SS
    82.		U2FE5W
    83.		QT5SQJ
    84.		VUDJT7
    85.		28KKJN
    86.		72H3MB
    87.		9JY5CX
    88.		KM6QFC
    89.		PTVSYU
    90.		9VGC9W
    91.		MRR7PM
    92.		VVH88A
    93.		YNQW5R
    94.		PMGCN8
    95.		X3YTDW
    96.		S22N2J
    97.		A37W4N
    98.		TDJQFH
    99.		VA7TAS
    100.		7TE866


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Program to read .TXT file one line at a time

    It says there is something wrong with my 'while' loop
    Is the error from the compiler?
    Please copy full text of error message and paste it here. Here is a sample:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    Or does the error happen when you try to execute the code? Again, copy and paste here the full text of the error message.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java program to read last line of a file
    By JavaPF in forum File Input/Output Tutorials
    Replies: 2
    Last Post: September 10th, 2009, 02:26 AM
  2. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  3. Reading a file line by line using the Scanner class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  4. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM