Question

JAVA getting the following errors: Project4.java:93: error: ']' expected arr[index] = newVal; // LEAVE THIS HERE....

JAVA getting the following errors:

Project4.java:93: error: ']' expected
arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE
^
Project4.java:93: error: ';' expected
arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE
^
Project4.java:93: error: <identifier> expected
arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE
^
Project4.java:94: error: illegal start of type
return true;
^
Project4.java:98: error: class, interface, or enum expected
static int bSearch(int[] a, int count, int key)
^
Project4.java:101: error: class, interface, or enum expected
while(x <= j)
^
Project4.java:105: error: class, interface, or enum expected
if(k[i] == key)
^
Project4.java:108: error: class, interface, or enum expected
}
^
Project4.java:112: error: class, interface, or enum expected
}
^
Project4.java:116: error: class, interface, or enum expected
}
^
Project4.java:119: error: class, interface, or enum expected
}
^
11 errors

MY CODE:

import java.util.*;
import java.io.*;

public class Project4
{
static final int INITIAL_CAPACITY = 5;
  
public static void main( String args[] ) throws Exception
{
if (args.length < 1 )
{
System.out.println("ERROR: Must put input filename on cmd line\n");
System.exit(0);
}
  
Scanner infile = new Scanner( new File( args[0] ) );

int[] arr = new int[INITIAL_CAPACITY];
int count= 0;

while ( infile.hasNextInt() )
{
if ( count==arr.length ) arr = upSizeArr(arr);
if (insertInOrder( arr, count, infile.nextInt() ) )
       ++count;
}
  
arr=trimArr(arr,count); // Now count == .length
printArray( arr ); // we trimmed it thus count == length so we don't bother to pass in count
  
}
  
// ############################################################################################################
  
static void printArray( int[] arr )
{
for( int i=0 ; i<arr.length ;++i )
           System.out.print(arr[i] + " " );
System.out.println();
}
  
static int[] upSizeArr( int[] fullArr )
{
int[] upSizedArr = new int[ fullArr.length * 2 ];
for ( int i=0; i<fullArr.length ; ++i )
           upSizedArr[i] = fullArr[i];
return upSizedArr;
}
  
static int[] trimArr( int[] oldArr, int count )
{
int[] trimmedArr = new int[ count ];
for ( int i=0; i<count ; ++i )
           trimmedArr[i] = oldArr[i];
return trimmedArr;
}
  
// REMOVE ALL COMMENTS FROM INSERT IN ORDER JUST BEFORE HANDIN
static boolean insertInOrder( int[] arr, int count, int newVal )
{
int index = bSearch( arr, count, newVal); // REPLACE WITH -YOUR- BSEARCH

       if ( index >= 0 ) return false;
       boolean negative = false;
       for(int x = o; x < count; x++)
       {
           if(arr[x] >=newVal)
           {
               index = x;
               negative = true;
               break;
           }
       }
       if(negative)
       {
           for(int x = count; x > index; x--)
           {
               arr[x] = arr[x-1];
           }
          
       }
       else
       {
           index = count;
       }
       arr [index] = newVal;
       return true;
   }
arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE
       return true;
}
  
// REMOVE ALL COMMENTS FROM BSEARCH JUST BEFORE HANDIN
static int bSearch(int[] a, int count, int key)
{
       int x = 0, j= count-1;
       while(x <= j)
       {
           int i = x + (j-x)/2;
          
           if(k[i] == key)
           {
               return i;
           }
           if(k[i] < key)
           {
               x = i + 1;  
           }
           else
           {
               j=i -1;
           }
       }
return -1;
}
}

INPUT:

100 89 65 46 32 90 50 38 67 71 42 92 99 57 90 89 98 34 85 19 60 15 99 79 57

Expected OUtput:

15 19 32 34 38 42 46 50 57 60 65 67 71 79 85 89 90 92 98 99 100

The help is appreciated

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear Student,

Thanks for posting an interesting question to remove the errors you are getting while compiling above program.

1. While compilng the above program I was also getting the exactly the same error which you have shared. Please find below error screenshot for the same.G. C:\Windows\system32\cmd.exe DO X D:\>javac Project4.java Project4.java:89: error: l expected arr[index] = newal; // LEAV

2. After debugging the above program I figure out that your code require four changes.

3. The four changes I made in the code are present at Line No 65, Line No 85, Line No 101 and Line No 105. I put remarks infront of those line numbers what changes I made.

Note:- Don't confuse with the Line Numbers. You can see the line numbers in the below screenshot of the program. I also put remarks infront of the each of the line which require changes.

(a) At line no 65 the change I did is that you have initialized x with alphabet o rather than zero (0). So, please initialize it with 0.

(b) At Line No 85 the change I did is to remove the extra curly braces which is present over there. I commented out that curly braces or if you want you can remove it too.

(c) At Line No 101 the change I did is that we are getting array in variable a not in variable k. So please use variable a for array at if conditoin.

(d) At Line No 105 the change is similar for use variable a instead of k at if condition.

4. I put remarks starting with *MyComment infront of each change.

5. Please find below complete screenshot of the program with proper comments at each of the line where I made changes:-

3 tô - import java.util.*; import java.io.*; 1 2 4 5 public class Project 4 { static final int INITIAL CAPACITY = 5; 0 4 publarr=trimArr(arr,count); // Now count == .length printArray( arr); // we trimmed it thus count == length so we dont bother toreturn trimmedArr; coon // REMOVE ALL COMMENTS FROM INSERT IN ORDER JUST BEFORE HANDIN static boolean insertInOrder( int[] arelse index = count; //}//*MyComment second change comment this closing braces or remove this curly braces arr [index] = newva

6. Please find complete program in text format with proper comments at each of the line where I made changes:-

import java.util.*;
import java.io.*;

public class Project4
{
   static final int INITIAL_CAPACITY = 5;

   public static void main( String args[] ) throws Exception
   {
       if (args.length < 1 )
       {
           System.out.println("ERROR: Must put input filename on cmd line\n");
           System.exit(0);
       }

       Scanner infile = new Scanner( new File( args[0] ) );

       int[] arr = new int[INITIAL_CAPACITY];
       int count= 0;

       while ( infile.hasNextInt() )
       {
           if ( count==arr.length ) arr = upSizeArr(arr);
           if (insertInOrder( arr, count, infile.nextInt() ) )
               ++count;
       }

       arr=trimArr(arr,count); // Now count == .length
       printArray( arr ); // we trimmed it thus count == length so we don't bother to pass in count

   }

   // ############################################################################################################

   static void printArray( int[] arr )
   {
       for( int i=0 ; i<arr.length ;++i )
           System.out.print(arr[i] + " " );
       System.out.println();
   }

   static int[] upSizeArr( int[] fullArr )
   {
       int[] upSizedArr = new int[ fullArr.length * 2 ];
       for ( int i=0; i<fullArr.length ; ++i )
           upSizedArr[i] = fullArr[i];
       return upSizedArr;
   }

   static int[] trimArr( int[] oldArr, int count )
   {
       int[] trimmedArr = new int[ count ];
       for ( int i=0; i<count ; ++i )
           trimmedArr[i] = oldArr[i];
       return trimmedArr;
   }

   // REMOVE ALL COMMENTS FROM INSERT IN ORDER JUST BEFORE HANDIN
   static boolean insertInOrder( int[] arr, int count, int newVal )
   {
       int index = bSearch( arr, count, newVal); // REPLACE WITH -YOUR- BSEARCH

       if ( index >= 0 ) return false;
       boolean negative = false;
       for(int x = 0; x < count; x++) //*MyComment first change initialize x with 0 rather than alphabet o
       {
           if(arr[x] >=newVal)
           {
               index = x;
               negative = true;
               break;
           }
       }
       if(negative)
       {
           for(int x = count; x > index; x--)
           {
               arr[x] = arr[x-1];
           }

       }
       else
       {
           index = count;
           //}//*MyComment second change comment this closing braces or remove this closing curly braces
           arr [index] = newVal;
           return true;
       }
       arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE
       return true;
   }

   // REMOVE ALL COMMENTS FROM BSEARCH JUST BEFORE HANDIN
   static int bSearch(int[] a, int count, int key)
   {
       int x = 0, j= count-1;
       while(x <= j)
       {
           int i = x + (j-x)/2;

           if(a[i] == key) //*MyComment third change use array a instead of array k
           {
               return i;
           }
           if(a[i] < key) //*MyComment fourth change use array a instead of array k
           {
               x = i + 1;
           }
           else
           {
               j=i -1;
           }
       }
       return -1;
   }
}

7. Please find below output screenshot of the program.

Note:- Please keep in mind that you are reading the numbers from a file. Hence, I place all the above numbers inside a text file called input.txt and place this file at the same location where our Project4.java file is present. While running the program I am giving the name of the file input.txt as the command line arguments.

C. C:\Windows\system32\cmd.exe D:\>javac Project4.java D:\>java Project input.txt 15 19 32 34 38 42 46 50 57 60 65 67 71 79 8

8. In the above output screenshot you can see that we are getting the same output what you are expecting as mentioned in the question.

9. Though I put comments and tried to explain each of the changes. Still if you have any query please feel free to reach me through comments section. I will be more than happy to help you.

10. If you like my answer, please upvote it by pressing thumb's up.

Thanks!!

Happy Learning!!

Add a comment
Know the answer?
Add Answer to:
JAVA getting the following errors: Project4.java:93: error: ']' expected arr[index] = newVal; // LEAVE THIS HERE....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr,...

    Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...

  • Why am I getting compile errors. rowPuzzle.java:9: error: class, interface, or enum expected public static boolean...

    Why am I getting compile errors. rowPuzzle.java:9: error: class, interface, or enum expected public static boolean rowPuzzle(ArrayList<Integer> squares, int index, ArrayList<Boolean> visited) ^ rowPuzzle.java:16: error: class, interface, or enum expected } //Java Program import java.util.*; // rowPuzzle helper function implementation // File: rowPuzzle.cpp // Header files section // function prototypes public static boolean rowPuzzle(ArrayList<Integer> squares, int index, ArrayList<Boolean> visited) { // base case // return true if the puzzle is solvable if (index == squares.size() - 1) {    return...

  • Java Programming: The following is my code: import java.util.Arrays; public class KWArrayList<E> {    // Data...

    Java Programming: The following is my code: import java.util.Arrays; public class KWArrayList<E> {    // Data fields    /** The default initial capacity */    private static final int INITIAL_CAPACITY = 10;       /** The underlying data array */    private E[] theData;       /** The current size */    private int size = 0;       /** The current capacity */    private int capacity = 0;       @SuppressWarnings("unchecked")    public KWArrayList() {        capacity...

  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt...

    I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt Project#3 is an extension of the concepts and tasks of Lab#3. You will again read the dictionary file and resize the array as needed to store the words. Project#3 will require you to update a frequency counter of word lengths every time a word is read from the dictionary into the wordList. When your program is finished this histogram array will contain the following:...

  • Execute your program like this: C:\> java Lab4 10000ints.txt 172822words.txt Be sure to put the ints...

    Execute your program like this: C:\> java Lab4 10000ints.txt 172822words.txt Be sure to put the ints filename before the words filename. The starter file will be expecting them in that order. Lab#4's main method is completely written. Do not modify main. Just fill in the methods. Main will load a large arrays of int, and then load a large array of Strings. As usual the read loops for each file will be calling a resize method as needed. Once the...

  • Java : This function is to search through a binary tree left and right and return...

    Java : This function is to search through a binary tree left and right and return a count of the nodes above depth k. This is what I have so far, but I'm getting a Null pointer exception. public class MyIntSET {    private Node root;    private static class Node {        public final int key;        public Node left, right;        public Node(int key) { this.key = key; }    }    public int sizeAboveDepth(int...

  • Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public...

    Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public class Matrix3 { private double[][] matrix; /** * Creates a 3x3 matrix from an 2D array * @param v array containing 3 components of the desired vector */ public Matrix3(double[][] array) { this.matrix = array; } /** * Clones an existing matrix * @param old an existing Matrix3 object */ public Matrix3(Matrix3 old) { matrix = new double[old.matrix.length][]; for(int i = 0; i <...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT