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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 48

Thread: Java read in + print to a file

  1. #1
    Junior Member
    Join Date
    Sep 2018
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java read in + print to a file

    Need to read in from a file. That file is a list of information like a name then next line can be a number. I need to take that info a output it into a file. So here i have the code to get the two file names but im starting to do the reading of the input file and it looks very wrong. I need to do a loop i think, how can i loop this to make it read in both strings and ints(I know the order). I still need to output it into a formatted way in the outfile but this loop is confusing me.




    import java.io.FileReader;
    import java.util.Scanner;
     
    public class Standalonereport {
     
    	public static void main(String[] args) {
     
    		String fileName;
    		String outFile;
     
     
    		Scanner keyboardScanner = new Scanner(System.in);
     
    		System.out.printf("Enter the file name: ");
     
    		fileName = keyboardScanner.next();
     
    		System.out.printf("Enter the output file name: ");
     
    		outFile = keyboardScanner.next();
    		//WORKING UP TO THIS POINT
     
    		Scanner inputScanner;
    		inputScanner = new Scanner(new FileReader(fileName));
    		String s;
    		s = inputScanner.nextLine();
    Last edited by hoobahstank; September 8th, 2018 at 09:56 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java read in + print to a file

    im starting to do the reading of the input file and it looks very wrong.
    Please explain why it's wrong.
    What is in the the file?
    What will be done with the lines read from the file?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2018
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java read in + print to a file

    The file contains information from a vet office so it holds like a name then the next line is age and procedure, amount. Theres a whole list i have its about 10 different things. I need to read in those values on a file. Then with those values read in, i need to output them into a file and have them formatted nicely like an actual report. But for my code its wrong because its not a loop also im unsure on how i can read in values continually.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java read in + print to a file

    Is there more that one data item on a line
    or is each item on a separate line?

    What is the program going to do with the data as it reads it in?


    When will the program write data to the output file?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2018
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java read in + print to a file

    No each line has a single item of data. Either a string or int, it is a set format so i will know when it is a string and when it is a int. Also i know i need to have a scanner to read in the file names like i had. But then i need another scanner to read in from the input file. Also the output file needs to be made using printstream. So i thought i had to read in each line of code and then actually store them in my program since i need to use the ints. The program needs to write out the data once i have read the file and made the calculations i guess.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java read in + print to a file

    Is this what your are trying to do?
    The program would have a loop that does the following
    Reads in several lines of related data,
    does some computations with the data from those lines
    and writes out some line(s) in the report file
    end of loop
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Sep 2018
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java read in + print to a file

    That would work yes.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java read in + print to a file

    Ok, what have you tried? Post the code you are having problems with and ask some questions about the problems.
    Be sure to wrap the code in code tags.

    Look at the API documentation for the Scanner class to see what methods might be useful
    https://docs.oracle.com/javase/8/docs/api/index.html
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Sep 2018
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java read in + print to a file

    Right, i do not know how to structure the loop file. I have this:
    try
    			{
     
    			Scanner inputScanner = new Scanner(fileName);
     
    			while (inputScanner.hasNextLine()) 
     
    				{
    				String i = inputScanner.nextLine();
    				System.out.println(i);
    				}
    				inputScanner.close();
    			}
    Im doing this as an extra step kinda. I just want to make sure im reading in the file correctly first so this is what im having an issue with. It wants me to add a finally statement.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java read in + print to a file

    Ok, that's a start. Does the loop read and print all the lines in the file?

    Note: i is a poor name for a line of text. line would be a better name.

    The complete try statement needs a catch clause at the end.
    See the tutorial: https://docs.oracle.com/javase/tutor...tions/try.html
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Sep 2018
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java read in + print to a file

    I made a catch statement and it now runs. But the output is just one line and its the name of the file that i input.
    try
    			{
     
    			Scanner inputScanner = new Scanner(fileName);
     
    			while (inputScanner.hasNextLine()) 
     
    				{
    				String i = inputScanner.nextLine();
    				System.out.println(i);
    				}
    				inputScanner.close();
    			} catch (Exception e) {
    				System.out.println("Exception");
    			}

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java read in + print to a file

    Read the API doc for the Scanner class to see how to use its constructor to read a file.

    the output is just one line and its the name of the file
    One of the constructors takes a String that contains the data that will be read with its methods. That is the one you used.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Sep 2018
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java read in + print to a file

    Kinda confused, so i used a wrong kind of scanner?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java read in + print to a file

    You used the wrong constructor for the Scanner class. Look at the API doc for the Scanner class to see what constructor.
    There are some code examples in the API doc that show how to use the Scanner class to read from a file.

    As another example, this code allows long types to be assigned from entries in a file myNumbers:
     
     
          Scanner sc = new Scanner(new File("myNumbers"));
          while (sc.hasNextLong()) {
              long aLong = sc.nextLong();
          }
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Sep 2018
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java read in + print to a file

    OK so i switched it up and here is the code. But after i enter the file names it just ends for some reason.
    		try
    			{
     
    			Scanner inputScanner = new Scanner(new File(fileName));
     
    			while (inputScanner.hasNextLong()) 
     
    				{
    				long aLong = inputScanner.nextLong();
    				System.out.println(aLong);
    				}
     
    				inputScanner.close();
     
    			} catch (Exception e) {
    				System.out.println("Exception");
    			}

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java read in + print to a file

    Make sure the code in the catch block includes a call to the printStackTrace method:
    e.printStackTrace();


    Does the file have a long value as the first item to be read? If not the while loop will not run because it requires: hasNextLong() be true.
    Change the while loop back to what was there in post#11 so that it reads lines, not long values
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Sep 2018
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java read in + print to a file

    Nothing. Im startin to think maybe i dont have the file in the correct spot. The file has to be in the folder with all the other code right? I put it in the same folder but unsure if it has to go to a more specific place. By the way i am using eclipse workspace

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java read in + print to a file

    Nothing.
    What do you mean by that?
    You should get an exception message if the file is not found.


    If filename does not have the path to the file, the file needs to be in the current directory for when the java program executes.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Sep 2018
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java read in + print to a file

    File name has to be the path? I have just been inputting the name of the file which is in the same folder. Is that wrong?

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java read in + print to a file

    File name has to be the path?
    Not necessarily. If the file is in the current directory when the program is executed, then just the filename will work.
    If the file is somewhere else, then the path to file needs to be given to the program so it can find the file.

    You should get an exception message if the program does not find the file.
    If there is no exception message, then the program is finding the file and the problem probably is in the program.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Junior Member
    Join Date
    Sep 2018
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java read in + print to a file

    It made no difference. I went back to this code and it just outputs the filename back to me still.
    while (inputScanner.hasNextLine()) 
     
    			{
    			String i = inputScanner.nextLine();
    			System.out.println(i);
    			}
    			inputScanner.close();
     
    			} catch (Exception e) {
    				System.out.println("Exception");
    				e.printStackTrace();
    			}

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java read in + print to a file

    The posted code does not show the statement that defines the Scanner class.
    Post #15 has the proper definition using the File class in the Scanner constructor.
       Scanner inputScanner = new Scanner(new File(fileName));  // define Scanner to read from fileName
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Junior Member
    Join Date
    Sep 2018
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java read in + print to a file

    Found my issue. That line was incorrect on my program i had = new Scanner(fileName) i did not have the new file and extra set of parentheses.

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java read in + print to a file

    I'm glad you are making progress.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Junior Member
    Join Date
    Sep 2018
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java read in + print to a file

    Hey so i now am outputting into the file but im not getting the data to transfer over. The only output i get is vet: Is this because im printing after the loop reads through the whole file?
    		try
    		{
     
    		Scanner inputScanner = new Scanner(new File(fileName));
     
    		while (inputScanner.hasNextLine()) 
     
    			{
    			String i = inputScanner.nextLine();
    			PrintStream ps = new PrintStream(outFile);
    			ps.printf("Vet: %s", i);
    			}
    			inputScanner.close();
     
    			} catch (Exception e) 
    				{
    				System.out.println("Exception");
    				e.printStackTrace();
    				}
     
     
     
    	}
     
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. C# dll to read in JAva (Biometric finger print )
    By dominicevans06 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 20th, 2014, 03:25 AM
  2. Print to file with Java
    By suni.profile@gmail.com in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 30th, 2014, 12:07 PM
  3. Print to file with Java
    By suni.profile@gmail.com in forum Member Introductions
    Replies: 4
    Last Post: May 30th, 2014, 06:50 AM
  4. how to get the print preview for pdf file in java
    By sivanand in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 20th, 2013, 12:55 PM
  5. Java I/O File code; how to read/write file
    By ryu2le in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 18th, 2011, 05:51 PM