Reading Only Integers Not Text?
Feb 19, 2014
my code is below and i want to read only integers.BUT the text file is starting with text and it stops executing without reading the numbers.Also i want to add the 3 parameters of each line.
try{
File fl = new File("C:/Users/Mario/Desktop/testing.txt");
//BufferedReader rd = new BufferedReader(new FileReader(fl));
Scanner sc = new Scanner(fl).useDelimiter("s+");
LinkedList<Integer> temps = new LinkedList<>();
sc.useDelimiter(System.getProperty("line.separator"));
while(sc.hasNext()){
[code]...
View Replies
ADVERTISEMENT
Apr 24, 2015
I'm working on a simple text editor, and I'm currently saving the contents of my JTextPane in a file using an HTMLEditorKit (text is a JTextPane):
private void save() throws IOException {
int returnVal = fc.showSaveDialog(window);
if (returnVal == JFileChooser.APPROVE_OPTION) {
StyledDocument doc = (StyledDocument)text.getDocument();
HTMLEditorKit kit = new HTMLEditorKit();
BufferedOutputStream out;
[Code] ....
The problem I'm having is that after opening a file that I saved, it does not display (if I disable text/html, it displays the entire html code, but when I re-enable it, nothing displays at all.) Am I loading it wrong, or am I setting the JTextPane's text incorrectly? Or is it, perhaps, another error that I didn't catch?
View Replies
View Related
Mar 30, 2015
Trying to read integers from a text file and place them in a linked list in order. Program is compiling and running fine, just not the way I am intending when I am calling the methods. (Note: You have to enter the file location in the var x if you plan on running this.)
import java.io.*;
import java.util.*;
public class Node {
public int value;
public Node next;
Node(int x) {
this.value = x;
[code]....
View Replies
View Related
Feb 28, 2014
I'm trying to read a .txt file and put each line in a array of strings but it's not working! What is wrong with this code?He stops in "while" loop.
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.lang.Character;
import java.lang.String;
[code]...
View Replies
View Related
Apr 26, 2010
What I am trying to use this code for is to scan the files for a match that was typed with the corresponding name. here is what i have:
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
[code]...
View Replies
View Related
May 26, 2014
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.
View Replies
View Related
May 2, 2014
I'm trying to print the number of vertices in a text file on a graph which is the first integer in the file and then the type of graph is an undirected graph for a 0 or a 1 for a directed graph. Right now I'm just trying to print the number of vertices which is a 6 in the text file.
Here's the text file:
6,1
0,2 0,4 1,4 1,5 2,1 2,3 2,5 3,2 3,4 4,1 4,5 5,1 5,3 5,2
Im getting the following error:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 6, Size: 0
at java.util.ArrayList.rangeCheckForAdd(Unknown Source)
at java.util.ArrayList.add(Unknown Source)
at Vertices.main(Vertices.java:21)
Then I need to know how to go about reading the next line in the file, do you read it with the nextLine and put it into an arrayList or do I read each index in the graph?
public class Vertices
{
public static void main(String[] args) throws Exception
{
Scanner inFile = new Scanner(new File("graphs.txt"));
// Read the number of vertices
String line = inFile.nextLine();
ArrayList<Integer> list=new ArrayList<Integer>();
String[] data=line.split("[\,]");
int numberofvertices=Integer.parseInt(data[0]);
int typeOfGraph=Integer.parseInt(data[1]);
list.add(numberofvertices,typeOfGraph);
System.out.println("The number of vertices is " + numberofvertices);
}
}
View Replies
View Related
Mar 16, 2014
I have to write a program that reads the input from a text file!
Beware the Jabberwock, my son,
the jaws that bite, the claws that catch,
Beware the JubJub bird and shun
the frumious bandersnatch.
I should print the number of lines, the longest line, the number of tokens on each line and the length of the longest token on each line.
I was able to find the number of tokes in the line but im having having problems reading the longest word ,, my program gives me the number of letter of each line instead of only the number of letter of the longest word!!
this should be my output:
Line 1 has 5 tokens (longest = 11)
Line 2 has 8 tokens (longest = 6)
Line 3 has 6 tokens (longest = 6)
Line 4 has 3 tokens (longest = 13)
Longest line : the jaws that bite, the claws that catch,
import java.io.*;
import java.util.*;
public class InputStats{
public static void main (String [] args )
[Code] .....
View Replies
View Related
Jul 2, 2006
I am using a JFrame with JPanel (with radiobuttons and jtextfields) and am wanting to get data from a text file (around 100 words) and input 4 words at a time into my gui. I have been trying to do this by filling an string array with the data, (no problems here) then using jTextField to read the array. (having a few problems in this area though) Am about 80% there...but just wondering is there a better way to approach this problem, that is, inputing words into gui, refreshing, input 4 new words, refreshing, etc.
View Replies
View Related
May 2, 2014
I have a problem in reading the text file. I have my source text file at "D:/input.txt".When the below code is executed the following errors are coming.
" java.lang.NoClassDefFoundError: Try
Caused by: java.lang.ClassNotFoundException: Try
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
[Code] ....
View Replies
View Related
Apr 29, 2014
I'm working on a project for my Java class and I've got the code compiled and running, I just don't think that the program is reading the text file the way I want it to.
The text file looks like this:
1168,4.25,10
1305,1.80,42
1345,12.56,16
1388,7.42,30
1480,6.54,80
1495,8.36,48
1560,15.27,65
...
The first number being product number, second being price, and 3rd being quantity. I want to be able to enter the product number and have the Textfields for price and quantity be filled accordingly.
But when I enter a product number (e.g., '1168') no match for the item is being found, regardless of what I enter. Really stumped on this. Here's my code so far --
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Scanner;
public class Project3
{
private static Frame myframe;
private static TextField productCode;
private static TextField invNumberField;
[Code]...
View Replies
View Related
Feb 25, 2015
In my app, I'm reading a xml file (with JAXB) but there is times that I have empty segments. I put some examples :
<tuv> <seg>Unknown: the action taken should always be known</seg> </tuv>
Here I read well the tag "seg", I can get all text "Unknown: the action taken should always be known" but if this text is like this:
<tuv> <seg><bpt i="1" x-wb-tag="b1" />Unknown<ept i="1" x-wb-tag="/b1" />: the action taken should always be known</seg> </tuv>
I don't get anything, my variable is empty and I want to get all text "Unknown: the action taken should always be known"
Could I get this text
"<bpt i="1" x-wb-tag="b1" />Unknown<ept i="1" x-wb-tag="/b1" />: the action taken should always be known"
Or only "Unknown: the action taken should always be known" (without tag "ept" and "bpt"? (I want this like a string)
My variable's value is a "string" ....
View Replies
View Related
Aug 6, 2014
Show me a code where I can get text file all content to String?
View Replies
View Related
Apr 16, 2015
I have a file that has the following text in it:
# Main configuration file for DEDServer
# Starting resource values
startingGold=1000000000
startingElixir=1000000000
[Code] ....
View Replies
View Related
Mar 30, 2014
Followed this: [URL]
public static final String FILENAME="BinarySearchTree.txt";
....
public BinarySearchTree(TNode r) throws IOException {
root = r;
textfile = new File(FILENAME);
fw = new FileWriter(textfile.getAbsoluteFile());
bw = new BufferedWriter(fw);
br = new BufferedReader(new FileReader(FILENAME));
[code].....
In the constructor I create the BufferedReader using the FileReader and path to the input text file(in same dir as this project). When I am done reading I close it. In the debugger it is unable to read a single line, then goes to close the file.
View Replies
View Related
Apr 7, 2014
public void savePlants(ArrayList flowerPack) throws IOException
{
Scanner input = new Scanner(System.in);
String name;
[Code].....
When I open the saved file the information I need seems to be saved, but when I try to load and search it the data is not there. This is homework that was due about 2 days ago. I just want to get it right in my head for next time.
This is my text file: ¬í sr java.util.ArrayListxÒ™Ça I sizexp w sr FlowerNÏŠ¨r;¾ Z hasScentZ hasThornsL flowerColort Ljava/lang/String;xr Plant"ô²Ò0¢ I IDL Nameq ~ xp t Roset redsr Fungus“ +) Z isPoisonousL fungusColorq ~ xq ~ t toadstool t brownsr Weed #©éÇÙN Z isEdibleZ isMedicinalZ isPoisonousL weedColorq ~ xq ~ t dandylion t yellowsq ~ t tulip t pinkxq ~ q ~ q ~
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
Feb 15, 2014
This is sort of like the last problem I had but it's all about the formatting at the end. I have a file that reads something like:
Name
a bunch of text here
and perhaps a second
or even a third line
I need to output this as:
Name; "a bunch of text here and perhaps a second or even a third line"
Right now I find 'Name' and it outputs:
Namea bunch of text here and perhaps a second or even a third line
How would I add ";" and quotes in the middle? I can imagine that I may need to find name, then skip to the next line and just add name manually as the variable output of the search string.
This is where I am now
public static void main(String[] args) {
boolean output=false;
String name="";
String junk="CHAMBERS";
TextIO.putln("Search for a name");
name = TextIO.getln();
TextIO.readFile("doc.txt");
[Code] .....
So I'm almost there, but those dange double Names are killing me. I tried to add a bit of code where:
if (line.indexOf(name) >= 0){}
View Replies
View Related
May 15, 2014
I have my below Swing GUI code in which I want to read and display some contents present in text file say (test.txt) which is located in my local disk (C: est.txt). My pane tab 2 is divided into two halves horizontally. How can I read and write the test.txt file contents under the tab tab2 in the first half of the pane(where currently it is 2 written)?
Sample test.txt present in my local hard disk location
username:test1
Password:test1
DataBasename: testDB
CODE FOR TABBED PANE
import javax.swing.*;
import java.awt.*;
public class SplitPaneExp {
public static void main(String[] args){
Runnable r = new Runnable() {
public void run() {
JFrame frame = new JFrame("WELCOME");
[Code] ......
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 23, 2015
I am advised to use a while loop the Scanner method hasNextLine() and in the while loop body, call the Scanner method nextLine(), and add the returned String to the ArrayList of Strings. what I have gotten so far:
Scanner input = new Scanner(new File(""));
while(input.hasNextLine()) {
String line = input.nextLine();
System.out.println(line);
View Replies
View Related
Oct 21, 2014
I need to read from a text file given to us that has a list of books with authors names and book titles separated by an @ symbol for our delimiter. Here is the code I have right now, but it throws an ArrayIndexOutOfBoundsException at line 7...and I am unsure why?
import java.io.*;
import java.util.*;
public class Driver {
public static void main(String[] args) {
new Driver(args[0]);
[Code] ....
I realize that it must have something to do with my command line argument...but I am unsure what. Am I not entering the file name correctly?
View Replies
View Related
Mar 9, 2015
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] .....
View Replies
View Related
Apr 5, 2014
I have an assignment on sorting, i kno i can get the sorting down but im having an issue with inputing the 512 ints in a file into an array. the instructor provided us with a file with 4 equal sets of ints. i tried to make my array of size [scan.nextInt()] and it cuts off the last 21 ints. and skips the first int. how can i get all of the integers in the text file into my array? this is what i have so far. if i hard code the array to size 50000 and then try to print the array it compiles but errors out when running it.
System.out.println("Please Enter text file in this format, XXXXX.txt :");
String file =fileName.nextLine();
Scanner scan = new Scanner(new File(file));
int [] data = new int[scan.nextInt()]; <-------here it skips first int
int count= data.length;
for (int i=0; i<data.length-1;i++) {
data[i]=scan.nextInt();
}
System.out.print(Arrays.toString(data));
rst 4 ints in output are: 501, 257, 390, 478...., supposed to be 492,501,390....and last ints are: ....88, 83, 79, 0 and supposed to be :88 83 79 77 76 72 71 71 66 57 56 48 48 41 33 30 23 23 18 17 15 13 9....it replace last ints with 0. why ? and how do i fix this. attached it the text file
View Replies
View Related
May 4, 2015
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] ....
View Replies
View Related
Apr 29, 2015
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] ....
View Replies
View Related