I/O / Streams :: Reading Up To The First Delimiter And Then Moving On To Next Line?
Mar 22, 2014
I have a text file that has contents as follows:
testing1,true,0,0.0,0,0.0,4.0,0.0,0.0,0.0,0.0
test2,true,0,0.0,0,0.0,4.0,0.0,0.0,0.0,0.0
I am trying to read only the first item in each list using a comma as a delimiter using the following code.
public String[] pList() {
ArrayList<String> names = new ArrayList<String>();
String word;
try {
String path = P_FOLDER + P_FILE;
[Code]....
View Replies
ADVERTISEMENT
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
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
Jul 24, 2009
I am just trying to move a rectangle in a diagonal line. When the applet pops up, the rectangle is there but doesn't move. If I drag the sides of the applet to make it bigger or smaller the rectangle will redraw itself and move, but I want it to move without touching the window.
Java Code:
package javaapplication3;
import java.applet.Applet;
import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Random;
[Code] .....
Why does it only repaint when I resize the window?
View Replies
View Related
May 14, 2014
I am trying to only allow the user to input numbers. But I need to enter a number twice before it moves to the next line statement and also skips a line when i enter th number a second time.
How can I go around fixing this.
My code for this is
case 1:
do{
Event event = new Event();
out.println("Please Enter the name.");
event.setEvent(input.next());
input.nextLine();
[Code]...
View Replies
View Related
Aug 4, 2014
I'm learning about the printf command, and when I have it, it is not letting me ad an input. Here is my quick little program:
import java.util.Scanner;
public class TestingPrintF {
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
[Code] ....
The first section works good, but when i move down to the second part it just automatically finishes without letting me enter a phrase.
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
Sep 17, 2014
I have a code below that is reading large image size and writing them to file however the process is too slow. how can i refine this code in such a way that it will work faster?
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.HttpURLConnection;
[code]....
View Replies
View Related
Dec 6, 2014
I'm trying to synchronize two folders and their sub directories between a client and a server. I have a modified version of this class which I've posted below. In my Client class, I create a WatchDir object and call its processEvents() method in an infinite loop. The method returns a myTuple object (a struct containing the event type and a path object) if an event is registered and null if not.
The problem is that this only seems to work for the first event to happen in the directory (i.e. if I add a file to the watched folder, my WatchDir object.processEvents() returns one Tuple with an ENTRY_CREATE event and never returns another Tuple for other file additions/deletions/modifications that happen after). I'd like for processEvents to be continuously called (hence the infinite while) returning a Tuple each time some event occurs.
My modified WatchDir:
import static java.nio.file.StandardWatchEventKinds.*;
import static java.nio.file.LinkOption.*;
import java.nio.file.attribute.*;
import java.io.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
[Code] ....
View Replies
View Related
Sep 17, 2014
I have a code below that is reading large image size and writing them to file however the process is too slow. how can i refine this code in such a way that it will work faster?
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.HttpURLConnection;
[code]...
View Replies
View Related
Sep 26, 2014
If I'm using an xml file and the piece of data I want from the file looks like Staff id ="Test";
Is it something like this? Only seen two examples of it but I'm not sure they were reading from a file like myself.
Staff id = """ + Test + """;
View Replies
View Related
Oct 11, 2014
I'm having a bit of trouble with using the Scanner and the Printwriter. I start with a file like this (1 = amount of Houses in the file)
1
FOR SALE:
Emmalaan 23
3051JC Rotterdam
7 rooms
buyprice 300000
energylevel C
The user gets (let's say for simplicity) 3 options:
1. Add a House to the file,
2. Get all Houses which fullfil requirements (price, FOR SALE / SOLD etc.) and
3. Close the application.
This is how I start:
Scanner sc = new Scanner (System.in);
while (!endLoop) {
System.out.println("Make a choice);
System.out.println("1) Add House");
System.out.println("2) Show Houses");
System.out.println("3) Exit");
int choice = sc.nextInt();
Then I have a switch for all of the three cases. I keep the scanner open, so Java can get the user input (house = for sale or sold, price = ... etc). If the user chose option 1, and all information needed is inputted and scanned, the House will be written to the file (which looks like what I typed above).
For this, I use try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Makelaar.txt", false)))). This works perfectly (at least so it seems.)
If the user chose option 1, and all requirements are inputted and scanned, the Houses will be read (scanner) from the file and outputted. For this I use the same Scanner sc. This also works perfectly (so it seems atleast).
My problem is as follows: If a House has been added, I can only read the House(s) which were already in the file. Let's say I have added 2 houses, and there were from the start 3 houses. If option 2 is chosen, the first 3 houses will be scanned perfectly. An exception will be caught for the remaining 2 (just added) Houses. How can I solve this? I tried to close the Scanner, and reopening it, but apparently Java doesn't agree with this
View Replies
View Related
Apr 23, 2015
I'm having trouble with how to read a text file into my program so the words can be sorted alphabetically. Should I use something like a FileReader?
An example of what I'm trying to do is a .txt file that holds the statement "java is a simple object oriented and distributed and interpreted and robust and secure and dynamic."
The output should organize the words like so: and and and and distributed dynamic interpreted is java object oriented robust secure simple
Here's what I have:
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: java AscendingAlphabet"
[Code].....
I can never get it to read the file correctly, it just prints the "usage: java..." statement.
View Replies
View Related
Feb 6, 2015
I have a code that imports a text file [URL] .... and has a variety of methods for sorting through it. The file is structured differently when loaded into the environment, as each line begins with a movie and lists all of its actors. Each line has a movie title, then its release date in parentheses, and then the actors in the movie all listed and separated by slashes (Ex: /lastname 1, firstname 1/lastname 2, firstname 2/etc.....
Well I tried to create a method to search all the actors in the file for an inputted word and return the ones that have that word somewhere in their names. I managed to get it to work, but the code only runs for one line of it. How should I get this to do what its doing, but for EVERY line?
Code is here: [URL] ....
View Replies
View Related
Oct 12, 2014
I have to read data from a text file and print it in a new text file. An example of one line is like this:
Johnson 85 98 75 89 82
I then have to take the average of all the numbers and assign a "grade" to the numbers for each line of the text file and make a new file so it looks like this for 10 lines:
Name 1 2 3 4 5 Average Grade
Johnson 85 98 75 89 82 85.80 B
My problem is extracting the data from the file so I can use it.
View Replies
View Related
Dec 6, 2014
I am having issues insert each line of the simple textfile into a specific varible I want it to go to. For example my text file is ordered like this
Dallas
78 F
North, 15 mph
dallasimage
Denver
29 F
South, 10 mph
denverimage
and I want Dallas in city variable, 78f in temperature variable, and so on until text ends, at the moment is all goes into city variable, it all prints from there! I tried inserting it into an array but it would read all the lines previous to it in addition to reusing readline and all failed.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Textreader {
public static void main(String[] args) {
[code]....
View Replies
View Related
Apr 28, 2014
This is my input from my text file: 1|John Jay Smith|1985-01-10|2010-05-10|2014-03-05|212-222-2233..So, I have to convert 1 to integer. Break John Smith into first name as John and Middle name as Jay Last name as Smith.If no middle name is given then first is John and Last is Smith . Read 1985-01-10 as a date value . 2010-05-10 as another date value which could be null. 2014-03-05 as another date value which could be null. and at last 212-222-2233 as phone number.
Scanner sc = new Scanner (inFile);
sc.nextLine();
while (sc.hasNext()){
String row= sc.nextLine();
I want to read this line of input and assign the value accordingly.
View Replies
View Related
Jan 8, 2014
How can I change the value of
String str = "1234|U||45|||"
into
String str = "1234|U| |45| | |"
View Replies
View Related
Apr 20, 2014
Working on my java skills. The is Delimiter function is suppose to return true if there is a delimiter in the string and false otherwise. The main is suppose to call this function and count the number of words in the string:
Here is what I came up with:
import java.util.*;
public class NumberOfWordsInString {
public static void main (String[] args){
Scanner in= new Scanner (System.in);
String s;
[Code] ....
Somehow my method isn't being successfully called by my main and the following error msg was received:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
length cannot be resolved or is not a field
The method isDelimiter(char) in the type NumberOfWordsInString is not applicable for the arguments (char[])
at javaimprovements.NumberOfWordsInString.main(Number OfWordsInString.java:10)
line 10:
char ch [] = new char [s.length];
and the main doesn't call the isDelimiter function
View Replies
View Related
Feb 13, 2015
I am testing some stuff here is my code:
public static void main(String[] args) {
String[] a = new String[5];
//String input = JOptionPane.showInputDialog(null, "enter input");
Scanner scanner = new Scanner(System.in);
[Code] ....
I am trying to type in 5 strings from java console and print them all out. I am using a "/" as a delimiter. My problem is that it does not print any output after I type strings separated by "/" like this: hello/gea/cae/eaf/aer and press enter. It works if I use JOptionPane but from the console its not working. I want to use the console instead of JOptionPane for this one.
View Replies
View Related
Sep 4, 2014
I have a header in file like below:
EMP_ID|EMP_NAME|DEPARTMENT|SALARY|ACTIVE1
passed to a string
String test = "EMP_ID|EMP_NAME|DEPARTMENT|SALARY|ACTIVE1";
I have to check if the header has only alphanumeric and pipedelimiter is allowed.
Other than these i need to raise an error.
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
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 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