I'me new here and beginner of JAVA.
I've just started learning about Scanner and Array!
I want to get some input from Scanner. and save it into Array.
the array size is 5 and I succeed save those
but what I could print is only one value from array.
I want to print all values in the array
and I have no idea what should I do or where I should fix!
hope you guys tell me about that.
My code is down below.
import java.util.Scanner;
import jdk.internal.util.xml.impl.Input;
public class Test2 {
public static void main(String[] args) {
int[] array = new int[5];
Scanner scanner = new Scanner(System.in);
System.out.print("five numbers");
for (int i = 0; i <= array.length; i++) {
array[i] = scanner.nextInt();
}
for (int nums : array) {
System.out.print(nums);
}
}
}
this is result :
five numbers1 2 3 5 5
The issue is with the following iteration which is expecting 6 numbers (since i starts at 0 and ends at 5).
This wont work :
for (int i = 0; i <= array.length; i++)
This will work
You should either input 6 numbers and change your array size to 6 elements. Either ways, you need to change the for loop to :
for (int i = 0; i < array.length; i++)
You problem is that you defined an array of size 5:
int[] array = new int[5];
BUT you expect for 6 input numbers, as the for-loop set to loop 6 time due to <=:
for (int i = 0; i <= array.length; i++) {
array[i] = scanner.nextInt();
}
The solution here is to change the condition to i < array.length
Explanation:
By definition:
Array size is 5
array.length = 5
inside the for-loop: array[i] = scanner.nextInt(), when i = array.length i.e. i=5, array[i] is out of bounds.
change
for (int i = 0; i <= array.length; i++)'$'
to
for (int i = 0; i < array.length; i++)'$'
as you want it to loop 5 times.
You can use
either
System.out.print(Arrays.toString(array));
i don't think you need to use
import jdk.internal.util.xml.impl.Input;
package file.
and change the loop statement
for (int i = 0; i <= array.length; i++)
to
for (int i = 0; i < array.length; i++)
otherwise it will rise an error when you will put 6th input like:
java.lang.ArrayIndexOutOfBoundsException
Related
int[] box = new int[9*8];
for(int i=0; i<9; i++) {
for(int j=0; j<8; j++) {
box[j] = i;
}
}
I've tried everything and it turns out to be way harder than it looks for me. Without using ArrayLists (I understand this works using box.add(i)) I can only use int[] type. I need to create a list of integers that looks like this [0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,..8,8,8,8,8,8,8,8] so 8 sets of integers from 0-8. Can anyone help me?
I believe the problem is that on line 4. The code sets a position on to a value, but this position repeats from 0 to 7.
This should work better:
int[] box = new int[9*8];
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 8; j++) {
box[i * 8 + j] = i;
}
}
Basically, it shift the 0 - 7 over 8 places for every new number.
I'm trying to add all the elements together in an array that was decided through user input, Every time I run the code that I've constructed below I get a number that is obviously not the sum of the elements. What am I doing wrong?
import java.util.Scanner;
public class SumProduct
{
public static void main (String []args)
{
Scanner input = new Scanner (System.in);
int[] array1 = new int [input.nextInt()];
input = scan.nextInt();
for (int i = 0; i < array1.length; i++)
{
array1[i] = input.nextInt();
}
for (int i = 0; i < array1.length; i++)
{
int j = array1[i];
int k = array1[i]+1;
int sum = j + k;
System.out.print(sum);
}
}
}
You probably want to prompt the user to enter the size of the array if you're going to do this.This line of code is allowing whatever the user enters to be the size of your array.
int[] array1 = new int [input.nextInt()]; //this sets the size of the array through user input
scan doesn't exist in the currrent context:
input = scan.nextInt(); // this is invalid syntax as scan is not the Scanner you created
for (int i = 0; i < array1.length; i++)
{
array1[i] = input.nextInt();
}
I would do this to keep adding elements to the array:
// no need for this: input = scan.nextInt();
for (int i = 0; i < array1.length; i++)
{
System.out.println("Enter integer to add:");
array1[i] = input.nextInt();
}
This code will give you the sum of the elements if you just add one element at a time instead of two to the sum variable:
int sum = 0;
for (int i = 0; i < array1.length; i++)
{
sum += array1[i];
System.out.print(sum); // this will print out sum after each addition
}
System.out.print(sum); // this will print out sum after the entire array is summed
Adding some logic to only allow the user to enter so many numbers to the array would be helpful as well. You will need to move on from them entering data into the array at some point. Then also remember to close the scanner when you're finished getting data from the user:
input.close();
Your problem is in the following lines of code:
for (int i = 0; i < array1.length; i++)
{
int j = array1[i];
int k = array1[i]+1;
int sum = j + k;
System.out.print(sum);
}
it should look something like
int sum = 0;
for (int i = 0; i < array1.length; i++)
{
sum = sum + array1[i];
}
System.out.print(sum);
The first change is to declare the variable "sum" outside of the loop. The way it's written, it will get declared, then disappear, then declared, then disappear for every loop iteration. You also probably want to initialize it to 0
The second change is to your summation logic. Lets assume your array contains the three numbers [1, 2, 3] and walk through your logic.
j = array1[0] //(j == 1)
k = array1[0] + 1 //(k == 1 + 1 == 2)
sum = j + k //(sum == 1 + 2 == 3)
You then throw out the variable "sum" like I mentioned earlier, and start over with the same logic on the second array element, then again on the third.
i.e. j = 2, k = 2+1, sum = 2 + 2 + 1. followed by j = 3, k = 3 + 1, sum = 3 + 3 + 1.
You can probably see how this isn't the sum, and results in you logging the three digits 3, 5, and 7 for this example case.
In my updated logic, we simply loop through each element and add it to the current running total.
hope this helps
I've been trying to work out what is wrong but I can't seem to figure it out. Essentially my code will get the user to input N size of array. The array will then be filled with random numbers generated from 1-100. I have a printArray method which prints the elements of an array in a single line that I've tested on a fixed array and it works, but when I call it from the generated array it gives me a lot of extra 0's.
Here is the code:
Scanner scan = new Scanner(System.in);
System.out.println("Size of array:"); //prompts the user enter size of array
int size = scan.nextInt();
int array[] = new int[size];
for (int i = 0; i < array.length; i++) {
array[i] = random.nextInt(100) + 1;
printArray(array);
and here is the display method:
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
if (i == 0) {
System.out.print(array[i]);
}
else if (i == array.length) {
System.out.print(array[i]);
}
else
System.out.print("," + array[i]);
}
}
When I run the code it will generate an output like this:(3 as example)
Size of array to sort?
3
36,0,036,68,036,68,75, where it's supposed to just be 36,68,75.
You are printing the array multiple times in a single line.
In the first iteration you print 36,0,0, In the second iteration you print 36,68,0 and only in the final iteration you print the fully initialized array - 36,68,75.
Move printArray(array); to the end of the loop. You might want to add a println at the end of printArray.
for (int i = 0; i < array.length; i++) {
array[i] = random.nextInt(100) + 1;
}
printArray(array);
So, I generate a 100 numbers between the range of 0 and 9. I store these 100 numbers in an array called 'array'. Then I have the array called 'count'. It has 10 elements, and I wanted to check the following: for each element in 'array' if it equals to 0-9 then count[0-9] increments by 1, count[0] = how many times number 0 appears and so on count[1] = 1, count[2] = 2... . I just keep getting the output of around 20k numbers and i suppose? the sum of each element?, no idea why. I was wondering if there is something major wrong with my for loop?
import java.util.*;
class RandomInt {
public static void main(String[] args) {
int size = 100;
int max = 10;
int[] array = new int[size];
int[] count = new int[max]; //count[0,0,0,0,0,0,0,0,0,0]
int loop = 0;
Random generator = new Random();
for (int i = 0; i < size; i++) {
array[i] = generator.nextInt(max); // Generates 100 random numbers between 0 and 9 and stores them in array[]
System.out.print(array[i]);
for (int x = 0; x < size; x++) {// loop through 10 elements in count
for(int j = 0; j < 10; j++){ //loop through 100 elements in array
if (array[x] == j) {// loop through each 100 elements of array[x] and if element array[x] = value
count[j] += 1; // then count[x] = x + 1
System.out.print(count[j]);
}
}
}
}
System.out.println("0 appears " + count[0] + " times.");
}
}
Your Login is Perfect only mistake which i found u made is with the brackets........!
Generate the numbers using first loop and then count the number of occurrence using different for loop.
Here is your code's modified version which generates 10 numbers and counts the individual number occurrence count.....
public class RandomInt {
public static void main(String[] args) {
int size = 10;
int max = 10;
int[] array = new int[size];
int[] count = new int[max]; //count[0,0,0,0,0,0,0,0,0,0]
int loop = 0;
Random generator = new Random();
for (int i = 0; i < size; i++)
{
array[i] = generator.nextInt(max); // Generates 100 random numbers between 0 and 9 and stores them in array[]
System.out.print(array[i]+" ");
}
for (int x = 0; x < size; x++)
{// loop through 10 elements in count
for(int j = 0; j < 10; j++)
{ //loop through 100 elements in array
if (array[x] == j)
{// loop through each 100 elements of array[x] and if element array[x] = value
count[j] += 1; // then count[x] = x + 1
//System.out.print(count[j]);
}
}
}
System.out.println("3 appears " + count[3] + " times.");
}
}
There's a simpler way to do this without nested loops, so forgive me for suggesting this as a fix rather than finding the issue in the loop.
for(int i=0; i<size; i++){
int num = generator.nextInt(max);
array[i] = num;
count[num]++;
}
One loop, incrementing the count for each number as it appears. You may need to ensure all the entries in count start at 0, but even then an additional loop through 10 entries is MUCH faster.
To increment your counter, you don't need to have two nested for loops. Instead, you can use the value of array[x] as your counter.
for (int i = 0; i < size; i++) {
count[array[i]]++
}
You've nested your counting loop inside of your random number generating loop. Move the counting part outside.
Edit: The reason you're getting like 20k or whatever instances of zero is because when you set array[0] with a random value, you also check how many instances of 0 are in array[1] to array[99].
You probably shouldn't do your count until you have finished assigning your numbers, but here is how you could. Note that you want the value at array[i] to be your index to count.
for (int i = 0; i < size; i++) {
array[i] = generator.nextInt(max); // Generates random numbers
count[array[i]]++;
}
System.out.println(Arrays.toString(array));
System.out.println(Arrays.toString(count));
I'm declaring a 2d Array with 100 rows and columns. Im trying to get the user to dictate the numbers that go into the array. Im supposed to store the values without storing them in a variable. This is what I have so far but I don't think this is correct
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int [][] nums = new int[100][100];
int digits;
for (int i = 0; i < nums.length; ++i)
{
int[scan.nextInt()][scan.nextInt()];
}
You'll need to use nested for loops for a 2-d array (one for rows and one for columns):
for (int i = 0; i < nums.length; ++i)
for (int j = 0; j < nums[i].length; ++j)
{
nums[i][j] = scan.nextInt();
}
Well, first of all, you are dealing with a two dimensional array, so you will need two loops, one for the rows and the other for the colums.
for(int i=0; i<100; i++)
{
for(int j=0;j<100;j++)
{
nums[i][j] = scan.nextInt();
}
}
This syntax - int[scan.nextInt()][scan.nextInt()]; is not even legal.