Print Writer And Reading Files
Apr 10, 2015how to use a Scanner to read data from a file. Is it possible to also average a set of numbers at the same time its being read?
View Replieshow to use a Scanner to read data from a file. Is it possible to also average a set of numbers at the same time its being read?
View RepliesI am completing a USACO online problem and am trying to create a print writer to write to my file(ride.out). I did this:
PrintWriter out = new PrintWriter(new BufferedReader(new FileWriter("ride.out")));
However, a load of undefined constructor errors come up for PrintWriter(BufferedWriter) and BufferedWriter(FileWriter). I have imported java.io.* so I don't know what the issue is. This has worked before.
edit: bufferedreader? i give up(not literally)
I am trying to write to an output file that the user names. I have flushed and closed the printwriter, so now I am at a loss. The console output works fine with the formatting, and the file is created, but I cannot get the file to populate. I have the following:
public static void main(String[] args) {
try
{
Scanner kb = new Scanner(System.in);
System.out.print("Input file: ");
String inputFileName = kb.next();
System.out.print("Output file: ");
String outputFileName = kb.next();
// Construct the Scanner and PrintWriter objects for reading and writing
File inputFile = new File(inputFileName);
Scanner in = new Scanner(inputFile);
PrintWriter out = new PrintWriter(outputFileName);
[code]....
I know that I need to use substrings, but I'm not sure how to implement them. All of the names are red in from the file "employees.txt".
Using the file Employees.txt
1) Scan the file and create user ids for each person according to the following scheme
a) Take the first and last character of the first name, convert them to upper case
b) Take the first and last character of the last name, convert them to upper case
c) Concatenate the results from a and b
i) Example: John Brown would yield => JNBN
d) Add the length of the first name to the length of the last name, disregarding spaces
e) Concatenate the result from step c to the result of step d
i) Example: John Brown would yield => JNBN9
f) Concatenate a four digit number to the end of this.
i) The numbers should begin with 0000 and increment by 1
ii) Example: If John Brown is the fifth name in the list the resulting UserID would be => JNBN90004
2) Display the user ids in four columns with a width of 14
I have copied a wav file as explained in [URL] ..... When trying to play my new file, it runs but no sound is hear. My reading and writing class is as explained in another post in the site:
This is the function that reads the file:
// read a wav file into this class
public boolean read(DataInputStream inFile) {
myData = null;
byte[] tmpInt = new byte[4];
byte[] tmpShort = new byte[2];
[Code] ....
I can't hear the copied wav file, but still the wav file is correctly copied...
public class Tester
{
public static void main(String[] args)
{
Product p1 = new Product();
p1.loadFromFile("Monitor.pr");
System.out.println("Actual: " + p1.toString());
System.out.println("Expected: Product [id=12345, name=Monitor, description=A freakin great monitor!]");
p1.setId(11111);
p1.setName("another product");
p1.setDescription("Description of another product!");
[code]....
The expected output is what my program should be doing and the actual output is what it's doing instead. As you can see, my code works for the middle test. But I cannot understand why it won't load the first fileFor the second half, the id and the quantity are separated by a comma... I'm not really sure how to deal with that. As you can see, I tried using a delimiter, but it doesn't seem to be doing any good.
So I have a text file, and I want my Java program to store names from the text file. How do I do that? This is what I have so far.
Java Code: import java.util.Scanner;
import java.io.File;
BingoCard[] cards = new BingoCard[n]; //Array of BingoCard objects, n being the length of the Array
Scanner sc = new Scanner(File("Names")); //Names is the name of the file
for(int c = 0; c < cards.length; c++)
cards[c] = new BingoCard(sc.nextLine); mh_sh_highlight_all('java');
Here's the constructor.
Java Code: private string myName;
public BingoCard(String name)
{
myName = name;
} mh_sh_highlight_all('java');
Whenever I compile, I get this error message.
So here is my code:
while (key != 3) {
if (key == 2) {
System.out.println("Input from a file");
// System.out.print("Enter file name: ");
// String str = expression.nextLine();
int i = 0;
File file = new File(
[code]....
What I need to happen is the file gets read in, the file is in the form:
a * ( b + c ) / 2 + ( 8 * b )
a = 5
b = 10
c = 20
( x + y ) * z
x = 13
y = 21
z = 3
And so on. My code is supposed to start at the beginning and have output like:
Infix: a * b ( b + c ) / 2 + ( 8 * b )
variable values:
a = 5
b = 10
c = 20
**It then needs to store the variable names along with their values in a hash table**
postfix: a b c + * 2 / 8 b * +
value: 155
And it continues reading the file in the same fashion.
My issue is I am not properly extracting the variables and their values from the file. I have the variables in an array list actually, so I have those, but don't know how to efficiently navigate to the values, then store the character and value in a hash table, while protecting against possible blank lines within the file. Thus just calling nextLine()s and next()s wouldn't work to get to values because they would lead to an error.
I just can't seem to figure it out how to solve this inherently recursive assignment.The task is to check a directory path and read the files within that path; also, if there are more directories within it, we have to go deeper into those directories to read the files within them - if any. What you're looking at is my skeleton of the assignment:
public class SearchingForStrings {
public static void main(String[] args) {
String path = "."; // Default
File sf = new File(path);
String mainDirectory = args[0]; // These two are just
String keyString = args[1]; // command-line arguments
countLinesWithString(sf, mysteriesDirectory, keyString);
[code]....
have to create a file named Lab13.txt. In the file I have 10 random numbers. I have to import the 10 numbers and have to Multiply all the numbers from Lab13.txt by 10 and save all the new numbers a new file named Lab13_scale.txt. so if the number 10 is in lab13.txt it prints 100 to Lab13_scale.txt. how do i get it to Multiply Here is what I have:
Java Code: import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
[Code].....
I want to read about a 100 files. There are 4 Categories of the files. I also want to create a Database of these files. The 4 Categories are my Columns. My question is how to it. I know what i want to do but not how to write in code.
View Replies View RelatedIn the program below I'm trying to read the contents of several .txt files in the same diretory and create a new .txt file containing all of the data from each file. My output is generated in the console however my .text file is blank.
public static void main(String[] args) throws IOException {
String target_dir = "C:Files";
String output = "C:Filesoutput.txt";
File dir = new File(target_dir);
File[] files = dir.listFiles();
[Code] .....
I am currently building a Plugin system for an application and I wanted to add a little security feature.
I have read many tutorials on this and they basically all came down to the same answer:
"Its complicated: You need a custom class loader and security manager and ... and even then its still tricky"
So I thought, before I load the classes dynamically I will simply read the class files. Why bother with a SecurityManager or ClassLoader if I can simply whitelist all allowed classes and search for all illegal Class access before I even load anything.
How can I Cache the data I'm reading from a collection of text files in a directory using a TreeMap? Currently my program reads the data from several text files in a directory and the saves that information in a text file called output.txt. I would like to cache this data in order to use it later. How can I do this using the TreeMap Class? These are the keys,values: TreeMap The data I'd like to Cache is (date from the file, time of the file, current time).
import java.io.*;
public class CacheData {
public static void main(String[] args) throws IOException {
String target_dir = "C:Files";
String output = "C:Filesoutput.txt";
File dir = new File(target_dir);
File[] files = dir.listFiles();
[Code] ....
In the current program I am trying to read multiple text files from a folder. I keep getting the following syntax error
"Syntax error on token ";", { expected after this token".
I highlighted the line where im getting this error in red.
import java.io.*;
import java.util.*;
public class CacheData {
// Directory path here
String path = "C:myfiles*.txt";
[Code] ....
How can i convert this linked list code to a read from input.txt
The first line in the input file will give the elements to initialize the linked list with. Consecutive lines will provide operation instructions.
Your code should read one line at a time. After reading each line, it should perform the corresponding operation and print the linked-list on the console.
If an operation is not possible, it should print "N/A".
Sample input file. Please note, the comments (// ...) are given for explanation, the input file will not have them:
4, 5, 6, 3// First line. This will provide the initial values for the linked list : 4->5->6->3
1, 9// Add a 9 at the front of the linked-list. After this operation the linked-list should be: 9->4->5->6->3
2, 1// Add a 1 at the end of the linked-list. After this operation the linked-list should be: 9->4->5->6->3->1
3, // Delete the first node in the linked-list. After this operation the linked-list should be: 4->5->6->3->1
4, // Delete the last node in the linked-list. After this operation the linked-list should be: 4->5->6->3
5, 11// Delete the node with the value 11 in it. Since this is not possible, it should print "N/A"
5, 6// Delete the node with the value 6 in it. After this operation the linked-list should be: 4->5->3
Sample output to the console:
4->5->6->3
9->4->5->6->3
9->4->5->6->3->1
4->5->6->3->1
4->5->6->3
N/A
4->5->3
My Code:
LinkedList.Java
class linkedList
{
protected Node start;
protected Node end ;
public int size ;
[Code] .....
I put the code on pastebin.com because it is relatively long. How to improve this code that does not occur starvation and was ucziwy terms of writers and readers.
[URL] .....
I've been working on a saving system for a game I'm working on using java IO writer and reader. When I tried exporting it (as a executable jar) to test I couldn't get it to work, when it worked before. If I had to guess it's the file location not being able to find the file. Which it just looks like :
FileReader end = new FileReader("Saves.txt");
I don't exactly know how to set it up to read the file. I'd tried giving it a look up but I don't know how to exactly word my problem.
Trying to write to a txt file using buffered writer - however I'm getting some issues.
BufferedWriter says 'The resource type BufferedWriter does not implement java.lang.AutoCloseable'
br says 'The type BufferedWriter is not visible'
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileWriter {
public static void main(String[]args){
[Code] .....
I am using netbeans to create a hotel booking system, just tessting out code to get the booking information input to a file when the next button is clicked.
File file = new File("file.txt");
try (BufferedWriter br = new BufferedWriter(new FileWriter(file))) {
br.write("################################");
br.newLine();
br.write(" Booking Information");
br.newLine();
br.newLine();;
br.write("First Name: " + TxtName.getText());
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
Nothing is getting wrote to the file though. Does that code seem right?
What step to know to develop software..
View Replies View RelatedIn a program I created, I'm using a text file that contains some texts needed for the program. The method relevant to this is something like the following.
private String wordgen(){
try {
BufferedReader reader = new BufferedReader(new FileReader("src/Resources/adjectives.txt"));
Random rand = new Random();
int low = rand.nextInt(400);
String fil="";
int i=0;
while(i!=low){
[Code]...
The program runs fine in netbeans project but once the jar is created it does not corporate with the text file. ("null" is returned) How can I attach text files to jar and exe?
Basically the class is supposed to have a static variable that keeps track of the number of companies (numberOfCompanies) that have been created and a static method that returns the value of this variable. I need to have a constructor and a method to addEmployee, a method to printCompany, and a toString method. The addEmployee method will check that there is only 1 salesperson, 2 designers, and 4 manufacturing persons at a time and will check that there are no more than 7 employees of the company.
The addEmployee method will return a String indicating what the error is (i.e. "There is already a sales person in this company") or a null if the employee was added. If attempting to name a third company, an error message will be shown. what i have so far but my print company mmethod returns null company class :in which i am obliged to use inputdialog because it says that it cannot return void when i use the showmeassage diaolog.
company class:
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class Company {
private ArrayList<Employees> list ;
private static int numberOfCompanies;
[Code] ....
error message when print button is clicked:
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: JOptionPane: parentComponent does not have a valid parent
at javax.swing.JOptionPane.createInternalFrame(Unknown Source)
at javax.swing.JOptionPane.showInternalOptionDialog(Unknown Source)
at javax.swing.JOptionPane.showInternalMessageDialog(Unknown Source)
[Code] ......
I need to transformation the txt files into xml files, but each row txt files don't have same elements, for example the first book is composite one author
<books>
<book>Title</book>
<price>price</price>
<author>Author</author>
</books>
but the second book is composite two author
<books>
<book>Title</book>
<price>price</price>
<author>Author</author>
<author>Author2</author>
</books>
I would have the xml files with the number of authors variables, I can use JAXB for my problem?or not?
I want to create two thread one thread will print odd number and another thread will print even number. but the number should be in a sequence like
1 2 3 4 5 6 7 8 9 10
I know that in c++ in order to read a string like
PHP Code:
. you need to use
getline(cin,string variable)
. In java, I have noticed that using
String name=keyboard.nextLine()
automatically reads the whole line...Suppose I wanted to read the first name only, how would I be able to accomplish that?