How To Make File Reader Object Move To Next Line If There Is No More Input On Line
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
ADVERTISEMENT
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
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
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
Jan 27, 2015
So I'm having a problem with the .hasMoreElement() method. I try to read lineOfInput for a file which contains:
Hobbit, Long,t@gmail.com,475-555-4444,3
Long, Hobbit,b@yahoo.com,445-222-5342,2
Hobbit,Long,b@yahoo.com,465-222-5342,2
Help,Yerp,,455-222-2222,4
but when i get to the ,, on the last line I get a NoSuchElementException and the program crashes.
StringTokenizer inputField = new StringTokenizer(lineOfInput, ",");
while(inputField.hasMoreElements()) //for each fields is a line
{
lastName = inputField.nextElement().toString();
firstName = inputField.nextElement().toString();
email = inputField.nextElement().toString();
phoneNumber = inputField.nextElement().toString();
level = Integer.parseInt(inputField.nextElement().toString());
}
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
Mar 21, 2015
I am trying to read a file and input each line of the file into an array. I have not set the array size as I was hoping to fill the array using a while loop. Unfortunately the scope of the array does to work inside the while loop so I am being told that the array 'students' has not been initialised even though it has just outside of the while loop. Is it possible to do what I am trying to without having the array initialised in the while loop as surely the array will be reset every time if it was in the while loop? Here is my code:
public static void students(String file) throws FileNotFoundException {
try {
File studentInfo = new File(file);
Scanner input = new Scanner(studentInfo);
String[] students;
[Code] ....
View Replies
View Related
Jun 22, 2014
I am having a lot of trouble with this lab. basically I have to make a text processor to read in code put it into a file and output the contents of the input file in alphabetical order one word per line. I have to reference the string varaibles input_filename and output_filename.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.File;
public class TextProcessor {
[Code] ....
these are the guidelines to said lab:
1. In the main method define two Strings called input_filename and output_filename. I will be setting these Strings to my own file names for testing your code, so make sure you get these variable names correct, and that you use them properly in the following steps.
2. Read in the text contents of the file referenced by input_filename.
3. Split the contents of the input text into separate tokens, using whitespace as a delimiter.
4. Lower case the tokens.
5. OPTIONAL: remove punctuation
6. Alphabetize the tokens.
7. Using output_filename, write the alphabetized tokens to an output file, one token per line.
8. Be sure to close file streams - no resource leaks!
9. Use methods to separate functionality in your program where possible
View Replies
View Related
Mar 27, 2015
can a keyevent in java make the space ship and the laser too ?
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
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
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
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
Mar 21, 2015
By using FileReader, FileWriter and their constituents, I am creating a file to contain employee information ( name, age, hours, etc. ). The user is to input all of the data on a single line and we were asked to implement StringTokenizer to assign that data to the file. I have never used the StringTokenizer before, but I have a rough idea how it is to function. I used pw.println to test what I have so far, now I would like to let the user build the initial file with the "first employees" of the company, and then view the file, and then go back and append new employee data to that same file. My question is, how can I take the user input as a StringTokenizer and add that to the file?
In the for loop below, I thought I would see if it would work, but it does not. The loop only executes once and does not allow me to enter data.
public class Records {
public static void main(String [] args) throws IOException {
Scanner input = new Scanner(System.in);
FileWriter fw = new FileWriter("dbs3.java");
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
System.out.println("NEW EMPLOYEE DATA SHEET");
System.out.print("Number of new employees: ");
int number = input.nextInt();
[Code] ....
View Replies
View Related
Oct 18, 2014
After i draw line between 2 fixed points on two different rectangles i need to rotate them. The problem is that my line is not updated, it stays on the same x1,y1 x2,y2. How to make line to follow rectangle when they are moving or rotating? I wrote some example code to demonstrate this.
Object that i want to draw:
[public class Object {
private int xPos;
private int yPos;
private int width;
private int height;
float xDistance = 0f;
float yDistance = 0f;
double angleToTurn = 0.0;
[Code] ....
View Replies
View Related
Jul 20, 2014
I am getting this error "Type mismatch: cannot convert from Object to E" in the last line of the pop() method.
package stack;
public class Node<E> {
E item;
Node next;
Node(E item) {
this.item = item;
this.next = null;
}
[code]....
I don't understand why that error occurs despite item being declared as type E in the Node class.
View Replies
View Related
Jul 17, 2014
I am using Java 1.6, I have this class ....
import java.util.ArrayList;
import java.util.List;
public class TestDrive
{
public TestDrive()
{
super();
[Code] ....
What is the most efficient way to group the preferences of the same type, In other word I want just a print statement to print this line.
BREAKFAST(Eggs,Milk),SPORTS(Basket,Tennis),....
View Replies
View Related
Feb 11, 2015
I'm having trouble with a program, reversing numbers. I got the program itself to work! However, I am having an issue with the output.
import java.util.Scanner;
public class p1a {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int n;
int reverse = 0;
[Code] .....
If I were to input, say 2341, I would get this: The reverse of 0 is 1432
The 0 should have been 2341, or whatever I input. What am I doing wrong?
I tried to move line 21 : (System.out.println("The reverse of " +n+ " is "+reverse);
Above the while loop, but instead get:
The reverse of 2341 is 0
How can I get my input and my result together in the same line?
View Replies
View Related
Mar 29, 2014
I have a csv file and I want to insert the csv file into the database columns consist of date, description1, description2, amount1, amount2, narration.. but the problem is the narration is coming in second line in csv file due to which the data is not getting inserted into the database. So how to append the narration into the first line,I have enclosed the demo format of csv file and a code to read the csv file..
myFile.csv file:
20-5-2013,"Cash Rec. From abc","pymt",-500.00,,
"Cash rec. from abc office",
20-5-2013,"Cash Rec. From xyz","rcpt",-5100.00,,
"Cash rec. from xyz office",
[Code] .....
View Replies
View Related
Jun 9, 2014
I'm trying to print the information entered into my TimeClockApp to a text file named timeclock.txt. Everything is writing to the text file, except instead of adding each part of the run through the app to one line, it is saving each part on a separate line like this:
i
3
2014/06/08 15:32:29
To ensure that each run through the time clock is recorded on a different line in the text file, I want it to add each item like this instead:
i 3 2014/06/08 15:32:29
I have tried to execute the
out.println(timeClock.get(i)); line as out.println(timeClock.get(i) + " "); and out.println(timeClock.get(i) + "
");,
but have had no luck in fixing this problem. My current code that writes the information to the text file is as follows:
// write items to timeclock.txt file
try {
// open an output stream for overwriting a text file
PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter(fileName))); // (filename, true) when
[Code] ....
View Replies
View Related
Mar 5, 2014
i start work with TxT files. And i need know how to write text in the next line if file exist. My code just last text replace new.. Here code :
Main :
package YoYo;
import java.util.Scanner;
public class Main {
static String atsakymas;
static String FileName;
[code]....
View Replies
View Related
Feb 14, 2015
I am want to delete the first line of text file using RandomAccessFile class. My Input file "bulkwfids.txt" has the data as below:
234567
345578
455678
566667
Expected output needs to be(first line deleted):
345578
455678
566667
But the actual ouput I am getting is as follows:
78
56
345578
455678
566667
Here is the code snippet I have written:
import java.io.*;
class DeleteLine {
public static void main(String [] args) throws FileNotFoundException, IOException {
RandomAccessFile raf = new RandomAccessFile("C://Users/hp/Desktop/bulkwfids.txt", "rw");
raf.readLine();
[Code] .....
View Replies
View Related
Feb 6, 2015
Lets say this txt file contains 2 lines.
---------
hello
bye
---------
What would i have to do to insert a a piece of text in between hello and bye? I would like for the it to search for "hello", then add a line after it so it would look like :
-------
hello
newtext
bye
-------
I have these 2 methods that work as intended, but i just cannot seem to do what i've stated above.
Java Code: /**
* @param location
* Location of the .txt file.
* @param text
* The String to search for in the .txt file.
[Code] ....
View Replies
View Related
Jan 24, 2015
I am trying to write to a file but everything I have tried doesn't seem to work. What I want to do is read a file and then output back to the same file on a new line. The text file contains the sentence "Java is a programming language".
import java.util.Scanner;
import java.io.*;
class HW1B {
public static void main(String[] args) {
Scanner fileIn = null;
try {
fileIn = new Scanner (new FileInputStream("text.txt"));
[Code] ....
These are the errors :
HW1B.java:19: error: no suitable constructor found for Scanner(FileOutputStream)
fileOut = new Scanner (new FileOutputStream("text.txt"));
^
constructor Scanner.Scanner(Readable) is not applicable
(argument mismatch; FileOutputStream cannot be converted to Readable)
constructor Scanner.Scanner(InputStream) is not applicable
[Code] ....
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
2 errors
View Replies
View Related
Jan 11, 2015
How to output text to a file, so I had to do my own research on google, but the results I found were confusing. I finally got my code to write to a file, but I cannot figure out how to append a new line. I know what part of the code is incorrect, but I don't know how to fix it. here is what I have right now:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
public class highscore {
public static void main(String[] args) throws IOException {
[Code] ....
I can see the last two lines are telling the program to overwrite the first input with the second. Of course if I pick a different file name for the second output, I get another file with the second input, but I need to learn how to append as well.
View Replies
View Related
Feb 7, 2015
i'm trying to add new value (string type) in an existent file.say that we have a .txt file which contain "mario"...i ask to the user a new name, and he write for expample "tony", now i want append the word "tony" in the existent file in this way: Iwrote this code:
Java Code:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
[code]....
first is saved next to the value existing. why? maybe because the program does not check if there is a string in the file?but I do not think. because otherwise it would happen with any name that is not entered in an odd position.
View Replies
View Related