Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: read integers not text

  1. #1
    Member
    Join Date
    Oct 2013
    Location
    Cyprus
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default read integers not text

    my code is below and i want to read only integers.BUT the text file is starting with text and it stops executing without reading the numbers.
    Also i want to add the 3 parameters of each line.
    Please help, thanks in advance.

     try{
            File fl = new File("C:/Users/Mario/Desktop/testing.txt");    
            //BufferedReader rd = new BufferedReader(new FileReader(fl));
            Scanner sc = new Scanner(fl).useDelimiter("\\s+");
            LinkedList<Integer> temps = new LinkedList<>();
            sc.useDelimiter(System.getProperty("line.separator"));
     
            while(sc.hasNext()){
             while(sc.next().matches("[0-9]+")){
                int num1=sc.nextInt();
                int num2=sc.nextInt();
                int num3=sc.nextInt();
                temps.add(num1+num2+num3);
     
             }
            }
     
            for(Integer dat:temps){
                System.out.println(dat);
            }
     
            }catch(Exception e){
                e.printStackTrace();
            }
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: read integers not text

    i want to read only integers.BUT the text file is starting with text
    That's the way files are read. The bytes are read in the order they are in. You need to read the file's bytes up to where the integers are located.
    There is an exception if the byte location in the file for the integers is known, then the RandomAccessFile can be used to start reading the file at that location.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to read multiple integers without assigning them to seperate variables?
    By gotdatdough in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2013, 07:20 PM
  2. Replies: 6
    Last Post: June 18th, 2012, 04:54 AM
  3. Reading text file and counting the number of words, integers, and floats.
    By Jsmooth in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: April 12th, 2012, 06:39 PM
  4. Replies: 0
    Last Post: October 29th, 2011, 02:37 AM
  5. [SOLVED] Writing Integers to .txt File; Returning Random Characters Instead of Integers
    By verbicidalmaniac in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 8th, 2011, 09:42 PM

Tags for this Thread