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

A Java program can have the following type of delimiters: {, }, (, ), [, and ].

ID: 3794359 • Letter: A

Question

A Java program can have the following type of delimiters: {, }, (, ), [, and ]. In a correct Java program, these delimiters must be properly nested. Think of each left delimiter {, (, and [ as opening a scope, and think of each right delimiter }, ), and ] as closing a scope opened by a corresponding left delimiter. A string of characters containing these delimiters has proper nesting of delimiters if each scope that is opened is eventually closed, and the scopes are opened and closed in a last-openedfirst-closed fashion. Write a program that reads a file containing Java source code and checks it for proper nesting of delimiters. Your program should read the source code from the file and print it to the screen. If the file is properly nested, all of it is printed to the screen and a message is printed that the file is
properly nested. If the file is not properly nested, then copying of the file to the screen stops as soon as improper nesting is detected, and your program prints a message that the file has errors. To simplify your task, you may assume these delimiters do not appear inside of comments and string literals, and that they do not appear in the program as character literals.

Explanation / Answer

package chegg;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Stack;

// class
public class DelimiterCheck {

   // main method
public static void main(String[] args) {

   // character stack define as st
   Stack <Character> st = new Stack<Character>();    
     
   try {
         
       // read MyFile.txt
   FileInputStream fileRead = new FileInputStream("MyFile.txt");
  
   char ch;
   int j=0;
   while (fileRead.available() > 0)
   {
   ch = (char) fileRead.read();
   j++;
     
   // use switch to perform action on delimiter
   switch (ch)
   {
   case '{':
   case '[':
   case '(':
   st.push(ch);
   break;
   case '}':
   case ']':
   case ')':
  
   if (!st.isEmpty())
   {
   char chpop = st.pop();
  
      
   if ((ch == '}' && chpop != '{') || (ch == ']' && chpop != '[') || (ch == ')' && chpop != '('))
   {
       System.out.println();
       System.out.println("-----------------");
       System.out.println("The file has errors. ");
       return ;
   }
      
   }
  
   default:
   break;
  
   }    
  
   System.out.print(ch);
   }
  
  
   System.out.println();
   System.out.println("-------the file is properly nested------");
   fileRead.close();
  
   }
   catch (IOException e)
   {
   e.printStackTrace();
   }
      
}
}
--------------------------------------------------------------------------------------------

MyText.File

package chegg;

import java.util.Scanner;

public class CheckSum {
  
   //main method
   public static void main(String[] args) {
      
       Scanner sc=new Scanner(System.in);
       //Enter the first 9 Digits of an ISBN integer :
       System.out.println("Enter the first 9 Digits of an ISBN integer : ");
      
       String d1to9=sc.next();       
      
       int d10=0;
      
       System.out.println("Digit 1 to 9 is is:"+ d1to9);
      
       for(int i=0;i<d1to9.length();i++)
   {
   char c=d1to9.charAt(i);
   d10= d10 + (i+1)*Character.getNumericValue(c);   //013601267 013031997
   }
      
       d10=d10%11;
      
       if(d10==10)
       {
       System.out.println("Checksum is 10 so d10 is X");
       System.out.println("The ISBN 10 number is: "+d1to9+'X');
       }
       else{      
       System.out.println("d10 is is: "+d10);
       System.out.println("The ISBN 10 number is: "+d1to9+d10);
       }      
   }
}

-----------------------------------------------

output :-

package chegg;

import java.util.Scanner;

public class CheckSum {
  
   //main method
   public static void main(String[] args) {
      
       Scanner sc=new Scanner(System.in);
       //Enter the first 9 Digits of an ISBN integer :
       System.out.println("Enter the first 9 Digits of an ISBN integer : ");
      
       String d1to9=sc.next();       
      
       int d10=0;
      
       System.out.println("Digit 1 to 9 is is:"+ d1to9);
      
       for(int i=0;i<d1to9.length();i++)
   {
   char c=d1to9.charAt(i);
   d10= d10 + (i+1)*Character.getNumericValue(c);   //013601267 013031997
   }
      
       d10=d10%11;
      
       if(d10==10)
       {
       System.out.println("Checksum is 10 so d10 is X");
       System.out.println("The ISBN 10 number is: "+d1to9+'X');
       }
       else{      
       System.out.println("d10 is is: "+d10);
       System.out.println("The ISBN 10 number is: "+d1to9+d10);
       }      
   }
}

-------the file is properly nested------

---------------------------------------------------------------------------------------------

If you have any query, please feel free to ask.

Thanks a lot.

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