SlideShare a Scribd company logo
1 of 5
Download to read offline
i am finishing a project for my cs 0401 java course. I have completed everything for the project
that requires us to sort and then using a binary search display a txt file read through the command
prompt. For some reason when the program treads through the file the first element isnt sorted
and just ends up being printed in front of all the other sorted elements.
my code is:
import java.util.*;
import java.io.*;
public class Project4
{
static final int INITIAL_CAPACITY = 5;
public static void main( String args[] ) throws Exception
{
// ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON
THE COMMAND LINE
if (args.length < 1 )
{
System.out.println(" usage: C:> java Project4   "); // i.e. C:> java Project4
P4input.txt
System.exit(0);
}
// LOAD FILE INTO ARR USING INSERINORDER
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);
insertInOrder( arr, count++, infile.nextInt() );
}
infile.close();
arr=trimArr(arr,count); // Now count == .length
System.out.println( "Sorted array of " + arr.length + " ints from " + args[0] + " after
insertInOrder:" );
printArray( arr ); // we trimmed it thus count == length so we don't bother to pass in count
} // END MAIN
//
#############################################################################
###############################
// USE AS IS - DO NOT MODIFY
static void printArray( int[] arr )
{
for( int i=0 ; i= index + 1; i--)
{
arr[i] = arr[i - 1];
}
arr[index]=key; // LEAVE THIS HERE
}
}
static int bSearch(int[] a, int count, int key)
{
int hi = count-1;
int lo = 0;
int mid = (hi +lo)/2;
if(hi == -1)
{
return mid;
}
else
{
mid = (hi+lo)/2;
}
while (lo <= hi)
{
if (key==a[mid])
{
return (mid+1);
}
else if (key < a[mid])
{
hi = mid-1;
mid = (hi+lo)/2;
}
else
{
lo = mid +1;
mid = (hi+lo)/2;
}
}
return (mid +1);
}
}
and the .txt file has the elements:
Solution
HI, I have fixed the issue.
import java.util.*;
import java.io.*;
public class Project4
{
static final int INITIAL_CAPACITY = 5;
public static void main( String args[] ) throws Exception
{
// ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON
THE COMMAND LINE
if (args.length < 1 )
{
System.out.println(" usage: C:> java Project4   "); // i.e. C:> java Project4
P4input.txt
System.exit(0);
}
// LOAD FILE INTO ARR USING INSERINORDER
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);
insertInOrder( arr, count++, infile.nextInt() );
}
infile.close();
arr=trimArr(arr,count); // Now count == .length
System.out.println( "Sorted array of " + arr.length + " ints from " + args[0] + " after
insertInOrder:" );
printArray( arr ); // we trimmed it thus count == length so we don't bother to pass in count
} // END MAIN
//
#############################################################################
###############################
// USE AS IS - DO NOT MODIFY
static void printArray( int[] arr )
{
for( int i=0 ; i= index + 1; i--)
{
arr[i] = arr[i - 1];
}
arr[index]=key; // LEAVE THIS HERE
}
}
static int bSearch(int[] a, int count, int key)
{
int hi = count-1;
int lo = 0;
int mid = (hi +lo)/2;
if(hi == -1)
{
return mid;
}
while (lo <= hi)
{
if (key==a[mid])
{
return (mid+1);
}
else if (key < a[mid])
{
hi = mid-1;
}
else
{
lo = mid +1;
}
mid = (hi+lo)/2;
}
if(hi < 0)
return 0;
return (mid +1);
}
}
/*
Sample run:
Sorted array of 25 ints from input.txt after insertInOrder:
5 8 8 19 23 25 27 27 31 31 69 70 71 75 90 98 103 103 109 140 145 145 153 157 162
*/

More Related Content

Similar to i am finishing a project for my cs 0401 java course. I have complete.pdf

In Java- Input is from a file- The input is all doubles- You are to in.pdf
In Java- Input is from a file- The input is all doubles- You are to in.pdfIn Java- Input is from a file- The input is all doubles- You are to in.pdf
In Java- Input is from a file- The input is all doubles- You are to in.pdf
ColinjSWDavidsonn
 
PAGE 1Input output for a file tutorialStreams and File IOI.docx
PAGE  1Input output for a file tutorialStreams and File IOI.docxPAGE  1Input output for a file tutorialStreams and File IOI.docx
PAGE 1Input output for a file tutorialStreams and File IOI.docx
alfred4lewis58146
 
Write a java program that would ask the user to enter an input file .pdf
Write a java program that would ask the user to enter an input file .pdfWrite a java program that would ask the user to enter an input file .pdf
Write a java program that would ask the user to enter an input file .pdf
udit652068
 
Active Software Documentation using Soul and IntensiVE
Active Software Documentation using Soul and IntensiVEActive Software Documentation using Soul and IntensiVE
Active Software Documentation using Soul and IntensiVE
kim.mens
 
Program reads in a file of phone numbers without area codes .pdf
Program reads in a file of phone numbers without area codes  .pdfProgram reads in a file of phone numbers without area codes  .pdf
Program reads in a file of phone numbers without area codes .pdf
nitinarora01
 
Write a java program that allows the user to input the name of a file.docx
Write a java program that allows the user to input  the name of a file.docxWrite a java program that allows the user to input  the name of a file.docx
Write a java program that allows the user to input the name of a file.docx
noreendchesterton753
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
Shahjahan Samoon
 

Similar to i am finishing a project for my cs 0401 java course. I have complete.pdf (20)

• GUI design using drag and drop feature of IDE(Net beans), • File IO
•	GUI design using drag and drop feature of IDE(Net beans), •	File IO•	GUI design using drag and drop feature of IDE(Net beans), •	File IO
• GUI design using drag and drop feature of IDE(Net beans), • File IO
 
Complete the following Java code such that it opens a file named Fruit.docx
Complete the following Java code such that it opens a file named Fruit.docxComplete the following Java code such that it opens a file named Fruit.docx
Complete the following Java code such that it opens a file named Fruit.docx
 
In Java- Input is from a file- The input is all doubles- You are to in.pdf
In Java- Input is from a file- The input is all doubles- You are to in.pdfIn Java- Input is from a file- The input is all doubles- You are to in.pdf
In Java- Input is from a file- The input is all doubles- You are to in.pdf
 
PAGE 1Input output for a file tutorialStreams and File IOI.docx
PAGE  1Input output for a file tutorialStreams and File IOI.docxPAGE  1Input output for a file tutorialStreams and File IOI.docx
PAGE 1Input output for a file tutorialStreams and File IOI.docx
 
Write a java program that would ask the user to enter an input file .pdf
Write a java program that would ask the user to enter an input file .pdfWrite a java program that would ask the user to enter an input file .pdf
Write a java program that would ask the user to enter an input file .pdf
 
Lab4
Lab4Lab4
Lab4
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswar
 
JAVA Q2- Write a program that reads strings from the user and writes t.docx
JAVA Q2- Write a program that reads strings from the user and writes t.docxJAVA Q2- Write a program that reads strings from the user and writes t.docx
JAVA Q2- Write a program that reads strings from the user and writes t.docx
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
Active Software Documentation using Soul and IntensiVE
Active Software Documentation using Soul and IntensiVEActive Software Documentation using Soul and IntensiVE
Active Software Documentation using Soul and IntensiVE
 
Program reads in a file of phone numbers without area codes .pdf
Program reads in a file of phone numbers without area codes  .pdfProgram reads in a file of phone numbers without area codes  .pdf
Program reads in a file of phone numbers without area codes .pdf
 
Java practical
Java practicalJava practical
Java practical
 
CORE JAVA-1
CORE JAVA-1CORE JAVA-1
CORE JAVA-1
 
Write a java program that allows the user to input the name of a fil.docx
 Write a java program that allows the user to input  the name of a fil.docx Write a java program that allows the user to input  the name of a fil.docx
Write a java program that allows the user to input the name of a fil.docx
 
Write a java program that allows the user to input the name of a file.docx
Write a java program that allows the user to input  the name of a file.docxWrite a java program that allows the user to input  the name of a file.docx
Write a java program that allows the user to input the name of a file.docx
 
source code which create file and write into it
source code which create file and write into itsource code which create file and write into it
source code which create file and write into it
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
 
Code red SUM
Code red SUMCode red SUM
Code red SUM
 
Bhaloo
BhalooBhaloo
Bhaloo
 
Write a program that will count the number of characters- words- and l.docx
Write a program that will count the number of characters- words- and l.docxWrite a program that will count the number of characters- words- and l.docx
Write a program that will count the number of characters- words- and l.docx
 

More from deepaksatrker

Develop an encryption and decryption algorithm Your program should a.pdf
Develop an encryption and decryption algorithm  Your program should a.pdfDevelop an encryption and decryption algorithm  Your program should a.pdf
Develop an encryption and decryption algorithm Your program should a.pdf
deepaksatrker
 
Write an audit memo that explains how quality control for an audit e.pdf
Write an audit memo that explains how quality control for an audit e.pdfWrite an audit memo that explains how quality control for an audit e.pdf
Write an audit memo that explains how quality control for an audit e.pdf
deepaksatrker
 
What legal impacts did the ACA have on tax-exempt hospitalsSolu.pdf
What legal impacts did the ACA have on tax-exempt hospitalsSolu.pdfWhat legal impacts did the ACA have on tax-exempt hospitalsSolu.pdf
What legal impacts did the ACA have on tax-exempt hospitalsSolu.pdf
deepaksatrker
 
Which members of the family above are afflicted with Huntingtons Di.pdf
Which members of the family above are afflicted with Huntingtons Di.pdfWhich members of the family above are afflicted with Huntingtons Di.pdf
Which members of the family above are afflicted with Huntingtons Di.pdf
deepaksatrker
 
What are the benefits to firms of international diversification Wha.pdf
What are the benefits to firms of international diversification Wha.pdfWhat are the benefits to firms of international diversification Wha.pdf
What are the benefits to firms of international diversification Wha.pdf
deepaksatrker
 
What are electromagnetic wavesSolutionWe are encompassed by w.pdf
What are electromagnetic wavesSolutionWe are encompassed by w.pdfWhat are electromagnetic wavesSolutionWe are encompassed by w.pdf
What are electromagnetic wavesSolutionWe are encompassed by w.pdf
deepaksatrker
 
Using RustBecause postfix expressions are simpler and faster to ev.pdf
Using RustBecause postfix expressions are simpler and faster to ev.pdfUsing RustBecause postfix expressions are simpler and faster to ev.pdf
Using RustBecause postfix expressions are simpler and faster to ev.pdf
deepaksatrker
 
This file contains a complete array-based MultiSet, but not the code.pdf
This file contains a complete array-based MultiSet, but not the code.pdfThis file contains a complete array-based MultiSet, but not the code.pdf
This file contains a complete array-based MultiSet, but not the code.pdf
deepaksatrker
 
T 5. A contract should be held enforceable even if a party is mistake.pdf
T 5. A contract should be held enforceable even if a party is mistake.pdfT 5. A contract should be held enforceable even if a party is mistake.pdf
T 5. A contract should be held enforceable even if a party is mistake.pdf
deepaksatrker
 
Subject - Project ManagementDescribe what the WBS is and why you .pdf
Subject - Project ManagementDescribe what the WBS is and why you .pdfSubject - Project ManagementDescribe what the WBS is and why you .pdf
Subject - Project ManagementDescribe what the WBS is and why you .pdf
deepaksatrker
 

More from deepaksatrker (20)

Develop an encryption and decryption algorithm Your program should a.pdf
Develop an encryption and decryption algorithm  Your program should a.pdfDevelop an encryption and decryption algorithm  Your program should a.pdf
Develop an encryption and decryption algorithm Your program should a.pdf
 
Cystic fibrosis is caused by a recessive mutant allele (cf). Two norm.pdf
Cystic fibrosis is caused by a recessive mutant allele (cf). Two norm.pdfCystic fibrosis is caused by a recessive mutant allele (cf). Two norm.pdf
Cystic fibrosis is caused by a recessive mutant allele (cf). Two norm.pdf
 
Compute the addition and multiplication of the following 2 binary fl.pdf
Compute the addition and multiplication of the following 2 binary fl.pdfCompute the addition and multiplication of the following 2 binary fl.pdf
Compute the addition and multiplication of the following 2 binary fl.pdf
 
BIOCHEMList and briefly describe the six types of the 6 basic rec.pdf
BIOCHEMList and briefly describe the six types of the 6 basic rec.pdfBIOCHEMList and briefly describe the six types of the 6 basic rec.pdf
BIOCHEMList and briefly describe the six types of the 6 basic rec.pdf
 
Write an audit memo that explains how quality control for an audit e.pdf
Write an audit memo that explains how quality control for an audit e.pdfWrite an audit memo that explains how quality control for an audit e.pdf
Write an audit memo that explains how quality control for an audit e.pdf
 
What legal impacts did the ACA have on tax-exempt hospitalsSolu.pdf
What legal impacts did the ACA have on tax-exempt hospitalsSolu.pdfWhat legal impacts did the ACA have on tax-exempt hospitalsSolu.pdf
What legal impacts did the ACA have on tax-exempt hospitalsSolu.pdf
 
Which members of the family above are afflicted with Huntingtons Di.pdf
Which members of the family above are afflicted with Huntingtons Di.pdfWhich members of the family above are afflicted with Huntingtons Di.pdf
Which members of the family above are afflicted with Huntingtons Di.pdf
 
What could be the arguments of a production functionA Cost and La.pdf
What could be the arguments of a production functionA Cost and La.pdfWhat could be the arguments of a production functionA Cost and La.pdf
What could be the arguments of a production functionA Cost and La.pdf
 
What are the benefits to firms of international diversification Wha.pdf
What are the benefits to firms of international diversification Wha.pdfWhat are the benefits to firms of international diversification Wha.pdf
What are the benefits to firms of international diversification Wha.pdf
 
What are electromagnetic wavesSolutionWe are encompassed by w.pdf
What are electromagnetic wavesSolutionWe are encompassed by w.pdfWhat are electromagnetic wavesSolutionWe are encompassed by w.pdf
What are electromagnetic wavesSolutionWe are encompassed by w.pdf
 
Using RustBecause postfix expressions are simpler and faster to ev.pdf
Using RustBecause postfix expressions are simpler and faster to ev.pdfUsing RustBecause postfix expressions are simpler and faster to ev.pdf
Using RustBecause postfix expressions are simpler and faster to ev.pdf
 
tnt en Assignment Nickele Company reoorts net income of sae,4n0 in .pdf
tnt en Assignment Nickele Company reoorts net income of sae,4n0 in .pdftnt en Assignment Nickele Company reoorts net income of sae,4n0 in .pdf
tnt en Assignment Nickele Company reoorts net income of sae,4n0 in .pdf
 
This file contains a complete array-based MultiSet, but not the code.pdf
This file contains a complete array-based MultiSet, but not the code.pdfThis file contains a complete array-based MultiSet, but not the code.pdf
This file contains a complete array-based MultiSet, but not the code.pdf
 
The structure of a high temperature superconductor containing barium.pdf
The structure of a high temperature superconductor containing barium.pdfThe structure of a high temperature superconductor containing barium.pdf
The structure of a high temperature superconductor containing barium.pdf
 
The present value o.pdf
The present value o.pdfThe present value o.pdf
The present value o.pdf
 
The Associated Handles section in Resource Monitor can be used to sh.pdf
The Associated Handles section in Resource Monitor can be used to sh.pdfThe Associated Handles section in Resource Monitor can be used to sh.pdf
The Associated Handles section in Resource Monitor can be used to sh.pdf
 
T 5. A contract should be held enforceable even if a party is mistake.pdf
T 5. A contract should be held enforceable even if a party is mistake.pdfT 5. A contract should be held enforceable even if a party is mistake.pdf
T 5. A contract should be held enforceable even if a party is mistake.pdf
 
Subject - Project ManagementDescribe what the WBS is and why you .pdf
Subject - Project ManagementDescribe what the WBS is and why you .pdfSubject - Project ManagementDescribe what the WBS is and why you .pdf
Subject - Project ManagementDescribe what the WBS is and why you .pdf
 
