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 6 of 6

Thread: simple program for reading text file

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default simple program for reading text file

    please if anyone could help me with the following code that would be greatly appreciated

    the code come more or less from a tutorial book

    1.
    import java.io.*;

    public class testRead{

    public static void main (String []args){
    File f = new File("test.txt");
    BufferedReader in = new BufferedReader(new FileReader(f));


    Movie movie = readMovie(in);

    public Movie readMovie(BufferedReader in)
    {
    String line = "";
    String [] data;
    String string1,string2,string3;



    line = in.readLine();

    if (line !=null)
    {
    data = line.split("\t");
    string1 =data[0];
    string2=data[1];
    string3 =data[2];
    return new Movie(string1,string2,string3);
    }
    else
    {
    return null;
    }
    }

    }

    }

    2.


    public class Movie{

    public String string1, string2, string3;

    public Movie(String string1, String string2, String string3)
    {
    this.string1 = string1;
    this.string2 = string2;
    this.string3 = string3;

    }

    }

    3. a text file called test.txt with the following data


    hello 1 29
    john 2 30


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: simple program for reading text file

    What exactly is your question? Is there something you don't understand? If so, what?

    Is the program giving an error? If it is, could you post it here?

    Also, could you use highlight tags for your code please?
    They work like this:

    [highlight=Java]
    your code here
    [/highlight]


    Also, my first impression is that you shouldn't be using...

    line.split("\t");

    ...because 1) I'm not sure if "/t" is a value stored by .txt files, and 2) I don't know if I see any spacings equivalent of "/t" in the file you presented...

    hello 1 29
    john 2 30

    Perhaps instead you could use...

    line.split(" ");

    ...so it splits the lline at space characters.
    Last edited by snowguy13; December 12th, 2011 at 05:47 PM.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: simple program for reading text file

    i am getting the following errors:


    testRead.java12:error:illegal start of expression
    public Movie readMove(BufferedReader in)
    ^

    testRead.java12:error:";" expected
    public Movie readMove(BufferedReader in)
    ^
    testRead.java12:error:";" expected
    public Movie readMove(BufferedReader in)
    ^

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: simple program for reading text file

    sorry, in what i just posted i tried to get the ^ symbol under the 'p' in public first time, then under '(', then under ')'

  5. #5
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: simple program for reading text file

    Oh! I see your problem! You have the...

    readMovie(BufferedReader in)

    ...method defined inside your main method. So, I think you can get rid of these errors my moving the method outside of the main method.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  6. #6
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: simple program for reading text file

    thanks for help so far - forgot to come back and respond at the time

    i'm also having a couple of problems with the following
    (missing return type and unreachable statment):

    [highlight = Java]

    import java.util.Scanner;

    public class testMethod{
    static Scanner sc = new Scanner(System.in);
    public static void main (String []args){


    boolean continueOrNot = true;
    do

    {
    playGame();
    }
    while (continueOrNot);
    }

    public static void playGame()
    {
    int number = (int)(Math.random()*10)+1;
    int guess = getGuess();

    }

    public static int getGuess()
    {
    boolean validInput=true;
    while (!validInput)
    {
    int guess = sc.nextInt();
    if ((guess <1) || (guess >10))
    {
    System.out.print("Wrong");
    validInput = false;
    }
    else
    {
    return guess;
    validInput = true;
    }
    }//end of while

    }//end of getGuess method



    }//end of class

    [/highlight]

Similar Threads

  1. Reading data from a text file into an object
    By surfbumb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2011, 08:37 PM
  2. Reading from Text File and Processing Information
    By royalslim in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 13th, 2010, 03:27 PM
  3. reading string in from text file
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: December 3rd, 2010, 05:31 PM
  4. Reading a lines from text file
    By kiran in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 26th, 2010, 10:54 PM
  5. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM