Results 1 to 3 of 3
Thread: File reading with Scanner
- 08-30-2012, 05:50 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 1
- Rep Power
- 0
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(); } }
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)
4
11111111111111111111111111111111
00000011100000001111111111111111
11110000101010100101010100000111
10101010101010101010101010101010
- 08-31-2012, 04:16 AM #2
Re: File reading with Scanner
That value is greater than Long.MAX_VALUE.
- 08-31-2012, 09:44 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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
-
Scanner not reading every character in a file?
By Brandonhspace in forum New To JavaReplies: 5Last Post: 06-07-2012, 07:57 AM -
Reading file problem using Scanner
By nfsmwbe in forum New To JavaReplies: 18Last Post: 01-04-2012, 03:26 PM -
Java scanner reading txt documents
By csisdifficult in forum New To JavaReplies: 3Last Post: 04-21-2011, 08:24 PM -
Scanner reading accented characters
By Phenomena in forum New To JavaReplies: 2Last Post: 04-29-2010, 04:06 PM -
Reading pattern using Scanner class
By Java Tip in forum Java TipReplies: 0Last Post: 01-18-2008, 12:02 PM
Bookmarks