Results 1 to 6 of 6
  1. #1
    Java26 is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Question Is this possible?

    Hey, I have been working with java for a few weeks now and am trying to expand on some tutorials i have been learning from. I was wondering though. I have just learned how to read data from a text file using the Scanner utility. I was trying to have it read a random line (just one) from the file and display it on the screen. My idea was to generate a random number and then have the Scanner read that line number from the file. Is this possible. All i am looking for is a little advice but examples are great too. Thanks!

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Not really. You could read the file line by line but do nothing until you are reach the line which you want, print this line and stop reading.

    Or if your lines all have the same length, you could maybe use the RandomAccessFile

  3. #3
    Java26 is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default

    Reading all the lines and not doing anything like you mentioned would work as long as it only reads one line. Not all of the lines are the same length.

    How would you read the file but only print out the line number that the random number generator outputs?

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Quote Originally Posted by Java26 View Post
    How would you read the file but only print out the line number that the random number generator outputs?
    Java Code:
    		Scanner sc = new Scanner(.....);
    		int line = new Random().nextInt(....) + 1;
    		int currentline = 1;
    		while (sc.hasNextLine() && currentline < line) {
    			sc.nextLine();
    			currentline++;
    		}
    		if(currentline==line && sc.hasNextLine()){
    			System.out.println(sc.nextLine());
    		}else{
    			System.out.println("line number does not exist");
    		}
    :p

  5. #5
    Java26 is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default

    ok that makes sense. One more question. what goes in the "(....)" parts? And where so i tell the program what the file name is? Thanks a lot for your help!

  6. #6
    Java26 is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default Got it!

    thanks for all your help! I had to tweak it a little but your input was a huge in helping me so Thanks!

    I copied the code below:

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    import java.util.Random;

    public class main {
    public static void main(String[] args)
    throws FileNotFoundException {
    File file = new File("data.txt");
    Scanner sc = new Scanner(file);
    Random rng = new Random();
    int v1 = rng.nextInt(30)+1;
    int cline = 1;

    while (sc.hasNextLine() && cline < v1) {
    sc.nextLine();
    cline++;
    }
    if(cline==v1 && sc.hasNextLine()){
    System.out.println(sc.nextLine());
    }else{
    System.out.println("line number does not exist.");
    }
    }
    }

Tags for this Thread

Posting Permissions

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