How to create an array with both int and char [duplicate] - java

This question already has answers here:
How do I declare and initialize an array in Java?
(31 answers)
Closed 8 years ago.
I'm trying to create a single array that contains the numbers 1-9 and the characters A-F. The array should look like: 1 2 3 4 5 6 7 8 9 A B C D E F. I don't know how to set the array up and would appreciate any advice.

Try this following piece of code,This will work for you,However you must keep in mind that An array can contain only a single type of value as said by #MadProgrammer in comments
import java.util.*;
public class MyClass
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
char[] arr = new char[16];
/*taking input from the keyboard*/
for(int i = 0; i < 16; i++)
{
arr[i] = sc.next().charAt(0);
}
/*displaying the contents of the array*/
for(int i = 0; i < 16; i++)
{
System.out.println(arr[i] + ",");
}
}
}
In this code the numbers 0-9,which you are taking as input are still inserted as characters.

Related

Output elements of arrayi n Java [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
How to print [ ] in an array (Java)
(7 answers)
Closed last year.
I am struggling to properly output the elements of an array in java. I know I should use a for loop to print out the array, but I am not sure exactly where to start in order to make sure all the values are printed after the function call in main. Any advice would be appreciated. Thank you!
/*
1 creator shares with 3 friends
1 minute = 3 shares/ friend
what does it look like after 10 mins
*/
public int[] shares(int minutes, int people){
int[] array = new int [20];
for(int i = 1; i <= minutes; i++){
people += Math.pow(3, people);
array[i] = people;
}
return array;
}
public static void main(String args[]){
Prc test = new Prc();
System.out.println(test.shares(10, 1));
}
In arrays, you should start with zero.
The for loop could be something like this:
for(int i=0; i<myArray.length; i++){
System.out.println(myArray[i]);
}

Is this a optimal way of initializing an array with 1 through 5? [duplicate]

This question already has answers here:
Fill arrays with ranges of numbers
(7 answers)
Closed 2 years ago.
The usage of i - 1 concerns me the most. Should i use another variable for the indexing or is this fine?
public class MainClass {
public static void main(String[] args) {
int[] array = new int[5];
for(int i = 1; i<=array.length; i++) {
array[i-1] = i;
}
for(int x: array)
System.out.println(x);
}
}
Technically, it doesn't matter. However, you may do
for (int i = 0; i < array.length;)
array[i] = ++i;
to generate the same output:
1
2
3
4
5

java modifying 2D array from a method [duplicate]

This question already has answers here:
Scanner error with nextInt() [duplicate]
(1 answer)
NoSuchElementException with Java.Util.Scanner
(10 answers)
Closed 5 years ago.
i looked around a bit here on the site but found nothing that seemed to help me on this problem.
so here's my little issue. lets say i have a function that looks like this
public static void main(String args[])
{
int[][] array = new int[9][9];
createArray(array);
}
and i'm trying to pass array into this method so that i can initialize it with input that was read from the console.
public static void createArray(int[][] array)
{
Scanner input = new Scanner(System.in);
int i = 0;
int j = 0;
for(i = 0; i < 9; i++)
{
for(j = 0; j < 9; j++)
{
array[i][j] = input.nextInt();
}
}
input.close();
}
i would think that this would work, since java passes arrays by reference so that means that the createArray() method is receiving the memory address of the array back in main, and therefore any changes here affects the original one in main.
but for some reason that i'm not seeing, running this gives me these errors, and i dont understand what i'm doing wrong

Separating numbers of a string array to a integer array [duplicate]

This question already has answers here:
split int value into separate digits
(8 answers)
Closed 6 years ago.
So I have a String array that has a number like "1234567898765" and I want to convert it to an int and assign each number to each index of an array of integers.
For example, array[i]= "1234567898765" and I want to do arrayOfIntegers[0] = 1 , arrayOfIntegers[1] = 2, arrayOfIntegers[2] = 3, ect... How can I do that?
public class StringTest {
public static void main(String args[])
{
String input = "123456789765";
int [] arrayOfInts = new int [input.length() ];
for (int i = 0; i < input.length(); i++ ) {
arrayOfInts[i] = Character.getNumericValue(input.charAt(i));
}
}
}

How to convert integer in to base -2 using JAVA [duplicate]

This question already has answers here:
Integer to binary array
(11 answers)
Closed 7 years ago.
If given int array A is [1,0,0,1,1] =9 A[i]*(-2)^i (^ power).
How to write a java program get the int array sequence for give integer.
public class A {
public static void main(String[] args) {
int [] arr = {1,0,0,1,1};
for (int i = 0; i < arr.length; i++) {
arr[i]=9*arr[i]*(int)Math.pow(-2,i);
}
} System.out.println(Arrays.toString(arr));
}

Categories

Resources