I have 45 mins to finish this. here is where I\'m at... any help would be apprec
ID: 3681744 • Letter: I
Question
I have 45 mins to finish this. here is where I'm at... any help would be appreciated. This is my second post :(
package project1;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
public class Main {
public static void main(String[] pArgs) {
Main mainObject = new Main();
mainObject.run();
}
private void run() {
ArrayList<Integer> list = new ArrayList<Integer>();
try {
Scanner scan = new Scanner(new File("src/project1/p01-in.txt"));
while (scan.hasNextInt()){
list.add(scan.nextInt());}
scan.close(); }
catch (FileNotFoundException e) {
e.printStackTrace();}
ArrayList<Integer> listRunsUpCount = new ArrayList<Integer>();
ArrayList<Integer> listRunsDnCount = new ArrayList<Integer>();
// listRunsUpCount ? FindRuns(list, RUNS_UP)
// listRunsUpCount ? FindRuns(list, RUNS_DN)
ArrayList<Integer> listRunsCount = new ArrayList<Integer>();
// listRunsCount ? Merge(listRunsUpCount, listRunsDnCount)
// Output("p01-runs.txt", listRunsCount)
}
Method FindRuns(ArrayList<Integers> pList, int pDir){
// Returns ArrayList of Integers
// listRunsCount ? arrayListCreate(pList.size(), 0)
int i = 0;
int k = 0;
While (i < pList.size()){
if(){ //If pDir is RUNS_UP and pList element at i is ? pList element at i + 1
k++;
}
else if (){ //ElseIf pDir is RUNS_DN and pList element at i is ? pList element at i + 1
k++;
}
else {
if(k !=0){
//Increment the element at index k of listRunsCount
k = 0;
}
i++;
}
if (k != 0){
//Increment the element at index k of listRunsCount
}
return listRunsCount;
}
Method Merge(pListRunsUpCount, pListRunsDnCount){
Returns ArrayList of Integers
listRunsCount ? arrayListCreate(pListRunsUpCount.size(), 0)
for (i = 0; i < pListRunsUpCount.size(); i++){
listRunsCount[i] = to the sum of the elements at i in pListRunsUpCount and pListRunsDnCount
}
return listRunsCount;
}
Method arrayListCreate(int pSize; int pInitValue){
Returns ArrayList of Integers
ArrayList<Integer> list = new ArrayList<Integer>();
for(i = 0; i < pSize; i++){
add(pInitValue) to list
}
return list;
}
void Output(pFilename; ArrayList<Integer> pListRuns ){
try {
Scanner scan = new Scanner(new File("src/project1/p01-out.txt"));}
catch (FileNotFoundException e) {
e.printStackTrace();}}
out.print("runs_total, ", the sum of pListRuns)
for (k = 1; k < pListRuns.size()){
out.print("runs_k, ", the element at index k of pListRuns)
}
out.close();
}
}
Explanation / Answer
word count problem in java
public class WordCounterProblem{ /* * This method return word count without using regular expression */ public int wordcount(String word) { if (word == null || word.isEmpty()) { return 0; } int count = 0; char ch[] = new char[word.length()]; for (int i = 0; i < word.length(); i++) { ch[i] = word.charAt(i); if (((i > 0) && (ch[i] != ' ') && (ch[i - 1] == ' ')) || ((ch[0] != ' ') && (i == 0))) { count++; } } returncount; } /* * Counting number of words using regular expression. */ public int countWord(String word) { if (word == null) { return 0; } String input = word.trim(); int count = input.isEmpty() ? 0 : input.split("\s+").length; return count; } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.