Results 1 to 3 of 3
  1. #1
    mik
    mik is offline Member
    Join Date
    Aug 2012
    Posts
    1
    Rep Power
    0

    Default File reading with Scanner

    Hi guys, thanks for taking the time to help me out!
    For those curious about the purpose of the code: I was assigned to create a program that converts a 32-bit binary string into an ipv4 address using java by separating the strings into 8-bit segments and converting them into decimal.
    Java Code:
    /**
     * @(#)ip.java
     *
     *
     * @author 
     * @version 1.00 2012/8/29
     */
     import java.util.Scanner;
     import java.io.*;
     import java.util.BitSet;
    public class ip {
    	public static int convert(BitSet barf)
    	{
    		int rvalue = 0;
    		int numset[]={128, 64, 32, 16, 8, 4, 2, 1};
    		for(int o=8; o<8; ++o)
    		{
    			if(barf.get(o))
    				rvalue+=numset[o];
    			
    		}
    		return rvalue;
    		
    	}
            
        /**
         * Creates a new instance of <code>ip</code>.
         */
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws IOException {
            // TODO code application logic here
            
            Scanner bleh = new Scanner(System.in);
            File filename = new File(bleh.nextLine());
            Scanner read =new Scanner(filename);
            int numaddr=read.nextInt();
            BitSet addr[] = new BitSet[numaddr];
            int epsy=0;
            
            for(int i=0; i<numaddr;++i)
            {
            	epsy=read.nextInt();
            	for(int z=0; i<32;++z)
            	{
            		if(epsy%10==1)
            		addr[i].set(z);
            		epsy/=10;
            	}
            }
            for(BitSet crum:addr)
            {
            	System.out.println(convert(crum.get(0, 8))+"."+convert(crum.get(8, 16))+"."+convert(crum.get(16, 24))+"."+convert(crum.get(24, 32)));
            }
          bleh.close();  
          read.close(); 
        }
    }
    throws the following error when run.
    Exception in thread "main" java.util.InputMismatchException: For input string: "11111111111111111111111111111111"
    at java.util.Scanner.nextLong(Unknown Source)
    at java.util.Scanner.nextLong(Unknown Source)
    at ip.main(ip.java:44)
    the file I'm feeding it contains the following
    4
    11111111111111111111111111111111
    00000011100000001111111111111111
    11110000101010100101010100000111
    10101010101010101010101010101010
    Thanks for your time, have a great day!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: File reading with Scanner

    That value is greater than Long.MAX_VALUE.

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: File reading with Scanner

    Junky's correct, though it would help if you showed the code that threw the exception as well.
    The code shown is using getInt, the code that threw that exception used getLong.

    Anyway, since you just want the "bits", read in the string and parse each character.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Scanner not reading every character in a file?
    By Brandonhspace in forum New To Java
    Replies: 5
    Last Post: 06-07-2012, 07:57 AM
  2. Reading file problem using Scanner
    By nfsmwbe in forum New To Java
    Replies: 18
    Last Post: 01-04-2012, 03:26 PM
  3. Java scanner reading txt documents
    By csisdifficult in forum New To Java
    Replies: 3
    Last Post: 04-21-2011, 08:24 PM
  4. Scanner reading accented characters
    By Phenomena in forum New To Java
    Replies: 2
    Last Post: 04-29-2010, 04:06 PM
  5. Reading pattern using Scanner class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 12:02 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •