hello,i have a problem in my math project.

my project is to write a program that read set of element and its relation. the input data will be from text file.(Set<space>Relation).

{1,2,3} {(1,1),(2,2),(3,3),(1,2),(1,3),(2,3)}

i have no problem reading text file into the program but im stuck when i want to try to put the relation into the two dimension array.

for example

{(1,1),(2,2),(3,3),(1,2),(1,3),(2,3)}

the two dimension array have to be like this.

col[0][1][2]
[0] 1  1  1
[1]    1  1
[2]       1

i don't know how to set one into two dimension array because there are various relation in the text file.

does anyone knows how to solve this problem?

please,help me..

thanks~

Recommended Answers

All 5 Replies

stuck when i want to try to put the relation into the two dimension array.

Can you define what a "relation" is and how it's value is put into the array? You need to explain what goes into each row and column of the 2D array.

As far as I can see...
Your 2d array looks like
int[][] rel = new int[3][3];
then for each relation (i, j) you set the corresponding element to 1
rel[i][j] = 1;

The size of the array depends on the number of elements in the set, and you have to convert the elements of the set into 0-(n-1) array index values. Maybe the array should be boolean[][] and you set the value to true where there is a relation?

this is my coding.

import javax.swing.*;
import java.util.ArrayList;
import java.io.*;
import java.util.StringTokenizer;
import java.lang.*;
import java.util.*;

public class tests 
{
    public static int s1[][];
    public static int s2[][];
    public static int s3[][];
    public static int s4[][];
    public static int s5[][];
    public static int s6[][];
    public static int s7[][];
    public static int s8[][];
    public static int s9[][];
    public static int s10[][];

    public static void main(String[] args) throws IOException, FileNotFoundException
    {
        BufferedReader infile = null; 

        ArrayList arr1 = new ArrayList();
        ArrayList arr2 = new ArrayList(); 
        ArrayList arr3 = new ArrayList();
        ArrayList arr4 = new ArrayList();
        try
        {
              infile = new BufferedReader (new FileReader ("numbers.txt"));

              String indata = null;

              while ((indata = infile.readLine())!= null) 
              {
                     StringTokenizer st = new StringTokenizer(indata," ");
                     String set = st.nextToken();
                     arr1.add(set);
                     String relation = st.nextToken();
                     arr2.add(relation);
              } 

              for(int i =0; i < arr2.size(); i++)
              {
                  String r = arr2.get(i).toString();
                  String result = r.replaceAll("[{}(),; ]", "");
                  arr3.add(result);
              }

              for(int i = 0; i < arr3.size(); i++)
              {
                  System.out.println(arr3.get(i).toString());
              }

              for(int i =0; i < arr1.size(); i++)
              {
                  String s = arr1.get(i).toString();
                  String result = s.replaceAll("[{}(),; ]", "");
                  arr4.add(result);  
              }

              int set1 = Integer.parseInt(arr4.get(0).toString());
              String ss1 = arr4.get(0).toString();
              int a = ss1.length();
              s1 = new int[a][a];
              int sA[][];

              char arrA;
              char indOdd=' ',indEven=' ';

                 char[] cArr = arr3.get(0).toString().toCharArray();
                 //System.out.println(arr3.get(0).toString().length());
                 int l = arr3.get(0).toString().length();
                 int arr10[][] = new int[(l/2)][2];

                    for(int i=0;i< 2;i++)
                    {
                        for(int row = 0; row < (l/2);row++)
                        {
                            for(int gh = 0;gh < l;gh++)
                            {
                                if(i%2==0)
                                {
                                indEven = cArr[gh];
                                System.out.println(indEven);
                                arr10[row][i] = indEven;
                                //System.out.println(arr10[row][i]);
                                //row++;
                            }
                            else
                            {
                                indOdd = cArr[gh+1];
                                System.out.println(indOdd);
                                arr10[row][i] = indOdd;
                                //row++;
                            }
                         }
                        }
                    }  




        }
        catch (FileNotFoundException fnfe)
        {
               System.out.println("File not found");
        }
        catch (IOException ioe)
        {
               System.out.println(ioe.getMessage());
        }
        catch (Exception e)
        {
               System.out.println(e.getMessage());
        }

        infile.close();


    }
}

i hope anyone understands it and can solve my problem.~

I doubt that anyone will make the effort to understand code that has meaningless variable names and no comments.

You need to explain what the problem is. Show the program's output, explain what is wrong with it and show what the output should be.

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.