10 Replies - 950 Views - Last Post: 23 July 2011 - 08:00 AM Rate Topic: -----

#1 codercoder   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 12-July 11

Reading From File

Posted 23 July 2011 - 05:19 AM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Reading From File

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Reading From File

Posted 23 July 2011 - 05:29 AM

You may find this helpful. If you need more examples or help using what you find there, let us know.
Was This Post Helpful? 2
  • +
  • -

#3 codercoder   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 12-July 11

Re: Reading From File

Posted 23 July 2011 - 05:32 AM

Fantastic! Thanks you so much sire!
Was This Post Helpful? 0
  • +
  • -

#4 immeraufdemhund   User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

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
Was This Post Helpful? 1
  • +
  • -

#5 codercoder   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 12-July 11

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!
Was This Post Helpful? 0
  • +
  • -

#6 immeraufdemhund   User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: Reading From File

Posted 23 July 2011 - 05:42 AM

Patterns

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]

Was This Post Helpful? 1
  • +
  • -

#7 codercoder   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 12-July 11

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 :D

Thank you so much and more power!
Was This Post Helpful? 0
  • +
  • -

#8 immeraufdemhund   User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

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();
}


Was This Post Helpful? 1
  • +
  • -

#9 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1825
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

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:
 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
Was This Post Helpful? 1
  • +
  • -

#10 codercoder   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 12-July 11

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!
Was This Post Helpful? 0
  • +
  • -

#11 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1825
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

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:

Quote

111 123
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
every number in its index!

This post has been edited by smohd: 23 July 2011 - 08:12 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1