Hello guys I and my class mates are very new in Java, I found the syntaxes in java are long and kinda difficult to move on from C++. Now we are using Java and we are ask to program something and this part of the program just happens to be the problem. How can we read lines from a file(I know how to read files but on String only how about int and other data types?). Our instructor never teaches anything but for reading lines alone (as a string with each line containing values , it's pretty easy) so we advances to our study all by ourselves but with no luck on what he's asking.
How can I implement a read from a file such that my program could read multiple text in a single line
examples:
//this should be read from a file
56
100 100 100 100 100
How can I do this read this without reading it as string(Yes I know, parse it BUT I can't parse a 1 2 3 4 5)? I wanted the first line be read and assigned it as an integer variable
the next line with multiple values on single line must be assigned each as an int in an integer array.
I've actually made the program to do so and although this is a machine exercise I dun wanna post the problem to which this will be solve, all of us where just stuck on how to read multiple values on single line. I've watched some tutorials and scour the web I found it working but with a bit of errors on it. When I put error catcher it just pops an error means I done something really wrong.
If you want me to post my incredibly stupid program just lemme know. I am working on it for now.
But if you answered my question on how-to's it will be great! Because now I can focus on finishing my work.
Reading From File
Page 1 of 110 Replies - 950 Views - Last Post: 23 July 2011 - 08:00 AM
Replies To: Reading From File
#3
Re: Reading From File
Posted 23 July 2011 - 05:32 AM
Fantastic! Thanks you so much sire!
#4
Re: Reading From File
Posted 23 July 2011 - 05:36 AM
also you may want to look into String Tokenizer
And another useful feature is part of the String class called Split
And another useful feature is part of the String class called Split
#5
Re: Reading From File
Posted 23 July 2011 - 05:38 AM
String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
what is \\s* for? I assume that represent white space in Java? Or I could be wrong,
Thanks!
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
what is \\s* for? I assume that represent white space in Java? Or I could be wrong,
Thanks!
#6
Re: Reading From File
Posted 23 July 2011 - 05:42 AM
Patterns
you are correct.
you are correct.
Quote
Any character (may or may not match line terminators)
\d A digit: [0-9]
\D A non-digit: [^0-9]
\s A whitespace character: [ \t\n\x0B\f\r]
\S A non-whitespace character: [^\s]
\w A word character: [a-zA-Z_0-9]
\W A non-word character: [^\w]
\d A digit: [0-9]
\D A non-digit: [^0-9]
\s A whitespace character: [ \t\n\x0B\f\r]
\S A non-whitespace character: [^\s]
\w A word character: [a-zA-Z_0-9]
\W A non-word character: [^\w]
#7
Re: Reading From File
Posted 23 July 2011 - 05:54 AM
Thank you guys so much with this array of knowledge it gets a one leap from our discussion I'll review your post and links that you provide!
Thank you anyway it works now here is my code!
import java.io.*;
import java.util.*;
public class prob1 {
public static void main(String [] args) {
try{
Scanner a = new Scanner
( new File("THANK YOU SO MUCH FOR HELPING ME!"));
int i = 0;
int[] permutation = new int[8];
while(a.hasNextInt())
{
permutation[i] = a.nextInt();
i++;
}
}catch(Exception error){
System.out.println("Error on file read!");
}
}
}
with your your help I am able to speed ahead in my coding! Thanks! I'll take it from here! Heheh
Thank you so much and more power!
Thank you anyway it works now here is my code!
import java.io.*;
import java.util.*;
public class prob1 {
public static void main(String [] args) {
try{
Scanner a = new Scanner
( new File("THANK YOU SO MUCH FOR HELPING ME!"));
int i = 0;
int[] permutation = new int[8];
while(a.hasNextInt())
{
permutation[i] = a.nextInt();
i++;
}
}catch(Exception error){
System.out.println("Error on file read!");
}
}
}
with your your help I am able to speed ahead in my coding! Thanks! I'll take it from here! Heheh
Thank you so much and more power!
#8
Re: Reading From File
Posted 23 July 2011 - 06:24 AM
your while loop is really a for loop that could be written as such
for (int i=0; i<permutation.length; i++)
{
permutation[i] = a.nextInt();
}
#9
Re: Reading From File
Posted 23 July 2011 - 06:44 AM
There are two things to care here, first if there is a nextInt then the size of the array. So you have two options:
1. Put both conditions together, like:
2. Use arrayList instead of array if you are not sure of the size you will need. You may look them:
http://download.orac.../ArrayList.html
1. Put both conditions together, like:
while(a.hasNextInt() && i<permutation.length){
2. Use arrayList instead of array if you are not sure of the size you will need. You may look them:
http://download.orac.../ArrayList.html
#10
Re: Reading From File
Posted 23 July 2011 - 07:13 AM
@smohd thank you so much! Thanks I will check about that programming habit though. But I still have one problem. As I've said I need the first line to be assign in an integer variable. But somehow the loop scan the text file all through out. I am looking for the skip methods for now and maybe it could give me some answers. But for now I may use the trick of assigning the first element of the array in the integer variable and pop it out of equation! Heheh
Is it possible to "new line" the read after reading the first line? it seems my program pops error when reading in multiple lines. I am searching more on this from the link.
Thanks for the heads up though, actually I completely disagree with what my instructor said about loops are the same in Java. If I am not wrong, in functions yes, but it depends on how the programmer uses and defines it. For loops are used only when the number of loop to which the statement be executed is defined. Vs While loops is only used only when we do not know how many times should a statement be looped.
I forgot about this but thanks for the heads up!
I appreciate your kind answers! Thank you so much!
Is it possible to "new line" the read after reading the first line? it seems my program pops error when reading in multiple lines. I am searching more on this from the link.
Thanks for the heads up though, actually I completely disagree with what my instructor said about loops are the same in Java. If I am not wrong, in functions yes, but it depends on how the programmer uses and defines it. For loops are used only when the number of loop to which the statement be executed is defined. Vs While loops is only used only when we do not know how many times should a statement be looped.
I forgot about this but thanks for the heads up!
I appreciate your kind answers! Thank you so much!
#11
Re: Reading From File
Posted 23 July 2011 - 08:00 AM
I dont know what you are afraid of for example I have the file:
That code will read every number in its own. And every index of my array will contain its own number like:
every number in its index!
Quote
111 123
222 123 234 345
333
444
222 123 234 345
333
444
That code will read every number in its own. And every index of my array will contain its own number like:
Quote
111
123
222
123
234
345
333
444
123
222
123
234
345
333
444
This post has been edited by smohd: 23 July 2011 - 08:12 AM
Page 1 of 1

New Topic/Question
Reply


MultiQuote




|