Hey,
Im trying to extract numbers from a scanner, but im having problems extracting them all.
Primarily i need it to

  • read a text file which contains some text and numbers.
  • go through the file and locate all numbers
  • differentiate between numbers contained in bracket (3), and those on their own 3.

    String[] anArray = {
    "0", "1", "2",
    "3", "4", "5",
    "6", "7", "8", "9"
    };

                while(scanfile.hasNext())
        {
            /* add counter up on digits 1-9 on each line
             * so numbers 1-9 are check on each line
             */
            for(int i =0;i<anArray.length;i++)
            {
                if(declarations.hasNext(anArray[i]){
                    System.out.println("number found");
                    scanfile.next();
                }
            }
    
            scanfile.nextLine();
        }
    

This is what iv done so far, it finds only one number per line, which is a problem, and if a combination such as 77 or 88 appears it doesent recognise them. Any ideas on a more efficient way to achieve the stated task?

This bit of code isn't particulary useful in showing us how you are decoding the data. Provide more source code please.

I actually got it working fairly easily using a delimeter but it still lacks the ability to split at a comma, any help correcting the delimeter to include comma's would be useful :)

file.useDelimiter("[^\\p{Alnum},\\.-]"); 

i tried the above, and it works perfectly except in cases when a comma is after a number, which it believes to not be an int I.e ,50 will be found using nextInt() but 50, will be read as a word for some reason.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.