Array processing in Java [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have this String array in which is made up of string representing a student id, first name, last name, email,and grades. My question is: Is there away I can split each entry in this array and be able to calculate the average grade of student in this sample array entry. I would appreciate if anyone can offer some solution on how to achieve that in this array.
String[] students = {"1,John,Smith, John1010#fakemail.com ,20,88,79,59",
"2,Suzan,Erickson, Sue9999#fakemail.com ,19,91,72,85",
"3,Jack,Napoli, Jack789#fakemail.com,19,85,84,87",
"4,Erin,Black, Aaron888#fakemail.com,22,91,98,82"};

Here a piece of code that does what you want:
String[] students = ...
double[] averages = new double[students.length];
for (int i = 0; i < students.length; i++) {
String[] student = students[i].split(",");
int sum = 0;
for (int j = 4; j < student.length; j++) {
sum += Integer.parseInt(student[j]);
}
averages[i] = (double) sum / (student.length - 4);
}
The array averages will contain all the averages at the same position. Note that this code doesn't handle any wrong format and that it's assuming that all the remaining values are grades.

I guess writing code for you will not be so appropriate... However, I can suggest what needs to be done:
1. Split each string by using String class split(regex). You can simply split the string with comma.
(https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String))
2. Just add the grade and divide by the size of students array.
I am sorry if you were expecting someone would provide the code...but learning by doing!!

public static void main(String[] args) {
int grades = 0;
String[] students = { "1,John,Smith,John1010#fakemail.com,20,88,79,59",
"2,Suzan,Erickson,Sue9999#fakemail.com ,19,91,72,85", "3,Jack,Napoli,Jack789#fakemail.com,19,85,84,87",
"4,Erin,Black,Aaron888#fakemail.com,22,91,98,82" };
for (int i = 0; i < students.length; i++) {
String[] student = students[i].split(",");
for (int j = 4; j < student.length; j++) {
grades += Integer.parseInt(student[j]);
}
grades=0;System.out.println(student[2] + ": " +(double) grades / (student.length - 4));
}
}

Related

How to print out possible string with alphabet set and length? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am using java
For example, the following situation:
First, the function is used as a print all possible strings.
char[] alphabetSet = "012abc".toCharArray();
int length =5;
Output:
0,1,2,a,b,c,01,02,0a,0b,0c,10,11,12,1a ..................... ccccc. stop in length = 5
Then, I want to add a loop stopper to fetch the specified string.
char[] alphabetSet = "012abc".toCharArray();
int length =5;
int loopStopper = 3;
Output:
a
Thank you
Use backtracking.
void print_all(char []ch,int maxLen){
for(int i=1;i<=maxLen;i++)
backTrack(ch,i,0,new char[i]);
}
void backTrack(char[] ch,int len,int k,char[] ans){
if(k==len){
System.out.print(new String(ans,0,len)+",");
return;
}
for(int i=0;i<ch.length;i++){
ans[k]=ch[i];
backTrack(ch,len,k+1,ans);
}
}
try this:
String alphabet = "012abc";// for example as your code "012abc"
char[] alphabetSet = alphabet.toCharArray();
int length = 5;
for (int i = 0; i < alphabetSet.length; i++) {
System.out.print(alphabetSet[i] + ",");
}
for (int j = 0; j <= length; j++) {
for (int i = 0; i < alphabetSet.length; i++) {
System.out.printf("%d%c,",j,alphabetSet[i]);
}
}

Trying to figure out how can I make a program to identify equal numbers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
so my real question is, how can i make this code identify all the "look alike" numbers while theire running from 1 to 99, for example :11,22,33,44,...
and while the program identify them it sends a message.
package doodle;
int num2=11;
for (int i=1; i<100; i++) {
System.out.println(i);
int num1=i;
if(num1==num2) {
System.out.println("WOW");
}
}
Thanks
I would do using a String
for (int i = 11; i < 100; i++) {
StringBuffer orig = new StringBuffer();
String left = orig.append(i).toString();
if (orig.reverse().toString().equals(left)) {
System.out.println(left);
}
}
or if you really wanted to use an int with flaky logic
int start = 11;
for (int i = 11; i < 100; i++) {
if (i == start) {
System.out.println(start);
start += 11;
}
}
Edit
As #mark has rightly pointed out, these solution only work whilst the range is up to 100
int num2=11;
for (int i=1; i<100; i++) {
if(i%num2==0) { //<---- look alike
System.out.println("WOW");
}
I would do it using String conversion and codePoint comparison
for (Integer number = 0; number < 1000; number++) {
System.out.println(number);
String stringnumber = String.valueOf(number);
if (stringnumber.length() > 1 && stringnumber.codePoints().allMatch((digit) -> digit == stringnumber.codePointAt(0))) {
System.out.println("WOW");
}
}
length check (length() > 0) is needed to exclude all numbers with only one digit, otherwise, the program would print "WOW" for all numbers from 0 - 9 too.
All numbers from 0 to Integer.MAX_VALUE can be handled.

How to convert Java code to mapreduce? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am trying to convert my Java code to Hadoop MapReduce.
I am new to MapReduce programming so can you please help me out with this.
I am trying to take count of all the distinct values in my file, and present the output in another file.
Input:
Alex
Alex
John
Stuart
Michael
John
Michael
Alex
Output:
Alex 3
John 2
Stuart 1
Michael 2
Here's my code:
public class test {
public static void main(String[] args) throws Exception {
String[] arr = { "sa", "pa" , "ga", "sa", "pa", "la" };
String[] result = new String[10];
int counter = 0, count = 0;
for (int i = 0; i < arr.length; i++) {
boolean isDistinct = false;
for (int j = 0; j < i; j++) {
if (arr[i] == arr[j]) {
isDistinct = true;
break;
}
}
if (!isDistinct) {
result[counter++] = arr[i];
}
}
for (int i = 0; i < counter; i++) {
count = 0;
for (int j = 0; j < arr.length; j++) {
if (result[i] == arr[j]) {
count++;
}
}
System.out.println(result[i] + " = " + count);
}
}
}
In this code I have given input as an array, but in actual circumstances I need to fetch that from a file in HDFS.
Plus output of the file should be in making a new file in HDFS.
Your problem is equivalent to the standard WordCount MapReduce example most developers will start with.
Example: WordCount v1.0
WordCount is a single MapReduce job with a Map and Reduce stage.
The Mapper reads each line from an input file, one at a time. It emits a key which is the value (in your case a name) and a count of 1.
The Reduce groups based on the Key (name) and adds up the values, finally emitting a key and the total number of times that key has been seen.
You'll notice the example includes a Combine stage which (basically) runs on the output of the Mapper. In this example the Reduce can be reused since the logic is the same and it receives and emits the same types of key/value objects. The combiner will reduce the amount of data sent to the Reduce stage.

How to transform a number into array. Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to transform a number into array.
For example...
num = 7
to
list = [1,2,3,4,5,6,7]
How do I do that?
Try this :
int []list = new int[num];
for (int i = 0; i < list.length; i++) {
list[i] = i + 1;
}
Try this:
Integer[] ints = new Integer[x];
for (int i = 0; i < ints.length; i++) {
ints[i] = i + 1;
}
I'm assuming you're want to create an array from 1 to the number.
Code:
for(int i = 0; i < num; i++) {
list[i] = i+1;
}
In java you have to declare the variables first.
This means that variables are strongly typed.
You cannot convert a variable into an array.
However you can create a new variable that is an array.
int num = 7;
int[] arr = {num}; // arr is an array containing num

Problems with 3 way sort in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Hi I think there may be a problem with my 3 way sort algorithm in the following Java program, also any suggestion on optimizing or just a simpler it would be greatly appreciated. The objective of the sort is to have the minus numbers first then zeros and then positive numbers
class ThreeWaySort
{
public static void main(String[] args)
{
int location = 0;
int[] sArray = new int[50];
for (int a = 25; a<= -24; a--)
{
sArray[location] = a;
location++;
}
int i = 0; int j = 0; int k = 50;
while (j!=k)
{
if (sArray[j]==0)
{
j++;
}
else if (sArray[j]<0)
{
int t = sArray[i]; sArray[i] = sArray[j]; sArray[j] = t; // case (ii)
i++; j++;
}
else
{
k--;
int t= sArray[j]; sArray[j] = sArray[k]; sArray[k] = t;
}
}
for (int a = 0; a <= 49; a++)
{
if(sArray[a] >-1)
{
System.out.println();
System.out.println();
System.out.println();
}
if(sArray[a] > 0)
{
System.out.println();
System.out.println();
System.out.println();
}
System.out.print(sArray[a] + " ");
}
}
}
When i run the program as is it costantly print out a zero followed by three line instead of what I'm expecting to be, Numbers below zero in a line, followed by 3 blank lines then any zeros in the array, 3 blank lines, positive numbers in the array.
The loop that populates your array is incorrect:
for (int a = 25; a<= -24; a--)
The variable a starts at 25, which is not less than or equal to -24, so the loop never executes. You should use >=.

Categories