Significant mass extinctions occurred during which of the following .pdf
Significant mass extinctions occurred during which of the following .pdfSignificant mass extinctions occurred during which of the following .pdf
Significant mass extinctions occurred during which of the following .pdf
 
SOLUTIONNotice thatt2becomes simpler when differentiated (wherease5t.pdf
SOLUTIONNotice thatt2becomes simpler when differentiated (wherease5t.pdfSOLUTIONNotice thatt2becomes simpler when differentiated (wherease5t.pdf
SOLUTIONNotice thatt2becomes simpler when differentiated (wherease5t.pdf
 

Recently uploaded

QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
httgc7rh9c
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food Additives
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 

i am finishing a project for my cs 0401 java course. I have complete.pdf

  • 1. i am finishing a project for my cs 0401 java course. I have completed everything for the project that requires us to sort and then using a binary search display a txt file read through the command prompt. For some reason when the program treads through the file the first element isnt sorted and just ends up being printed in front of all the other sorted elements. my code is: import java.util.*; import java.io.*; public class Project4 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println(" usage: C:> java Project4 "); // i.e. C:> java Project4 P4input.txt System.exit(0); } // LOAD FILE INTO ARR USING INSERINORDER 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); insertInOrder( arr, count++, infile.nextInt() ); } infile.close(); arr=trimArr(arr,count); // Now count == .length System.out.println( "Sorted array of " + arr.length + " ints from " + args[0] + " after insertInOrder:" ); printArray( arr ); // we trimmed it thus count == length so we don't bother to pass in count
  • 2. } // END MAIN // ############################################################################# ############################### // USE AS IS - DO NOT MODIFY static void printArray( int[] arr ) { for( int i=0 ; i= index + 1; i--) { arr[i] = arr[i - 1]; } arr[index]=key; // LEAVE THIS HERE } } static int bSearch(int[] a, int count, int key) { int hi = count-1; int lo = 0; int mid = (hi +lo)/2; if(hi == -1) { return mid; } else { mid = (hi+lo)/2; } while (lo <= hi) { if (key==a[mid]) { return (mid+1); } else if (key < a[mid]) {
  • 3. hi = mid-1; mid = (hi+lo)/2; } else { lo = mid +1; mid = (hi+lo)/2; } } return (mid +1); } } and the .txt file has the elements: Solution HI, I have fixed the issue. import java.util.*; import java.io.*; public class Project4 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println(" usage: C:> java Project4 "); // i.e. C:> java Project4 P4input.txt System.exit(0); } // LOAD FILE INTO ARR USING INSERINORDER Scanner infile = new Scanner( new File( args[0] ) );
  • 4. int[] arr = new int[INITIAL_CAPACITY]; int count= 0; while (infile.hasNextInt()) { if ( count==arr.length ) arr = upSizeArr(arr); insertInOrder( arr, count++, infile.nextInt() ); } infile.close(); arr=trimArr(arr,count); // Now count == .length System.out.println( "Sorted array of " + arr.length + " ints from " + args[0] + " after insertInOrder:" ); printArray( arr ); // we trimmed it thus count == length so we don't bother to pass in count } // END MAIN // ############################################################################# ############################### // USE AS IS - DO NOT MODIFY static void printArray( int[] arr ) { for( int i=0 ; i= index + 1; i--) { arr[i] = arr[i - 1]; } arr[index]=key; // LEAVE THIS HERE } } static int bSearch(int[] a, int count, int key) { int hi = count-1; int lo = 0; int mid = (hi +lo)/2; if(hi == -1) { return mid; }
  • 5. while (lo <= hi) { if (key==a[mid]) { return (mid+1); } else if (key < a[mid]) { hi = mid-1; } else { lo = mid +1; } mid = (hi+lo)/2; } if(hi < 0) return 0; return (mid +1); } } /* Sample run: Sorted array of 25 ints from input.txt after insertInOrder: 5 8 8 19 23 25 27 27 31 31 69 70 71 75 90 98 103 103 109 140 145 145 153 157 162 */