PrintWriter Only Prints Last Line?
Dec 6, 2014
I'm tyring to print the same output in console to a text file, but I can only get the last line of the console output in the text file, not sure what is wrong with my code:
while (in.hasNextLine()) {
PrintWriter writer = new PrintWriter("output5.txt");
tempS = in.nextLine().toLowerCase();
System.out.println(wp.bestPages(tempS));
[code]....
What's causing only the last time to be printed in text file? Are there better ways to print console outputs into a text file than PrintWriter?
View Replies
ADVERTISEMENT
Feb 17, 2015
You are given a file containing the names and addresses of company employees from many years ago that your manager has asked you to import into a database. You can use a CSV file and your database application to load the file, but the file your manager gave you was exported from an old, non-standard accounting system. Here is its format:
Fred|Flintstone
1212|Bedrock
Austin|Texas
Harry|Potter
1234|Hogwarts Road
[Code] .....
View Replies
View Related
Apr 12, 2014
I've written a simple html/servlet program that has a user enter their name and password on a form and then either register or login using a submit button. I have the program working, except when a user doesn't fill in either of the text fields I can't figure out how to get it to print to the page. Right now I just have it printing to my Eclipse console which is not what I want. What am I missing?
HTML code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Cookies</title>
[code]....
View Replies
View Related
Nov 9, 2014
i've got this code that i cant get to work as i want it to. when its exported and i run it the file i wants gets created but when i open the file there is a single number like 999998000001
import java.io.IOException;
import java.io.PrintWriter;
public class mainHej {
public static void main(String arg[]){
[code]...
View Replies
View Related
Aug 20, 2014
If I am using the File object to read a file and PrintWriter object to write to a separate file, can I use them under one method. Or is the File one method and the PrintWriter another method?
View Replies
View Related
May 1, 2015
I thought you can use PrintWriter to write to the console but when I run the following code I only get "abc" printed to the console.
import java.io.*;
class Class1{
public static void main(String...abc) throws Exception{
PrintWriter writer = new PrintWriter(System.out);
writer.println("test");
System.out.println("abc");
}
}
View Replies
View Related
Oct 24, 2014
How to write a new file:
import acm.program.*;
import java.io.*;
import java.io.PrintWriter.*;
import acm.util.*;
import javax.swing.*;
public class PrintWriter extends ConsoleProgram {
public void run() {
try {
PrintWriter wr = new PrintWriter( new FileWriter ("hello.txt") ) ;
wr.println("Hello world!");
wr.close();
} catch (IOException er) {
println("File could not be saved.");
}
}
}
(I added the imports at top myself.) However, I am getting compiler errors for the lines: PrintWriter wr = new PrintWriter( new FileWriter ("hello.txt") ) , which says that the constructor PrintWriter( FileWriter ) is undefined, and
wr.close();, which says close() method is undefined.
Pretty sure the only real problem is that PrintWriter is not accepting the FileWriter as constructor, but I don't see why. I have tried this on machine with JRE 1.4 and it worked as expected, creating new file titled "hello.txt", prints line of "Hello world!" in that file, and saves it to the directory I picked in the dialog. But I can't get it to work on this machine that uses Compiler 1.6, Java Runtime v8u25.
I have also tried using just a string in the parameter for PrintWriter, such as PrintWriter wr = new PrintWriter ("hello.txt") , and from what I can tell by reading the java spec for java 8, this should work. [URL] .... But I get error message for that constructor as well.
View Replies
View Related
Oct 25, 2014
I am creating a car simulator that simulators the odometer and fuel gauge on a car. I want to print the results to a text file but it seems to be printing only the last two lines instead of all of them. How can I make it so it doesn't overwrite the previous input?
Here's my main method:
public static void main(String[] args) throws FileNotFoundException {
CarInstrumentSimulator carInstrumentSimulator = new CarInstrumentSimulator();
FuelGauge gas = carInstrumentSimulator.new FuelGauge();
[Code].....
View Replies
View Related
Dec 8, 2014
I have done lots of PrintWriting before but this is the first time I have done it on my new computer and I'm having an error I haven't seen before. This is my code:
Rprint(){
try {
PrintWriter pout = new PrintWriter(new FileWriter("C:UsersJackDocumentsexample.r "));
pout.println("nums = c(1,2,5,6,8,10)");
pout.println("nums");
pout.close();
} catch (IOException ex) {
System.out.println("Error!");
Logger.getLogger(Rprint.class.getName()).log(Level .SEVERE, null, ex);
}
}
*** java.io.FileNotFoundException: C:UsersJackDocuments
This.txt (The filename, directory name or volume label syntax is incorrect) ***
I have a feeling it may be to do with permissions, because it lets me print simply ("example.r") into my project folder.
View Replies
View Related
Jun 30, 2014
Write a program that asks the user for the names of two files. The first file should be opened for reading and the second file should be opened for writing. Ihe program should read the contents of the first file, change all characters to uppercase, and store the results in the second file. The second file will be a copy of the first file, except that all the characters w ill be uppercase. Use Notepad or another text editor to create a simple file that can be used to test the program
Here is my code. I Also made two text document files that are located in the same directory as the .class file (the code file) that are named OriginalFile and UpperCase respectively. Please let me know what I need to do. Here is the code.
import java.util.Scanner;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class UpperCaseFile
{
public static void main (String [] args) throws IOException
{
[Code]...
View Replies
View Related
Apr 29, 2015
private static void getTablas(int num) {
//ciclos anidados
System.out.print("x");
//linea horizontal
for (int i = 1; i <= num; i++) {
System.out.printf("%3d ",i);
[Code] .....
View Replies
View Related
Nov 21, 2014
I have wrote this class who read from text line by line and save the words in fileOnTable.. Now i don't know what to read in ReadOffer to save the words in object offers and return this.. One more question.. What JUnit test can write for this code..?
package com.example.crazysellout.UserSide;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
[Code] ....
View Replies
View Related
May 5, 2014
My requirement is to find the line number using multiline string. Here I need to extract the string between FROM and where clause(from the below string) and need to find the line number in the file
SELECT HL.LOCATION_ID,HPS.PARTY_SITE_ID,HCAS.CUST_ACCT_SITE_ID
INTO LN_SITE_LOCATION_ID,LN_LOC_PARTY_SITE_ID,LN_CUST_ACCT_SITE_ID
FROM HZ_LOCATIONS HL,
HZ_PARTY_SITES HPS,
[Code]....
View Replies
View Related
Sep 10, 2014
Write a program that accepts the line number from the user and prints only that particular line from the Floyd triangle.
Example:
Input: 2
Output: 2 3
Input: 3
Output: 4 5 6
View Replies
View Related
Feb 18, 2015
How do I make the file reader object move to the next line if there is no more input on the line. Here is my text and output file as you can see that my text file column cuts off on the 2nd line after 70. I want to read that next line which is 100 into my labs variable however its reading it into my final exams variable. I'll also post the code but I didn't think it was necessary.
textfile:
100908095
1008070
10070
output:
Labs Projects Tests Final Exams
100908095
1008070100
70
[import java.util.Scanner;
import java.io.*;
public class MyGrades
{
public static void main (String[] args) throws IOException
{
int lab, project, test;
int finalExam;//Par and Player values
[Code]...
not sure if I code wrapped it correctly
View Replies
View Related
Oct 15, 2014
If I want to read my file line by line and when it hits a certain value from that point it should start deleting the lines until the tag ends.Just curious will my code actually work at the moment or not because it goes through so many times then goes straight back to the variable declarations at the start of the method and never hits the console print line.
public void removeEnvironment(){
//declare variable to environment id
String environmentID = "Environment id";
String lines = null;
boolean lineFound = false;
boolean end = false;
[code]...
View Replies
View Related
Oct 20, 2014
I'm creating a program that searches a txt file for a given string, then return the number line and the line itself. However, my testFile class isn't detecting my searchWord methods.
The searchWord and recursiveSearch is written in a java class called BasicFile
public List<String> searchWord(String key) throws Exception {
LineNumberReader lnr = new LineNumberReader(new FileReader(f));
return recursiveSearch(lnr.readLine(), key, lnr);
}
public List<String> recursiveSearch(String currentLineText, String key, LineNumberReader lnr)
[code]....
Is it because I'm using a list instead of a string?
View Replies
View Related
Apr 12, 2014
I'm doing an assignment for uni and have come across a small hiccup. What I'm trying to do is scan in a text file and read "commands" for it line by line, E.g:
Student Mary 12345 19
Student Joe 12346 19
Change Joe 19 20
Change Mary 19 20
So that lines that begin with the word "Student" indicate that I should create a new student file with that name, student ID, and age.
"Change" indicates that I should be changing the specified student's current age to the new age etc.
What I'm currently doing is something along the lines of this:
Scanner input = new Scanner(new FileReader(args[0]));
String[] line;
while (input.nextLine().startsWith("Student")) {
line = input.nextLine().split("s+");
[Code] ....
The problem I'm having is that every second line seems to be getting skipped (because I'm calling nextLine() so much?) but I can't think of a way to "peek" at the first word of each line without advancing past it. Is there any way of doing this?
View Replies
View Related
Jun 13, 2014
Does printwriter always truncates the existing file to 0 size before adding any new data ??
View Replies
View Related
Dec 8, 2014
how to read file line by line ? Namely my input file I want to read look as follow:
AAA 1, 1
12 222 12
AAA 2, 2
11 122 11
My output file should look as follow:
1, 1, 12
1, 1 222
1, 1, 12
2, 2, 11
2, 2, 122
2, 2, 11
I think the lines need to be stored in ArrayList, then I would like those lines to write to csv file, but how on read I can construct such output file? This is my code for reading file
public ArrayList readFile(String filename)
{
try
{
[Code]....
View Replies
View Related
Jan 20, 2015
The second message dialog result is always 0 ... i want to find the minimum grade...
import javax.swing.*;
import java.util.Arrays;
public class Parrarrapapa{
public static void main (String[]args){
String length = JOptionPane.showInputDialog("Number of students");
[code]....
View Replies
View Related
Nov 16, 2014
Ok, so let's say I am having a user input scores and at the end I want to print out the results so I do something like:
int[] Scores = new int[1000];
//code to ask user for input and store into array //
The user only inputs lets say 5 scores out of possible 1000. I then try to print it out by doing something like this:
for(int counter = 0; counter < Scores.length; ++counter) {
System.out.println(Scores[counter])
}
How would I go about printing out only the index's that were input, because right now it prints out the scores and then 995 0's after.
View Replies
View Related
Feb 4, 2015
When I run this code, it is supposed to get one value from turnTimer(); and return it, just as a test. This works when I enter a valid pit. For example. If I were to input "A" when it's player one's turn, it will return 1, like it should. However, if I were to type "H" when it's player one's turn, it returns "Not a valid pit!"(like it should) but then it also returns 12. It shouldn't know that H is 12 because it's in a separate method. I'm confused as to why it's printing both values.
import java.util.*;
public class Mancala {
static Scanner input = new Scanner(System.in);
public static int pit;
public static void main(String[]args) {
Mancala mancala = new Mancala();
int[] board = {0,3,3,3,3,3,3,0,3,3,3,3,3,3};
[Code] .....
View Replies
View Related
Dec 8, 2014
I've a ArrayList with dogs on, and I've a function that should allow me to delete a dog from the register and if the dog was found it prints "The dog was deleted" and if the dog doesn't exist in the arraylist it should print "the dog couldn't be found".
Everything works perfect until I shall delete a dog that is not first on the list. Then the program shows first "dog was not found" and on the row after "the dog was deleted" if the dog was second on the list. If it was third I will get 2 messages that the dog wasn't found and then that the dog was deleted. I've no clue why it prints both else and if!
public static void taBortHund(){
//Har tar vi bort hund fran listan
//System.out.println(hundlista);
System.out.print("Vilken hund vill du ta bort? ");
String hunden = tangentbord.nextLine();
for (int taBort = 0; taBort<hundlista.size(); taBort++){
[code]....
View Replies
View Related
Dec 12, 2014
I am checking how to do following task.
01. pickup the selected text file and read the line by line and output the text in to visual text pane.
what i did:.
01. I wrote code that read the text file and output in to jave console/ also some of the interface.
the code read txt file:
Java Code:
String fileName = "C:/Users/lakshan/Desktop/lawyer.txt";
File textFile = new File(fileName);
Scanner in = new Scanner (textFile);
while(in.hasNextLine()){
[code]....
so it will read any text file dynamically and output to the text pane in interface. I think scanner code must be execute after the select the file from the browser and set the scanned result in to variable. then later out put the var as string in some jswing component?
View Replies
View Related
Sep 30, 2014
I am stuck and having a problem trying to find what i believe is a simple fix but can't wrap my head around it. For class I needed to make a table that prints out Sin,Cos,Tan for angles in steps of 5 to 180. The problem is in my output with the first couple lines of the table. for angle 5.0 the Sin(.0872) Cos(.9962) and Tan (.0875) are being displayed on the angle line 10.0
public class TrigTable {
public static void main(String[] args) {
double angle = 0;
double sin =0;
double cos = 0;
double tan = 0;
System.out.println("Angle Sin Cos Tan"); // Table header for the values
[Code] ....
Here is the first couple lines of output:
Angle Sin Cos Tan
----- --- --- ---
0.0 0.0 1.0 0.0
5.0 0.0 1.0 0.0
10.0 0.0872 0.9962 0.0875
15.0 0.1736 0.9848 0.1763
20.0 0.2588 0.9659 0.2679
View Replies
View Related