Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given the below Java code in adding subsets (provided nicely by anonymous that a

ID: 3761489 • Letter: G

Question

Given the below Java code in adding subsets (provided nicely by anonymous that answered my original question). How would I read a given .txt set of 2 and 3 digint integers into the array, instead of using the preplaced ( 2, 6, 8, 5, 1 10), In other words, what is the Java code that could read a lengthy text file of integers into the array in order to use those numbers and having the .txt file replace the (2, 6, 8, 5, 1, 10) ?

Thanks!

Run2

=============================================

==============================================

Program name SubSet.java

==============================================

import java.util.Scanner;

public class SubSet {

   public static void main(String[] args) {

       // Declare set
       int S[] = { 2, 6, 8, 5, 1, 10 };
       int D,setNumber=0;

       Scanner sc = new Scanner(System.in);

       System.out.println("Enter D value: ");
       D = sc.nextInt();

       // logic to find 2 digit set
       // logic to find 3 digit set
       for (int i = 0; i < S.length; i++) {

           for (int j = i + 1; j < S.length; j++) {
              
               if ((S[i] + S[j]) == D) {

                   System.out.println("S"+(++setNumber)+"= {" + S[i] + "," + S[j] + "}");
               }
      
           }
       }
       // logic to find 3 digit set
       for (int i = 0; i < S.length; i++) {

           for (int j = i + 1; j < S.length; j++) {

               for (int k = j + 1; k < S.length; k++) {

                   if ((S[i] + S[j] + S[k]) == D) {
                       System.out.println("S"+(++setNumber)+"= {" + S[i] + "," + S[j] + ","+ S[k] + "}");
                   }
               }
           }
       }
      
       if(setNumber==0) {

           System.out.println("Solution subset does not exist");
       }
   }

}

EL Markers E Properties Servers Console × Data Source Explorer b Snipp SubSet [java Application] /opt/jdk1.7.0_21/bin/java (Oct 26, 2015 11:56:22 Enter D value: 9 51= {8,1} S2- 12,6,1)

Explanation / Answer

import java.util.Scanner;

public class SubSet {

   public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
       // Declare set
       int S[] = new int[100000];
       int D,setNumber=0;
        System.out.println("Enter file name: ");
       String fname = sc.nextLine();
       Scanner scanner = new Scanner(new File(fname));
        int i = 0;
    while(scanner.hasNextInt()){
    s[i++] = scanner.nextInt();
    }
    int len = i;
       System.out.println("Enter D value: ");
       D = sc.nextInt();

       // logic to find 2 digit set
       // logic to find 3 digit set
       for (int i = 0; i < len; i++) {

           for (int j = i + 1; j < len; j++) {
            
               if ((S[i] + S[j]) == D) {
                   System.out.println("S"+(++setNumber)+"= {" + S[i] + "," + S[j] + "}");
               }
    
           }
       }
       // logic to find 3 digit set
       for (int i = 0; i < len; i++) {

           for (int j = i + 1; j < len; j++) {

               for (int k = j + 1; k < len; k++) {

                   if ((S[i] + S[j] + S[k]) == D) {
                       System.out.println("S"+(++setNumber)+"= {" + S[i] + "," + S[j] + ","+ S[k] + "}");
                   }
               }
           }
       }
    
       if(setNumber==0) {
           System.out.println("Solution subset does not exist");
       }
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote