0

input format thru a file

4

(6,7)(8,9)

5

(8,9)(6,7)

I wrote the following code to get all the integers stored in an array

public static void main(String[] args) throws IOException
{   
   
    int n=0;
    int arr[]= new int[13];
    Scanner s = new Scanner(new File("data.txt"));
    s.useDelimiter("[,() ]{1,}");
    
    while(s.hasNextInt())
    {   arr[n]=s.nextInt();
       
        n++;    
    }
    s.nextLine();
    
    while(s.hasNextInt())
    {   
        arr[n]=s.nextInt();
        n++;            
    }
    s.nextLine();
    
    while(s.hasNextInt())
    {   
        arr[n]=s.nextInt();
        n++;
    }
    s.nextLine();

    while(s.hasNextInt())
    {   
        arr[n]=s.nextInt();
        n++;
    }    

    n=0;
    while(n<9)
    { 
        System.out.println(arr[n]);
        n++;
    }
}

but it gives only those integers contained within the bracket.

Output

6

7

8

9

8

9

6

7

Desired Output-

4

6

7

8

9

5

8

9

6

7

How can I have 4 and 5 also in array?

Community
  • 1
  • 1
Manas
  • 1

1 Answers1

1

I'd use something like How do I create a Java string from the contents of a file? to get the contents as a string, then use

String[] items = fileContent.split(",()[]{}\\s");
Community
  • 1
  • 1