Mapreduce-java: calcutaing average for array list - java

I have and assignment for mapreduce and I am very new in mapreduce programming.
I want to calculate for each year and specific city what is average, min and max values.
so here is my sample input
Calgary,AB,2009-01-07,604680,12694,2.5207754,0.065721168,0.025668362,0.972051954,0.037000279,0.022319018,,,0.003641149,,,0.002936745,,,0.016723641
Calgary,AB,2009-12-30,604620,12694,2.051769654,0.060114973,0.034026918,1.503277516,0.054219005,0.023258217,,,0.00354166,,,0.003361414,,,0.122375131
Calgary,AB,2010-01-06,604680,12266,4.015745522,0.097792741,0.032738892,0.368454554,0.019228992,0.032882053,,,0.004778065,,,0.003190444,,,0.064203865
Calgary,AB,2010-01-13,604680,12551,3.006492921,0.09051656,0.041508534,0.215395047,0.012081755,0.023706119,,,0.004231772,,,0.003083003,,,0.155212503
I know how to find city and year
I am using this code:
String line = value.toString();
String[] tokens = line.split(",");
String[] date = tokens[2].split("-");
String year = date[0];
String location = tokens[0];
Now I want to find these two numbers in each line (e.g.2.5207754,0.065721168 , not exactly the same but all the number after third and fourth comma) and find an average, a min and max.
and in the output should looks like this:
Calgary 2009 average: "" , min; "" , max: ""
Calgary 2010 average: "" , min; "" , max: ""
I was trying to use this code to find the values in each line but because the data set is in not the same in the each line I got error (in the part there is no data or bigger that this length)
float number = 0;
float number2 = 0 ;
char a;
char c;
a = line.charAt(34);
c = line.charAt(44);
if (a == ',')
{
number = Float.parseFloat(line.substring(35, 44));
}
else
{
number = Float.parseFloat(line.substring(35, 46));
}
if (c == ',')
{
number2 = Float.parseFloat(line.substring(45, 56));
} else
{
number = Float.parseFloat(line.substring(47, 58));
}
Text numbers = new Text(number + " " + number2 + " ");
Then I was trying to use this code and the same as above it doesn't work:
String number = tokens[4];
String number2 = tokens[5];
so can you help me to do this project?

Looking at your input, it seems your records are separated by space. You can first split using " " and then get the individual values and use them for calculation
String[] arr = line.split(" ");
for(String val : arr){
String[] dataArr = val.split(",");
String city = dataArr[0];
String date = dataArr[2];
String v1 = dataArr[5];
String v2 = dataArr[6];
System.out.println("city: "+city +" date: "+ date +" v1: "+ v1+"v2: "+ v2);
}
city: Calgary date: 2009-01-07 v1: 2.5207754v2: 0.065721168
city: Calgary date: 2009-12-30 v1: 2.051769654v2: 0.060114973
city: Calgary date: 2010-01-06 v1: 4.015745522v2: 0.097792741
city: Calgary date: 2010-01-13 v1: 3.006492921v2: 0.09051656
city: Calgary date: 2009-01-07 v1: 2.5207754v2: 0.065721168

Related

Sting manipulation won't print full output to input

I have code for string manipulation but the output only generates the first name and not the rest of the input. I don't get any errors so I'm not sure what I'm doing wrong. Can someone show point out what I'm doing wrong if so and help me fix it so the output shows everything?
The expected input and output is for first name, last name, full name, upper case full name, lower case full name, number of vowels, number of consonants, and a few sentences plus the date.
Here is the code -
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class StringManipulation {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter first name: Bk ");
String fName = input.next();
System.out.println("Enter last name: Codeman");
String lName = input.next();
String UserFullName = fName.concat(" ").concat(lName);
System.out.println("\nHello " + UserFullName);
System.out.println("Length of name: " + UserFullName.length());
UserFullName = UserFullName.toUpperCase();
System.out.println("\nIn Upper Case: " + UserFullName);
UserFullName = UserFullName.toLowerCase();
System.out.println("In Lower Case: " + UserFullName);
//Counter variable to store the count of vowels and consonant
int vCount = 0, cCount = 0;
for(int i = 0; i < UserFullName.length(); i++) {
//Checks whether a character is a vowel
if(UserFullName.charAt(i) == 'a' || UserFullName.charAt(i) == 'e' || UserFullName.charAt(i) == 'i' || UserFullName.charAt(i) == 'o' || UserFullName.charAt(i) == 'u') {
//Increments the vowel counter
vCount++;
}
//Checks whether a character is a consonant
else if(UserFullName.charAt(i) >= 'a' && UserFullName.charAt(i)<='z') {
//Increments the consonant counter
cCount++;
}
}
System.out.println("\nNumber of vowels: " + vCount);
System.out.println("Number of consonants: " + cCount);
String text = "I am a very good student who works hard";
System.out.println("\nText: " + text);
System.out.println("At Position 10 of Text: " + text.charAt(10));
System.out.println("good starts at position: " + text.indexOf("good"));
System.out.println("good ends at position: " + (text.indexOf("good") + "good".length()-1));
System.out.println("\nEnter the word Excellent");
String word = input.next();
while(!word.equals("Excellent")) {
System.out.println("Incorrect, enter again");
word = input.next();
}
System.out.println("Good job");
Date date = new Date();
SimpleDateFormat formatter;
String strDate;
System.out.println("\nCurrent Date and Time");
formatter = new SimpleDateFormat("MMMM dd, yyyy");
strDate = formatter.format(date);
System.out.println(strDate);
formatter = new SimpleDateFormat("h:mm a");
strDate = formatter.format(date);
System.out.println(strDate);
input.close();
}
}
I happen to run your program and i have no errors and also the desired output was obtained can you check again or please inform if this ain't the output you seek
Enter first name: Bk
Stannars
Enter last name: Codeman
Jose
Hello Stannars Jose
Length of name: 13
In Upper Case: STANNARS JOSE
In Lower Case: stannars jose
Number of vowels: 4
Number of consonants: 8
Text: I am a very good student who works hard
At Position 10 of Text: y
good starts at position: 12
good ends at position: 15
Enter the word Excellent
Excellent
Good job
Current Date and Time
May 31, 2021
11:19 AM

Simple java program that finds the number of words in a sentence

public static String number(String words) {
int length = words.length;
int total = 0;
while(int index < length) {
total = total + 1;
index = index + 1;
}
}
String output;
output = total + " word";
return output;
}
Example output for this would be:
numberOfWords("Hello whats up?")
3 word
This would work for all proper sentences but I have to account for bad input for example:
"Hi my name is bob"
, this would be like thirty plus words. Also
" "
, should be 0 words. Is there any simple way to make the first example to "hi my name is bob" ?
you can do something like this :
String trimmed = text.trim();
int words = trimmed.isEmpty() ? 0 : trimmed.split("\\s+").length;
or (simplest way):
use str.replaceAll("\\s+"," ");
Simplest will work in every case
String word = "Hi my name is bob";
word = word.replaceAll("\\s+", " ");
String count [] = word.split(" ");
System.out.println(count.length);

For loop that prints from array

I am trying to print out two separate exam marks for students using a text file.
Below is a screenshot of the text file:
The first column is the student ID and the following two columns are both exam marks
Below is my code that does that calculations and prints:
//for loop that does calculations and prints
for(int i = 0; i < arraySize;i++){
String markOneFull = studentExamOneArray[i];
String markOneString = markOneFull.substring(5,7);
double markOne = Double.parseDouble(markOneString);
examOneNoID[i]= markOne;
String markTwoFull = studentExamTwoArray[i];
String markTwoString = markTwoFull.substring(8,10);
double markTwo = Double.parseDouble(markTwoString);
examTwoNoID[i] = markTwo;
/* String markThreeFull = studentExamThreeArray[i];
String markThreeString = markThreeFull.substring(5,10);
double markThree = Double.parseDouble(markThreeString);
examThreeNoID[i] = markThree;
String markFourFull = studentExamFourArray[i];
String markFourString = markFourFull.substring(5,10);
double markFour = Double.parseDouble(markFourString);
examFourNoID[i] = markFour;
*/
// Aggregate Mark
double aggregate = (examOneNoID[i] + examTwoNoID[i])/2;
DecimalFormat oneDigit = new DecimalFormat("#,##0.0");
//time to write
write.println(studentArray[i] + "\nAB101: " + examOneNoID[i] + " " + " AB102: " + examTwoNoID[i] + " Overall Mark: " + oneDigit.format(aggregate));
write.println("----------------------------------------");
}
write.close();
}
When i run the program i get an error message saying:
Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at MarksProcessing.main(MarksProcessing.java:42)
Try something like this:
Scanner sc = new Scanner(new File(fileName));
int i = 0;
while (sc.hasNext())
{
students[i] = sc.nextInt();
studentExamOneArray[i] = sc.nextDouble();
studentExamTwoArray[i] = sc.nextDouble();
double aggregate = (studentExamOneArray[i] + studentExamTwoArray[i])/2;
DecimalFormat oneDigit = new DecimalFormat("#,##0.0");
System.out.println("Student: " + students[i] + " FirstGrade: " + studentExamOneArray[i] + " SecondGrade: " + studentExamTwoArray[i] + " Overal: " + oneDigit.format(aggregate));
i++;
}
Now, you don't need substring and so on. You know the structure of file with grades or whatever that file is. And you don't need extra arrays (you use one for strings and then one for doubles and so on). Just read the numbers into array, then do the math and print.
I think, the specified problem can be easily handled . As we can see that here we are parsing string type into different kind of datatypes , so we should try to catch a Number Format exception.

The string is getting the wrong value

I am trying to build a string like 11 11 but I am facing problem I am getting for start the following string 98 11 and not 11 11.
How can I fix that?
I appreciate any help.
Character number = newName.charAt(2); //here number is 1
Character numberBefore = newName.charAt(1); //here numberBefore is 1
try (PrintWriter writer = new PrintWriter(path+File.separator+newName);
Scanner scanner = new Scanner(file)) {
boolean shouldPrint = false;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if(numberBefore >0 ){
String start= number+number+" "+number+number; //here start is `98 11`
}
Yes, this is due to the associativity of +.
This:
String start= number+number+" "+number+number;
is effectively:
String start = (((number + number) + " ") + number) + number;
So you're getting number + number (which is performing numeric promotion to int) and then string concatenation.
It sounds like you want:
String numberString = String.valueOf(number);
String start = numberString + numberString + " " + numberString + numberString;
Or alternatively:
String start = String.format("%0c%0c %0c%0c", number);
yes this is because of associativity of +
you can try the below code also
String c1 =Character.toString(number);
String s =c1+c1+" "+c1+c1;
String newName = "111";
Character number = newName.charAt(2); // here number is 1
Character numberBefore = newName.charAt(1); // here numberBefore is 1
if (Character.getNumericValue(numberBefore) > 0) { // checking against numeric rather than ascii
System.out.println("ASCII value of char " + (int) number); // ASCII code for '1' = 49
String start = String.valueOf(number) + String.valueOf(number) + " " + number + number; // here start is `98 11`
System.out.println(start);
}
}

Java .split() not working

I am trying to split a line from a text file. The text file is being imported from an inventory system and I don't know how to make it formatted with a tabular format. I can't tell you what is in the file but I will explain what I'm talking about. Confidentiality...
Line 1:
name order sorder assemblyID desc date
123 123 123 1-2-3 123-456-789 12-3 1\2\3
Line 2:
123 123 123 1-2-3 123-456-789 12 3 1\2\3
If you can see... The description column has a space in it. Which means it will allocate that into separate part of my array. First array is of size 7 but the second array will be 8. Here is what I have.
public static void main(String[] args) throws IOException, ParseException {
ArrayList < CustordData > list = new ArrayList < CustordData > ();
CustordData cd = new CustordData();
int[] array = new int[10];
DateFormat format = new SimpleDateFormat("MM/dd/Y");
try {
String read = null;
BufferedReader in = new BufferedReader(new FileReader("Custord.txt"));
while ((read = in .readLine()) != null) {
String[] splited = read.split("\\s+");
cd.setCustName(splited[0]);
cd.setPurchaseOrder(splited[1]);
cd.setSalesOrder(splited[2]);
cd.setAssemblyID(splited[4]);
cd.setOrderDesc(splited[5]);
cd.setKitDate(format.parse(splited[6]));
}
for (CustordData d: list) {
System.out.println(d.getCustName() + d.getPurchaseOrder() + d.getSalesOrder() + d.getAssemblyID() + d.getOrderDesc() + d.getKitDate() + d.getShipDate() + d.getPricePer() + d.getTotal());
}
} catch (IOException e) {
System.out.println("There was a problem: " + e);
}
If you are certain that only one column will have extra spaces, and that it will be always the same column, you could still use split but you would do something like so:
Let N be the index of the description column,
Assign columns [1, N-1] to the data you need
Assign columns [N, TotalColumns - 1] to description
Assign column TotalColumns to date
Something like so:
public static void main(String[] args) {
String noSpaces = "123 123 123 1-2-3 123-456-789 12-3 1\\2\\3";
String withSpaces = "123 123 123 1-2-3 123-456-789 12 3 1\\2\\3";
String[] splitNoSpaces = noSpaces.split("\\s+");
printData(splitNoSpaces);
String[] splitWithSpaces = withSpaces.split("\\s+");
printData(splitWithSpaces);
}
private static void printData(String[] data)
{
int totalColumns = data.length;
System.out.println("Name: " + data[0]);
System.out.println("Order: " + data[1]);
System.out.println("SOrder: " + data[2]);
System.out.println("AssemblyID: " + data[3]);
System.out.print("Description: ");
for(int i = 4; i < totalColumns - 2; i++)
{
System.out.print(data[i] + " ");
}
System.out.println();
System.out.println("AssemblyID: " + data[totalColumns - 1]);
}
Yields:
Name: 123
Order: 123
SOrder: 123
AssemblyID: 1-2-3
Description: 123-456-789
AssemblyID: 1\2\3
Name: 123
Order: 123
SOrder: 123
AssemblyID: 1-2-3
Description: 123-456-789 12
AssemblyID: 1\2\3
If you know that only problem is in description then test lenght of array. Then join array from index 5 to array lenght - 1.
KitDate will be last field of array.

Categories