3 Replies - 565 Views - Last Post: 07 November 2010 - 04:52 PM Rate Topic: -----

#1 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

Getting numbers out of a file

Posted 07 November 2010 - 12:37 PM

Hello..

I have a question regarding getting numbers out of the file..
SO basically my file has numbers and text in it, therefore i am going thorugh the file character by character and i am using
a series of if-statements to see whteher chcaracter is a number, comma, carriage return or just a character.
When i come across a number using method Character.isDigit() it works fine for numbers from 0 - 9 but when there is number like 100 it doesnt read it.. how can i get passed my problem?

public int[][] readFile(){
		file = new File("somefile.txt");
		if(file.exists()){
			System.out.println("Exists");
		}
		FileInputStream inputFile;
		Reader reader;
		boolean isTrue = true;
		int i,j;
		i = -1;
		j = -1;
		
		try{
			inputFile = new FileInputStream(file);
			reader = new InputStreamReader(inputFile);
			int index;
			
			//the while loop;
			try {
				while((index = reader.read()) != -1){
					char current = (char) index;
					
					if(Character.isLetter(current)){
						//do nothing
//						System.out.print(c);
					}
					else if(Character.isDigit(current) && isTrue){
						String s = current + "";
						int x = Integer.parseInt(s);
						a = new int[x][x];
						isTrue = false;
					}
					else if(current == ','){
						j++;
					}
					else if(current == '\n'){
						i++;
					}
					else if(current == ' '){
						
					}
					else if(j > 7){
						j = 1;
					}
					else if(Character.isDigit(current)){
						String s = current + "";
						int value = Integer.parseInt(s);
//						System.out.println(i + " " + j);
						a[i][j] = value;
					}
					else{
						//do nothing
					}
					
				}//end while loop
			} catch (IOException e) {
				System.out.println("IOException occured");
			}
			
		}catch(FileNotFoundException e){
				System.out.println("Exception has occured");
		}
		
		return a;
		
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Getting numbers out of a file

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

Re: Getting numbers out of a file

Posted 07 November 2010 - 02:45 PM

Hello.. i posted this question before but i think i was not very clear what i meant.
I have a question on how to read a number from a file. (i need to process this number later on, but for now i need to get it out of the file). i can manage this task as long as number is between 0-9 but when it get bigger im confused.

Lets suppose my file consists of this:

i like 2,cookies,3,234,java

Then i could go through it character by character and see which value is a number.. however, if i do this then when i get to number 234 i am going to get 3 distinct numbers.. and i want to treat is a single number.. can anyone suggest what i can do in this case or is there any built in method to solve my problem.

I have tried to solve this by using DataInputStream readByte() method and cast it to characters but then i cannot read numbers greater than 9. Can anyone suggest a solution? Thank you in advance.
Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Getting numbers out of a file

Posted 07 November 2010 - 03:20 PM

It's difficult to recommend a possible BEST approach to a solution without knowing what you DON'T care about. Will you need to develop a solution that can retrieve all of the cases you've given in your sample:

1) numbers combined with alpha characters (single and multiple digits)
2) numbers by themselves (single and multiple digits)
3) a line of text with multiple possible delimiters

When I first looked at your problem and read that you're searching for '234' in a line of comma delimited items, I thought, "That would be simple using the split() method and searching for numbers," but I'm not sure the solution is that simple.

Read up on split() to see if that will help. Let us know more about your problem, and maybe we can be more helpful.
Was This Post Helpful? 1
  • +
  • -

#4 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Getting numbers out of a file

Posted 07 November 2010 - 04:52 PM

i'd use a scanner to read the file instead of a file writer. Scanner can read nextInt() instead of having to deal with the digit recognition stuff. Just my preference. If you want to go this route, you'll need to set up a loop to keep reading single digits til you reach a non digit character.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1