I am trying to iterate over an array and add the sum of the array except the number 13 and the number after it.
Example
[1,1,1,1,13,2] = [1,1,1,1,0,0] = 4
this is what I have so far the main things I need to know is how do I check if the array has a number 13 in it and how do I change it to a 0
public static int sum13(int[] nums) {
for(int i=0; i < nums.length; i++) {
if(nums.indexOf(i) == 13) {
}
}
}
public static void main(String[] args) {
//this is the main method
int[] a = {1,2,3,13,4};
sum13(a);
}
}
You can try this , to skip adding all number when you get 13 in your array :
public static int sum13(int[] nums) {
int sum = 0;
for(int i=0; i < nums.length; i++) {
if(nums[i] == 13) {
break;
}
sum += nums[i];
}
return sum;
}
public static void main(String[] args) {
//this is the main method
int[] a = {1,2,3,13,4};
System.out.println(sum13(a));
}
Try this.
public static int sum13(int[] nums) {
return IntStream.of(nums).takeWhile(i -> i != 13).sum();
}
Related
I have a problem, which read N numbers from the command line, and I want to check if all those numbers are the same.
This is my code
class q2 {
public static boolean check(int[] arr) {
for (int i = 0; i < arr.length-1; i++) {
for (int j = 1; j < arr.length; j++) {
if (arr[i]==arr[j]) return false;
}
}
return true;
}
public static void main(String[] args) {
int[] arr = new int[args.length];
boolean result = check(arr);
System.out.println(result);
}
}
but in any case (but no input at all) it return false and I don't know where is the mistake.
If you are passing the values from the command line, you need to add those values into the array that you will pass into the check method:
public static void main(String[] args) {
int[] arr = new int[args.length];
for(int i = 0; i < arr.length; i++){
arr[i] = Integer.parseInt(args[i]);
}
boolean result = check(arr);
System.out.println(result);
}
You can simplify your check method e.g. you do not need a double loop. Just compare the current element with the next one as shown below:
public static boolean check(int[] arr) {
for (int i =0;i<arr.length-1;i++)
if (arr[i] != arr[i + 1])
return true;
return false;
}
public class count {
private static int countPositive(int[] elems) {
int positive = 0;
for (int i=0;i<elems.length;i++) {
if (elems[i] > 0){
positive++;
}
}
return positive;
}
public static void main(String[] args) {
for(int i=0;i<args.length;i++)
//int x =Integer.parseInt(args[i]);
//System.out.println(countPositive(new int[]));
}
}
here i want to convert every str "numbers" in int number,but i have no idea how to write it. Do i have to add another array to save these int numbers,and then call the countPositive? please offer some help
my purpose is to write a command line argument, and it give me a number of positive numbers,for example
> java count 1 2 3 4 5 -1 -2 -3
5
> java count 0 -1 -2 -3 -3 -4
0
Your current code shows effort, is on the right track, and actually contains all the pieces you need to actually get a working solution. I refactored your countPositive() method to directly accept the string array of command line arguments. Note that there is no reason to create an integer array; you can simply parse each command line argument and analyze it on the fly.
public class count {
private static int countPositive(String[] elems) {
int positive = 0;
for (int i=0; i < elems.length; i++) {
int element = Integer.parseInt(elems[i]);
if (element > 0) {
positive++;
}
}
return positive;
}
public static void main(String[] args) {
int count = countPositive(args);
System.out.println(count));
}
}
You have two choices - either add a second array, or count the positive numbers on the fly.
First solution - first transform the Strings to ints, and then count the positives ones:
public class count {
private static int countPositive(int[] elems) {
int positive = 0;
for (int i = 0; i < elems.length; i++) {
if (elems[i] > 0) {
positive++;
}
}
return positive;
}
public static void main(String[] args) {
int[] intArr = new int[args.length];
for (int i = 0; i < args.length; i++) {
intArr[i] = Integer.parseInt(args[i]);
}
System.out.println(countPositive(intArr));
}
}
Second solution - transform the Strings to ints one at a time, and count the positive ones:
public class count {
public static void main(String[] args) {
int positive = 0;
for (int i = 0; i < args.length; i++) {
if (Integer.parseInt(args[i]) > 0) {
positive++;
}
}
System.out.println(positive);
}
}
A bonus, third solution using Streams:
public class count {
public static void main(String[] args) {
System.out.println(Arrays.stream(args).mapToInt(i -> Integer.parseInt(i)).filter(i -> i>0).count());
}
}
You can start by copying the String[] args to an int[] (and then use that to call countPositive). Like,
public static void main(String[] args) {
int[] values = new int[args.length];
for (int i = 0; i < args.length; i++) {
values[i] = Integer.parseInt(args[i]);
}
System.out.println(countPositive(values));
}
or, in Java 8+, do it all with Stream(s) like
System.out.println(Stream.of(args).mapToInt(Integer::parseInt)
.filter(val -> val > 0).count());
Okay, so i need to find all the negative numbers of array and return them.I found the negative number, but how do i return them all? P.S yes i am a beginner.
public static void main(String[] args) {
int [] array = {5,-1,6,3,-20,10,20,-5,2};
System.out.println(findNumber(array));
}
public static int findNumber(int[] sum) {
int num = 0;
for (int i = 0; i < sum.length ; i++) {
if(sum[i] < num) {
num = sum[i];
}
}
return num;
}
Java 8 based solution. You can use stream to filter out numbers greater than or equal to zero
public static int[] findNumber(int[] sum)
{
return Arrays.stream(sum).filter(i -> i < 0).toArray();
}
There are multiple ways of doing this, if you just want to output all of the negative numbers easily you could do this:
public static void main(String[] args) {
int [] array = {5,-1,6,3,-20,10,20,-5,2};
ArrayList<Integer> negativeNumbers = findNumber(sum);
for(Integer negNum : negativeNumbers) {
System.out.println(negNum);
}
}
public static ArrayList<Integer> findNumber(int[] sum) {
ArrayList<Integer> negativeNumbers = new ArrayList<>();
for (int i = 0; i < sum.length ; i++) {
if(sum[i] < 0) {
negativeNumber.add(sum[i]);
}
}
return negativeNumbers;
}
As you told you are beginner, i'm giving code in using arrays only.
Whenever you come across a negative number, just add it to the array and increment it's index number and after checking all the numbers, return the array and print it.
public static void main(String[] args)
{
int [] array = {5,-1,6,3,-20,10,20,-5,2};
int[] neg = findNumber(array);
for(int i = 0 ; i<neg.length; i++)
{
System.out.println(neg[i]);
}
}
public static int[] findNumber(int[] a)
{
int j=0;
int[] n = new int[a.length];
for(int i = 0; i<a.length ; i++)
{
if(a[i] <0)
{
n[j] = a[i];
j++;
}
}
int[] neg = new int[j];
for( int k = 0 ; k < j ; k++)
{
neg[k] = n[k];
}
return neg;
}
I hope it helps.
You can modify your method to iterate through the array of numbers, and add every negative number you encounter, to a List.
public static List<Integers> findNegativeNumbers(int[] num) {
List<Integers> negativeNumbers = new ArrayList<>();
for (int i = 0; i < num.length; i++) {
if(num[i] < 0) {
negativeNumbers.add(num[i]);
}
}
return negativeNumbers;
}
You could then print out the list of negative numbers from this method itself, or return the list with return to be printed in main.
You code is returning the sum of elements, but I understood that you wanted every negative number.
So, I assumed you want something like this:
public static void main(String[] args) {
int [] array = {5,-1,6,3,-20,10,20,-5,2};
Integer [] result = findNumbers( array );
for( int i : result )
{
System.out.println( i );
}
}
public static Integer[] findNumbers(int[] v) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < v.length ; i++) {
if(v[i] < 0) {
list.add(v[i]);
}
}
return list.toArray( new Integer[0] );
}
Is it?
Best regards.
public static int[] findNum(int[] array)
{
int negativeIntCount = 0;
int[] negativeNumbers = new int[array.length];
for(int i = 0; i < array.length; i++)
{
if(array[i] < 0)
{
negativeIntCount++;
negativeNumbers[i] = array[i];
}
}
System.out.println("Total negative numbers in given arrays is " + negativeIntCount);
return negativeNumbers;
}
To display as an array in output :
System.out.println(Arrays.toString(findNum(array)));
To display output as space gaped integers :
for(int x : findNum(array))
{
System.out.print(" " + x)
}
I want to separate negative numbers and positive numbers in an array.
For example, if my array has 10 values and they are {-8,7,3,-1,0,2,-2,4,-6,7}, I want the new modified array to be {-6,-2,-1,-8,7,3,0,2,4,7}.
I want to do this in O(n^2) and I have written a code as well. But I am not getting the right outputs. Where is my code wrong?
import java.util.Random;
public class Apples {
public static void main(String[] args) {
Random randomInteger=new Random();
int[] a=new int[100];
for(int i=0;i<a.length;i++)
{
a[i]=randomInteger.nextInt((int)System.currentTimeMillis())%20 - 10;
}
for(int i=0;i<a.length;i++)
{
if(a[i]<0)
{
int temp=a[i];
for(int j=i;j>0;j--)
{
a[j]=a[j-1];
j--;
}
a[0]=temp;
}
}
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
}
}
You have two j-- while you need only one, so remove either one of them.
for(int j=i;j>0;j--)
{
a[j]=a[j-1];
// remove j--; from here
}
You might consider the following as an alternative way of doing the partition of negatives/positives. This is based on K&R's quicksort, but only makes one pass on the array:
import java.util.Random;
public class Sweeper {
public static void printArray(int[] a) {
for (int elt : a) {
System.out.print(elt + " ");
}
System.out.println();
}
public static void swap(int[] a, int i, int j) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
public static void partition(int[] a, int target) {
int last = 0;
for (int i = 0; i < a.length; ++i) {
if (a[i] < target && i != last) swap(a, i, last++);
}
}
public static void main(String[] args) {
Random rng = new Random();
int[] a = new int[20];
for (int i = 0; i < a.length; i++) {
a[i] = rng.nextInt(20) - 10;
}
printArray(a);
partition(a, 0);
printArray(a);
}
}
I am taking a programming class, and things just aren't clicking for me. I have an assignment that asks to:
Write a program to assign the integer values 1 through 25 to a 25
element integer array. Then, print the array as five separate lines
each containing five elements separated by commas. The last element
on each line should be followed by a newline instead of a comma. The
output of your program should appear exactly as follows:
1,2,3,4,5
6,7,8,9,10
11,12,13,14,15
16,17,18,19,20
21,22,23,24,25
Hints:
One way to determine every 5th element is to use the modules operator (%). If you divide the subscript by 5 and the remainder is
0, it is the 5th number.
You can use System.out.print() to print a value without a newline following it. This will allow you to print multiple things on the same
line.
I have a little bit of code but I don't know where to go from here:
public class Program4
{
public static int[] array;
public static void main(String[] args);
{
int[] numbers = new int [25]
for(int i=0; i<25; i++)
array[i] = i + 1;}
public static void printArray()
{
for(int i=1; i<=25; i++);
{
System.out.print(array[i - 1]);
if (i % 5 == 0)
System.out.printIn();
}
}
I just have a mental block about programming-can anyone help point me to some helpful examples?
Try this,
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static int[] array;
public static void main(String[] args)
{
array = new int[25];
for(int i=0; i<25; i++)
array[i] = i + 1;
printArray();
}
public static void printArray()
{
int i;
for(i=1; i<=25; i++){
if (i % 5 != 0)
System.out.print(array[i-1]+",");
else
System.out.println(array[i-1]);
}
}
}
public class Foo {
public static int[] nums;
public static void main(String[] args) {
nums = new int[25];
for (int i = 0; i < nums.length; i++) {
nums[i] = i + 1;
}
printArray(nums);
}
public static void printArray(int[] myArray) {
for (int i = 0; i < myArray.length; i++) {
System.out.print(String.valueOf(myArray[i]);
if (i % 5 == 0) {
System.out.println();
} else if (i % 5 != 4){
System.out.println(", ");
}
}
}
public class Test {
public static void main(String[] args) {
int[] array = new int [25];
for(int i=0; i<25; i++) {
array[i] = i + 1;
}
for (int i=1; i<=25; i++) {
System.out.print(array[i - 1]);
if (i % 5 == 0) {
System.out.println();
} else {
System.out.print(", ");
}
}
}
}
And try to learn Java syntax first of all.
Here is an enhanced version of your code.
public class Program4
{
public static int[] array = new int[25];//instantiate the array to its default values
public static void main(String[] args)
{
//calling the methods from main
addToArray();
printArray();
}
//add the numbers to the array
public static void addToArray(){
for(int i=0; i<25; i++)
array[i] = i + 1;
}
//print the numbers from the array
public static void printArray()
{
for(int i = 1; i <= 25; i++){
if(i % 5 == 0){
System.out.print(i);
System.out.println();
}
else{
System.out.print(i + ",");
}
}
}
}