I've been running into the issue of calling the class that's supposed to print even and odd numbers into the main method, printing each number without overwriting the previous one.
I've tried replacing the public int with public void, and it still didn't work, if you guys could help me it would help a lot.
this is the code:
package myprojects;
import java.util.*;
class ArrayMethod {
private int[] Array;
public void Calc(int[] Array) {
this.Array = Array;
}
public int Sum() {
int sum = 0;
for (int i = 0; i < Array.length; i++) {
sum += Array[i];
}
return sum;
}
public double Average() {
return Sum() / Array.length;
}
public int PrintOdd() {
for (int i = 0; i < Array.length; i++)
if (Array[i] % 2 != 0) {
int Odd = Array[i];
System.out.println(Odd);
}
}
public int PrintEven() {
for (int i = 0; i < Array.length; i++)
if (Array[i] % 2 == 0) {
int Even = Array[i];
System.out.println(Even);
}
}
}
public class ArrayEX {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
ArrayMethod B = new ArrayMethod();
int[] A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
B.Calc(A);
System.out.println("Sum of the numbers inside array is " + B.Sum());
System.out.println("Average of the numbers inside array is " + B.Average());
System.out.println("Even numbers " + B.PrintEven());
System.out.println("Odd Numebrs " + B.PrintOdd());
}
}
thank you to everyone who answered my question, the issue was resolved by making PrintOdd and PrintEven methods into Void type and calling it to main method outside of a print System.
my dearest thanks to #popovko57 and #zapl, who provided the solution.
If your function return nothing, you need to use void type method.
First you can update PrindOdd and PrintEven method to print each odd and even numbers when these functions are called:
class ArrayMethod {
...
public void PrintOdd() {
System.out.println("PrintOdd");
for (int i = 0; i < Array.length; i++)
if (Array[i] % 2 != 0) {
int Odd = Array[i];
System.out.print(Odd + " ");
}
}
public void PrintEven() {
System.out.println("PrintEven");
for (int i = 0; i < Array.length; i++)
if (Array[i] % 2 == 0) {
int Even = Array[i];
System.out.print(Even + " ");
}
System.out.println();
}
}
To print the result you need to update the way to call these functions:
public class Main {
public static void main(String[] args) {
....
B.PrintEven();
B.PrintOdd();
}
}
I hope that answers your question
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();
}
Okay, hopefully this makes more sense. I have an array hard coded with only 1s and 0s. I am trying to write a function that reads each element to see if it is a 0 or 1. If it is a 1, it will execute another function and then change that 1 to a 0 so that it is not read again. I have it printing simply as a placeholder for the other function I will be implementing later. I'm having trouble getting the findfirst1 function to check every element in the array. I have tried putting the incrementors for i and k in different places within the flow of the code but nothing I have tried gets me the correct output.
public static void main(String[] args)
{
int[][] testarray = {{1,0,0,0,0,0,0,1},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{1,0,0,0,0,0,0,1}};
findfirst1(testarray);
}
public static void findfirst1(int[][] array1)
{
int value = 0;
for(int i = 0; i < 6;i++)
{
for(int k = 0; k < 7;k++)
{
value = array1[i][k];
if(value == 1)
{
System.out.println(value);
array1[i][k] = 0;
}
else
{
System.out.println(value);
}
}
}
}
Okay, so after starting completely over and writing it all from scratch I figured it out. The array.length was right all along. I had trouble wrapping my head around it because I was so focused on the idea of the "image".
Edit: I just found an error where it wouldn't print the last line for array1, so I just added an extra row of 0s and it worked.
public class ChainCodeClass {
public static void main(String[] args)
{
int[][] array1 = {{0,1,0,0,0,0,0,0},{0,1,0,0,0,1,0,0},{0,1,1,1,0,0,1,0},
{0,0,0,1,1,0,0,1},{0,0,0,0,1,0,0,1},{0,0,0,0,0,1,0,0},
{0,0,0,0,0,0,1,0}**,{0,0,0,0,0,0,0,0}**};
int[][] array2 = {{0,0,0,0,0,0,0,0},{0,1,1,1,1,0,1,0},{0,0,0,0,1,0,1,0},
{0,0,1,1,1,0,1,0},{0,0,1,0,0,0,1,0},{0,0,1,0,0,0,1,0},{0,0,0,0,0,0,1,1},
{0,0,0,0,0,0,0,0}};
System.out.print("First Image");
print(array1);
findfirst1(array1);
print(array1);
System.out.print("Second Image");
print(array2);
outline8(array2);
}
public static void findfirst1(int[][] array)
{
int value = 0;
for(int i = 0; i < array.length; i++)
{
for(int k = 0; k < array.length; k++)
{
value = array[i][k];
if(value == 1)
{
System.out.print(value + " ");
array[i][k] = 0;
}
else
{
System.out.println(value);
}
}
}
}
public static void print(int[][] array)
{
for(int i = 0; i < array.length; i++) // print function for the array using incrementors
{
System.out.print("\n");
for(int k = 0; k < array.length; k++)
{
System.out.print(array[i][k] + " ");
}
}
System.out.println();
}
}
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);
}
}
My program is as below:
package simplemirror;
public class simple {
public static void main (String arg[]){
for( int i = 1; i <= 3; i++ ){
for( int j = 0; j < i; j++ ){
System.out.print(i+"*");
}
System.out.println("");
}
}
}
Above program output is as below:
1*
2*2*
3*3*3*
I need output as below:
1
2*2
3*3*3
2*2
1
this code will work try this.............:)
public class simple {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n;
n=3;
for(int i=1;i<2*n;i++){
if(i<=n){
for(int j=1;j<i;j++){
System.out.print(i+"*");
}
System.out.println(i);
}
else{
for(int j=i+1;j<2*n;j++){
System.out.print(2*n-i+"*");
}
System.out.println(2*n-i);
}
}
}
}
try
public static void main(String[] args) {
int limit=5;
int rows=limit+(limit-1);
int center=(int) Math.ceil(((double)rows/2));
boolean centerReached=false;
for(int i=1;i<=limit&&i>0;){
for(int j=1;j<=i;j++){
System.out.print(i);
if(i==j)
continue;
System.out.print("*");
}
if(center==i)
centerReached=true;
if(centerReached)
i--;
else
i++;
System.out.println();
}
}
ouptut
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1
EDIT:
It's been a while, and I have learned a few things.
You can use streams!
private static void print(int num) {
IntStream.concat(
IntStream.iterate(1, (i) -> ++i).limit(num),
IntStream.iterate(num - 1, (i) -> --i).limit(num).limit(num - 1))
.boxed()
.map(i -> Stream.generate(i::toString).limit(i).collect(Collectors.joining("*")))
.forEach(System.out::println);
}
Please don't ask such question here. Ans to your question may be looks like follows but once again don't ask such questions here.
public class simple {
public static void main (String arg[])
{
for( int i = 1; i <= 3; i++ ){
for( int j = 0; j < i; j++ ){
if(j== i-1)
{
System.out.print(i);
}
else
{
System.out.print(i+"*");
}
}
System.out.println("");
}
}
}