2 Replies - 273 Views - Last Post: 31 July 2013 - 01:39 PM Rate Topic: -----

#1 i_am_become_time  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-July 13

Scanner Errors with Text File

Posted 31 July 2013 - 09:59 AM

I'm running into an issue with Scanner in which it works fine when using System.in and entering the text into the console, but it throws an exception error. Here's my code:

/**
 *  reddit DailyProgrammer Challenge #131
 *  "Who Tests the Tests?"
 */


import java.util.*;

public class TestTheTest {
	
	public void test(){
		Scanner s = new Scanner(System.in);
		int lines = s.nextInt();
		System.out.println("Number of lines tested: " + lines);
		while(s.hasNext()){
			int type = Integer.parseInt(s.next());
			String s1 = s.next();
			String s2 = s.next();
			if (stringTest(type, s1, s2)){
				System.out.println("Data is good!");
			}
			else System.out.println("Data is bad, test fails.");	
		}
		s.close();
	}
	
	//Tests the strings based on type and returns true for a match
	private boolean stringTest(int type, String s1, String s2) {
		boolean test = false;
		if (type == 0){
			int length = s1.length();
			String s1Reverse = "";
			for (int i  = 1; i <= length; i++){
				s1Reverse += s1.charAt(length - i);	
			}
			if (s2.equals(s1Reverse)){
				test = true;
			}
		}
		if (type == 1){
			if (s2.equals(s1.toUpperCase())){
				test = true;
			}
		}
		return test;
	}

	public static void main(String[] args){
		
		TestTheTest test = new TestTheTest();
		test.test();	
	}
}



When I change System.in to the file name, I get the following:

Quote

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at TestTheTest.test(TestTheTest.java:13)
at TestTheTest.main(TestTheTest.java:51)


I don't understand the inconsistency considering that the exact same text is being read (I think).

Is This A Good Question/Topic? 0
  • +

Replies To: Scanner Errors with Text File

#2 ConciselyVerbose  Icon User is offline

  • D.I.C Regular

Reputation: 90
  • View blog
  • Posts: 315
  • Joined: 05-July 13

Re: Scanner Errors with Text File

Posted 31 July 2013 - 10:53 AM

It sounds like you are just doing

new Scanner("filename.txt");


Try doing

new Scanner( new File("filename.txt"));


If I am wrong and you are Doing this correctly, can you show us what the file looks like?
Was This Post Helpful? 0
  • +
  • -

#3 i_am_become_time  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-July 13

Re: Scanner Errors with Text File

Posted 31 July 2013 - 01:39 PM

View PostConciselyVerbose, on 31 July 2013 - 10:53 AM, said:

It sounds like you are just doing

new Scanner("filename.txt");


Try doing

new Scanner( new File("filename.txt"));


If I am wrong and you are Doing this correctly, can you show us what the file looks like?


You are correct. Thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1