Solution: Loading a file

From the course: Learning Java 8

  • Course details

    Java is one of the top-five programming languages, and is used for websites, embedded controllers, and Android app development. This is an introduction to get you started programming with Java. Peggy Fisher introduces the basics: data types, strings, arrays, expressions, loops, and functions. She'll help you control the flow and logic of your code, and create classes using the principles of object-oriented design. Then go a bit beyond the basics and learn advanced techniques for working with arrays, manipulating files, and building graphical user interfaces (GUIs) that respond to user input. This three-hour capsule course is perfect for developers who need to get up to speed with Java fast, as well as for beginning programmers who want their first taste of this popular language.


    • Click here to view Peggy Fisher’s instructor page

      Peggy Fisher

      Content Strategist, Software Development Languages at Linkedin Learning Solutions with content

      Peggy Fisher is a programmer and content manager at LinkedIn Learning.

      Peggy's main focus is application programming in Java, Arduino, and C++. She has also worked on courses in COBOL and discrete ,athematics. Previously she was a faculty member at Penn State University's College of Information Sciences and Technology. She started out as a programmer working for a large insurance company, but after 18 years she left her job as a director of information technology to pursue her true passion teaching. She earned a master's degree in math education, and went on to teach high school math and computer science in Pennsylvania. In 2012, Peggy accepted a position as an instructional designer at Penn State, and shortly thereafter began teaching Intro to Application Programming with Java. She is a strong supporter of women in STEM. As one of the few female programming teachers, she serves as a mentor to incoming female freshmen who are considering a career in programming. She was also the K–12 outreach coordinator for the college, where she scheduled, ran, and taught summer camps for middle school and high school students. In a PBS NewsHour interview, she expressed that all students should take at least one programming class either in high school or college. Peggy enjoys constantly learning and finding new and exciting ways to bring technology to life in and outside of the classroom, such as using Arduino microcontrollers or Lego Mindstorms, to help make learning hands-on and fun.

    Skills covered in this course

  • Welcome

    - This latest challenge was designed to give you practice with reading and writing files. Let's take a look at my solution and, as always, remember that your solution might be very different. Also, I do wanna point out if your program seems to work but the output file is empty every time, check to make sure you close the file before exiting your program. Okay, at the top of my program we have our import statements. We import* to allow us to read and write from files. In the main part of the program, the first thing I did was to clear my input file, File menuItems = new File ("menuNoPrices.txt"). I'll show you the file real quickly. I'll go to Windows Explorer. I'm already in my FileChallenge, and here is my menuNoPrices. If I open it up, you can see I have eight different menu items. Okay, let's go back to the program. I'm also creating my file for the output. This one's going to be called menuWithPrices.txt. That file doesn't exist yet, but because it's an output file, the program will go ahead and create it. I'm gonna scroll down a little bit. I have a String variable to hold my menuItem and a double variable to hold the price. Here is my try and catch. I wanna make sure I handle any exceptions. The first thing I did was create a Scanner object called input, which will represent the menu items in the input file. Right below that I have a PrintWriter statement that's creating a variable output that represents my output file menuPrices. I still need to have Scanner in = new Scanner ( because I wanna ask the user to enter in the prices. I'm using the while loop, which will read from the input file until there is no more menu items. I'm reading the first line in the file into the variable called menuItem, then I'm asking the user, what is the price of this item? I read the price in. Because the price is a number, it won't read in the end line symbol, so I have to get rid of that using in.nextLine. Then I'm printing the menuItem out to my output file. I use the escape sequence \t, and I'm also appending the price to the same line. I'm gonna scroll down a little bit. This line right here, line 36, as I stated, is really important. I have to close my file, otherwise all the information will be left out in the buffer. And, of course, I'm catching my Exception error and printing it out if there is any problems. Let's try and run this program. You can see it read Onion Rings from the input file, so I'll put a price for Onion Rings, $2.5. I'll put a price for each item. Soda was the last item in the list, so I put in the price, $1.75, and my program ended. Now, let's open up our output file and see what it looks like. I'm already in the right folder, I'm in the FileChallenge folder, and there it is, menuWithPrices. Let's open it up. As you can see, it appended the price of each item into my file. It looks like it worked. Okay, hopefully you had fun with this activity and learned more about reading and writing to files.

  • Practice while you learn with exercise files

    Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.

    Download the exercise files for this course. Get started with a free trial today.

  • Download courses and learn on the go

    Watch courses on your mobile device without an internet connection. Download courses using your iOS or Android LinkedIn Learning app.

    Watch this course anytime, anywhere. Get started with a free trial today.