Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Reading only integers from a file


  • Please log in to reply
6 replies to this topic

#1 Cruel Hand

Cruel Hand

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 195 posts
  • Programming Language:Java, Objective-C, Visual Basic .NET
  • Learning:C, Java, C++, Objective-C, PHP, (Visual) Basic, Python, JavaScript, Perl, Ruby, PL/SQL, Pascal, Assembly, Haskell

Posted 16 March 2012 - 03:42 PM

Hi again, this time I don't have a problem reading the file or finding it, I just need help with how to make the Scanner read only integers from the file, for example, if a double is in the file, it will skip that and keep reading integers.

My code thus far:
import java.util.Scanner;
import java.io.*;

public class Lab4Quest2 {
	public static void main(String[] args) throws IOException{
		File file = new File("C:/Users/Jake/workspace/Intro to OOP/src/integer.txt");
		Scanner input = new Scanner(file);
		String integer;
		int count = 0;
		System.out.println("Integers: ");
		
		while (input.hasNext()){
		integer = input.nextLine();
		System.out.println(integer);
		count++;
		}
		System.out.println("Total integers: " + count);
	}
}

I have everything right, I just need help with like if there is 9.6 in the file, it will skip that and move to the next item in the file.

thanks in advance :)
  • 0

#2 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 16 March 2012 - 03:54 PM

There is a hasNextDouble that you can play with it. But I would just read each token as a string and use a regex [0-9]+ and if that matches the string then it's an int.

while(fin.hasNext()) {
         String s = fin.Next();
         if (s.matches("[0-9]+")) integers++;
}

  • 0

#3 Cruel Hand

Cruel Hand

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 195 posts
  • Programming Language:Java, Objective-C, Visual Basic .NET
  • Learning:C, Java, C++, Objective-C, PHP, (Visual) Basic, Python, JavaScript, Perl, Ruby, PL/SQL, Pascal, Assembly, Haskell

Posted 16 March 2012 - 04:10 PM

it worked and ran perfectly, thank you.

Do you mind explaining a little more what the matches() method did there?

Did it just search the line of text in the file for only the characters 0-9 and if it contained anything else, it skipped it?

I just want to make sure I'm understanding the code before I use it.

thanks
  • 0

#4 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 16 March 2012 - 04:15 PM

matches is a regex function that checks if a string matches a certain pattern. The pattern I provided you does exactly what you said it does. It matyches anything that only contains the characters 0 to 9. The plus just means match 1 or more of the previous pattern; a digit in this case.

Does that help?
  • 0

#5 Cruel Hand

Cruel Hand

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 195 posts
  • Programming Language:Java, Objective-C, Visual Basic .NET
  • Learning:C, Java, C++, Objective-C, PHP, (Visual) Basic, Python, JavaScript, Perl, Ruby, PL/SQL, Pascal, Assembly, Haskell

Posted 16 March 2012 - 04:19 PM

yes, it does thank you. So for example, if you didn't have the plus, it would only print integers with one digit? The plus makes it possible to scan integers with multiple digits?
  • 0

#6 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 16 March 2012 - 04:20 PM

Yes. You got it! :D
  • -1

#7 Cruel Hand

Cruel Hand

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 195 posts
  • Programming Language:Java, Objective-C, Visual Basic .NET
  • Learning:C, Java, C++, Objective-C, PHP, (Visual) Basic, Python, JavaScript, Perl, Ruby, PL/SQL, Pascal, Assembly, Haskell

Posted 16 March 2012 - 04:26 PM

thank you very much! :)
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download