Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 9th, 2013, 07:27 PM
lisa92 lisa92 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 43 lisa92 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 31 m 45 sec
Reputation Power: 2
Need help parsing (NoSuchElementException)?

This is a program that uses brute force to check if a equation is 2-sat or not. My problem however is with the parsing of the file. I get NoSuchElementException when I read this
int numV = input.nextInt();
Also, the main method was already given (except the last part where I call the brute method). The text file looks like this:
5
-1 4
-4 1
-1 -4
-3 -2

Code:
public class BruteForce {

    // Reads input from stdin or a specified file. Accepts an optional command-line argument,
    // a flag to indicate whether to use a simple brute-force solution
    // or not.
    public static void main(String[] args) {
        Scanner intScan;
        boolean bruteForce = true;

        Scanner inp = new Scanner(System.in);

        for (int i=0;i<args.length;i++) {
            if (args[i].equals("-nobrute"))
                bruteForce = false; // don't use brute force, use clever algorithm
            else {
                try {
                    inp = new Scanner(new File(args[i]));
                } catch(FileNotFoundException e) {
                    System.out.println("File not found: "+args[i]);
                    System.exit(1);
                }
            }
        }

        intScan = new Scanner(inp.nextLine());

        // then read in each disjunction
        while (inp.hasNextLine()) {
            String s = inp.nextLine();
            intScan = new Scanner(s);

            // read in a disjunction: (a \/ b) represented by 2 integers separated by whitespace
            // each variable is represented by a number in the range 1..numV, or -numV..-1
            // (negative numbers represent logically negated values)
            int a = intScan.nextInt();
            int b = intScan.nextInt();
            //System.out.println("Read "+a+" and "+b);
        }
        
        
        	 
        boolean is2SAT = brute(intScan);
        
        if(is2SAT){
        	System.out.println("This is a 2-SAT equation!");
        }
        
    }
    
 
    
    public static boolean brute(Scanner input) {
    	
     boolean evaluatable = true;
    
     //ERROR HAPPENS HERE
     // first read in number of vars involved
       int numV = input.nextInt();
     

        for (int j = 0; j < numV; j++) {
        
         String binaryRepresentation = Integer.toBinaryString(j);
        
         	for (int i = 0; i < numV && evaluatable; i++) {

         		
         		int iFirst = input.nextInt();
         		
         		int iSecond = input.nextInt();

         		// Assigns correct sign according to j:
         		// 10011 means true-false-false-true-true
         		if (binaryRepresentation.charAt(Math.abs(iFirst)-1) == '0') {
         			iFirst *= -1;
         		}
         		if (binaryRepresentation.charAt(Math.abs(iSecond)-1) == '0') {
         			iFirst *= -1;
         		}

         		evaluatable = evaluateAnd(iFirst, iSecond) && evaluateOr(iFirst, iSecond);

         	}
         	if (evaluatable) {
         		return true;
         	}
        }
     
        return false;
    }
    
    public static boolean evaluateOr(int a, int b) {
     return a > 0 || b > 0;
    }
    
    public static boolean evaluateAnd(int a, int b) {
    return a > 0 && b > 0;
   }
}

Reply With Quote
  #2  
Old February 9th, 2013, 11:13 PM
fjmva fjmva is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 1 fjmva User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 45 sec
Reputation Power: 0
send the java file

send the java file to fjohnnn at yahoo dot com i'll have a look at it.

Reply With Quote
  #3  
Old February 10th, 2013, 05:59 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,951 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 29 m 42 sec
Reputation Power: 345
Where do you get the exception? I see many calls to Scanner methods. Which one gives the error.

Reply With Quote
  #4  
Old February 10th, 2013, 10:14 AM
lisa92 lisa92 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 43 lisa92 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 31 m 45 sec
Reputation Power: 2
Code:

public static boolean brute(Scanner input) {
    	
     boolean evaluatable = true;
    
     //ERROR HAPPENS HERE
     // first read in number of vars involved
       int numV = input.nextInt();

Reply With Quote
  #5  
Old February 10th, 2013, 10:27 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,951 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 29 m 42 sec
Reputation Power: 345
Use one of the Scanner class's hasNext... methods to test that there is data to be read before trying to read it
Or change the input file so there is enough data for the program to read.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Need help parsing (NoSuchElementException)?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap