Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 4;
items_arr = 4;
System.out.println("The elements in the array are: ");
for (int x = 0; x < items_arr; x++)
System.out.println("Array[" + x + "]=" + array[x]);
System.out.print("\n");
Scanner insert = new Scanner(System.in);
System.out.print("Enter an Element to Insert: ");
int input = insert.nextInt();
for (s = 0; s < items_arr; s++)
if (array[s] == input)
break;
items_arr++;
for (s = 0; s < items_arr; s++)
System.out.println("Array[" + s + "]=" + array[s]);
break;
The output is. The elements are
Array [0]= 1
Array [1]= 2
Array [2]= 3
Array [3]= 4
Enter an element to Insert: 5
Array [0]= 1
Array [1]= 2
Array [2]= 3
Array [3]= 4
Array [4]= 0
when I insert 5 it posts 0
any suggestions please.. thanks!
To insert in to the array you shuould be doing follwoing operation
array[s]=input
Two notes here
Arrays are fixed length, and you should be checking the array length before inserting values in to that,other wise you will get ArrayIndexOBException. Safer to sue List/Set
As better coding practise, and to improve the readablity, you should be enclosing the conditional/loop statements (such as if or for) - see eg below
eg: 1
for (int x = 0;x<items_arr;x++) {
System.out.println("Array["+x+"]="+array[x]);
}
eg 2:
for(int s = 0; s < items_arr; s++) {
if (array[s] == input) {
break;
}
}
You have not inserted 5 in your array,
do something after items_arr++
array[ items_arr] = input;
If you do not insert any thing then by default every element is 0
You should be using a Collection type; I would recommend an ArrayList - that is -
List<Integer> al = new ArrayList<Integer>();
for (int i = 1; i < 5; i++) {
al.add(i);
}
Scanner insert = new Scanner(System.in);
System.out.print("Enter an Element to Insert: ");
int input = insert.nextInt();
al.add(input); // And so on...
You are not updating/inserting the array with the new input.
for(s = 0; s < items_arr; s++)
if (array[s] == input)
break;
items_arr++;
just replace the above code with
array[ items_arr] = input;
items_arr++;
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I would like to write a program that reads an array of integers and finds the minimum value.
The first line contains the size of an array.
Output
An integer number representing the minimum in the input array.
Input:
5
5 1 4 2 3
My code:
import java.util.Arrays;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int[] c = new int[a ];
for (int i = 0; i < c.length; i++) {
c[i] = sc.nextInt();
}
Arrays.sort(c);
int b= c[0] > 0 ? c[0]: c[c.length -1];
for (int i =1; i < c.length - 1; i++){
if(c[i] < 0){
if(c[i] > b){
b = c[i];
}
}
else{
if (c[i] < b){
b= c[i];
}
}
}
System.out.println(b);
}
}
My output:
3
-1 -2 -3
What is wrong with my code?
You are making it more complicated than necessary. No need to sort the array. Just set a variable to be equal to the first value in the array. Then scan through the array setting the variable to the actual array element if it is less than the present variable value.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
For any given dimension (here I just set it to 8 to make it easy) I want to print out the letters with odd indexes. So I am trying to get an output like: B D F H... and other letters depending on the dimension). I put a while loop to make the row 0 since I only want to print out the the odd letters on the first row and then inside the while loop I added a for loop to print out the columns (letters) with odd n. However I am getting a not an error:
error: not a statement
for (n = 1; n<dimension; n +2){
^
I am also unsure of where to put the loops to print out the odd letters.
This is my code so far:
public static void main(String[] args) {
int dimension = 8; // normally any given dimension (int dimension = Integer.parseInt(args[0]))
int n = dimension - 1;
int m = dimension -1;
char [] alphabet = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int [] nums = new int [dimension];
for (int i = 0; i < dimension; i ++) {
nums [i] = i + 1;
}
int [] [] position = new int [dimension] [dimension];
for (int row = 0; row < dimension; row ++) {
for (int col = 0; col < dimension; col ++) {
position [row] [col] = alphabet[col] + nums[row];
}
}
char p = (char)(position[m][n] - nums[n]);
while (m == 0) {
for (n = 1; n<dimension; n +2){
System.out.println(p); //odd letters on the first row
}
}
}
}
Edit: the program is compiling but the loop is not working so I am not getting any output. How can I fix it?
This is a simple syntax error.
for (n = 1; n<dimension; n +2){
Should be:
for (n = 1; n<dimension; n += 2){
The final part of the for statement is an operation that can be used to change the iterator (or do other operations). If you consider the following line of code:
n +2;
This is not a valid statement by itself. However, the following statement is valid:
n += 2;
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 5 years ago.
Improve this question
I'm trying to write a program that reads a sequence of integers and divide it into two sequences. The values on odd positions will be the first sequence and the values of even positions will be the second sequence. The program prints the elements of the first sequence and then the elements of the second sequence separated by a single space. The first input value specifies the number of elements.
Input: 7 1 2 4 5 6 8 9
Expected Output: 1 4 6 9 2 5 8
My Output: 2 0
package twosequences;
import java.util.Scanner;
public class TwoSequences {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] values = new int[n];
for (int i = 0; i < values.length; i ++) {
values[i] = sc.nextInt();
}
int odd = 0;
int even = 0;
int i = 1;
if (i % 2 == 0) {
even = values[i];
} else {
odd = values[i];
}
i++;
System.out.printf("%d %d%n", odd, even);
}
}
I'm not sure why I'm outputting 2 0. Any suggestions?
You need two different loops to iterate over even and odd elements respectively to obtain the desired output.
for (i = 0 ; i < n ; i += 2) {
even = values[i];
System.out.printf("%d ", even);
}
for (i = 1 ; i < n ; i += 2) {
odd = values[i];
System.out.printf("%d ", odd);
}
Hope this helps.
Print the even-numbered elements in one loop; then use another loop to print the odd-numbered elements.
System.out.print(values[0]); // print by itself to avoid prepending " ".
for (int i = 2; i < values.length; i += 2) {
System.out.print(" " + values[i]);
}
for (int i = 1; i < values.length; i += 2) {
System.out.print(" " + values[i]);
}
System.out.println();
you take the second element of an array (2), and only for that one you are checking if it's even or not.
The result goes true for first 'if', and
even = 2;
, the if statement ends and odd stays as declares (0), that' why u got result like that.
If you want output whith more than 2 integers, you need to change
, where n would be all scanned even numbers.
Try to put a counter in first for loop, like
for (int i = 0; i < values.length; i ++) {
values[i] = sc.nextInt();
if(values[i]%2==0) evenCounter++;
}
Now to count odds just count values.length - evenCounter.
Also you do not need to make simple arrays like int[], you can go with
List<Integer> list = new ArrayList<>();
You will be able to add elements in for loop withour knowing how many elements you will be implementing.
Hope some of those helps.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
I wanted to see better code/logic of the below questions from you guys.
Que: Say, there are two arrays - Input and Output array. Taking 4 elements in input array. O/p array will also have length of same as Input array. Output should have - first index value should product of all values from the rest of indexes except first. Similarly, second index value should have product of all other values from the rest of indexes except second. Likewise, the product should go till output array get the same length as that of input's. I have written code but I wanted to see different answers from different viewers. More customized and use of library functions is helpful. Customizing my code also is welcome.
System.out.println("Enter the size of the array..");
scan1 = new Scanner(System.in);
int n = scan1.nextInt();
int[] arr = new int[n];
System.out.println("Enter the array elements..");
for (int i = 0; i<n; i++)
arr[i] = scan1.nextInt();
System.out.println("Array is.." +Arrays.toString(arr));
int[] arr1 = new int[arr.length];
for (int i = 0;i<arr.length;i++){
int sum = 1, j = 0;
while (j<arr.length){
if(i==j)
System.out.println("I and J are equal");
else
sum = sum * arr[j];
j++;
}arr1[i] = sum;
}
System.out.println(Arrays.toString(arr1));`
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to write a code that fill the array with the specific character in a string.
For example
String s = "abcdefghijklmnopqrstuv";
String[] arr = new String[5];
int x = 0;
for (int i = 0; i < 5; i++) {
arr[i] = s.charAt(x);
x += 2;
}
An error shows up "Incompatible types"
How to fix this? I'm new to java.
Solved! Thanks!
You're assigning a character to a position in a String array. In your code, arr[i] refers to a String, and s.charAt(x) is a char.
It seems like arr should be a char array instead of a String array.
Not sure exactly what you're getting at here, so you have two options.
A char array:
String s = "abcdefghijklmnopqrstuv";
char[] arr = new char[5];
for (int i = 0; i < 5; i++) {
arr[i] = s.charAt(x);
x ++;
}
A string array where the strings are single characters:
String s = "abcdefghijklmnopqrstuv";
String[] arr = new String[5];
for (int i = 0; i < 5; i++) {
arr[i] = s.substring(x, x+1);
x ++;
}