sum of remaining array numbers after user input - java

hi guys i've had some help with this program today, basically what i want is for an array of 1 - 200 to be held, then the user inputs a number between 1 and 200.
The ramaning numbers are then added together and the answer outputted.
e.g. user enters 100, numbers from 100-200 are then added together and answer is output.
With the code i have so far it is always outputting 0 as the answer. Any ideas?
thanks.
//Importing scanner
import java.util.Scanner;
//Class name
class programfive{
//Main method
public static void main (String[]args){
//declaring and inizialising scanner
Scanner input = new Scanner (System.in);
//Declaring and inizialising variables
int userInput = 0;
int sum = 0;
//Array initializer
int array[] = new int [201];
//Prompt for user input
System.out.print("Please enter a value between 1 and 200: ");
userInput = input.nextInt();
//For loop - starts at number user inputted, stops when reaches 200, increments by 1.
for (int i = userInput; i<=200; i++)
{
sum += array[i];
}
System.out.println(sum);
}//End of main method
}//End of class

Because you haven't put anything in your array, it contains the default int value at each index, and that is 0 .
You have to fill it with the values you want, so that array[0] contains 0, array[1] contains 1, etc..
int array[] = new int [201];
for(int i=0; i<array.length;i++)
array[i] = i;
Also, you could get rid of the array and get the same result :
for (int i = userInput; i<=200; i++)
{
sum += i;
}

you need to initialize the array first, or changing the sum loop to:
for (int i = userInput; i<=200; i++)
{
sum += i;
}

Untested but should work:
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
int userInput = 0;
int sum = 0;
System.out.print("Please enter a value between 1 and 200: ");
userInput = input.nextInt();
for (int i = userInput; i<=200; i++)
sum += i
userInput.close();
System.out.println(sum);
}

you forgot to populate your array with numbers currently all of your array elements are pointing to the default value of 0.
add this line of code after the array declaration and your good to go:
for(int i=0;i<array.length;i++)
array[i]=i;

System.out.println("Enter five numbers: ");
Scanner scanner = new Scanner(System.in);
int[] array = new int [5] ;
for (int i =0; i < array.length; i++){
array[i] = scanner.nextInt();
}
int sum = 0;
for (int i : array){
sum = sum +i;
}
System.out.println(sum);

Related

how many times the array contains the value?

It is a simple program to find if input x value is in the array or not.
The user types numbers into the array, after that types a number to count how many times this numbers repeats in the array.
What I have:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] mas = new int[5];
for (int i = 0; i < mas.length; i++) {
System.out.print("Input of mas["+i+"]: ");
int n = sc.nextInt();
}
valueX(mas);
for (int i = 0; i < 10000; i++) {
System.out.println("Would you like to continue (1=yes, 0=no)?");
int n = sc.nextInt();
if (n==1) {
valueX(mas);
}
if (n==0) {
System.out.println("Program terminated");
sc.close();
break;
}
}
}
public static void valueX(int mas1[]){
Scanner scanner = new Scanner(System.in);
System.out.print("Input x: ");
int x =scanner.nextInt();
int count =0;
for (int i = 0; i < mas1.length; i++) {
if (x==mas1[i]) {
count++;
}
}
System.out.println("Value "+x+" appears "+count+" time(s) in the array.");
}
The valueX metod should do this work but it does not.
What I expect to get:
Input of mas[0]: 2
Input of mas[1]: 2
Input of mas[2]: 3
Input of mas[3]: 4
Input of mas[4]: 2
Input x: 2
Value 2 appears 3 time(s) in the array.
But what my code does:
Input of mas[0]: 2
Input of mas[1]: 2
Input of mas[2]: 3
Input of mas[3]: 4
Input of mas[4]: 2
Input x: 2
Value 2 appears **0** time(s) in the array.
Could you please find the mistake ?
You are not storing the input value in array so your array has all 0(default value of int) value, hence the issue
int[] mas = new int[5];
for (int i = 0; i < mas.length; i++) {
System.out.print("Input of mas["+i+"]: ");
int n = sc.nextInt();
mas[i] = n;
//^^^^^^^^
}

How Can this code be adjusted to input and print a list of numbers?

I'm trying to create a program that allows the user to input numbers into a array called "numArray", however an exception is thrown once the user inputs numbers into array. How can this be fixed?
import java.util.Scanner;
class ArrayNumList
{
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);
System.out.println("How many nums would you like to enter? ");
int n = in.nextInt();
System.out.println("Enter " + n + " nums");
int[] numArray = new int[n];
for(int i = 0; i <= n; i++)
{
int index = in.nextInt();
System.out.print(numArray[index]);
}
}
}
There is no index index if the user inserts a big enough number / negative number. Fix it by using the actual loop variable i like this:
for(int i = 0; i <= n; i++) {
numArray[i] = in.nextInt();
System.out.print(numArray[i]);
}
The problem is that you aren't storing your data anywhere in the array.
for(int i = 0; i <= n; i++)
{
int index = in.nextInt();
System.out.print(numArray[index]);
}
Every time you run through the array, you store your input into the variable index. From there, you try to display the value in your array at that index. This can lead to a few problems.
1) You haven't put your numbers into this array yet, so each value in the array is going to be 0. To fix this:
numArray[i] = in.nextInt();
2) If someone inputs a number that's bigger than the size of your array, you'll get an IndexOutOfBoundsException. I don't think you were trying to do it this way, but this is what your code is currently attempting to do.
There are 3 issues:-
1) Use i < n as you start from 0
2) Trying to add input value as index in the array.
3) Printing out the values while entering which create confusion for you better to do that in separate loop but I leave it to u or Better to use println instead of print
Try this:-
public static void main(String[]args) {
Scanner in = new Scanner(System.in);
System.out.println("How many nums would you like to enter? ");
int n = in.nextInt();
System.out.println("Enter " + n + " nums");
int[] numArray = new int[n];
for(int i = 0; i < n; i++)
{
numArray[i] = in.nextInt();
System.out.println(numArray[i]);
}
}
Firstly it should be i<n and not i<=n because n means the array length and i means the array index. The index starts from 0. For eg an array holds the values 1234, so the length is 4 but the index are 0,1,2,3. So the index is always one less than the array length. Also you have to store the input with the array index.
for(int i = 0; i < n; i++)
{
numArray[i] = in.nextInt();
}
And then later print it
for(int i = 0; i < n; i++)
{
System.out.println(numArray[i]);
}

Adding to arrays and printing arrays in Java

I need help figuring out how to get the user to input a number of integers no more than 10, and then add them to an array and print them out from the array. The code I have below, when run, asks the user for the integers and then runs forever and doesn't work. What am I doing wrong?
public static void main(String[] args) {
Scanner input = new Scanner(System.in); // create a new scanner
System.out.print("Enter integers between 1 and 100\n ");
int[] nextNumber = new int[10];
int i = 0;
int number = input.nextInt();
while (i < nextNumber.length){
i++;
nextNumber[i] = number;
number = input.nextInt();
}
int a = 0;
while (a < nextNumber.length){
a++;
System.out.println(nextNumber[a]);
}
Seems to me like you increment your index too fast. You should increment your index variables at the end of your loops, not the beginning.
I would suggest you use for loops instead since they are designed for that:
Scanner input = new Scanner(System.in); // create a new scanner
System.out.print("Enter integers between 1 and 100\n ");
int[] nextNumber = new int[10];
for (int i = 0; i < nextNumber.length; i++){
nextNumber[i] = input.nextInt();
}
for (int a = 0; a < nextNumber.length; a++){
System.out.println(nextNumber[1]);
}
Also, although I did not change it in the code, it seems like your last line should be:
System.out.println(nextNumber[a]);
Increment the array index after the values have been assigned to the arrays
while (i < nextNumber.length) {
number = input.nextInt();
nextNumber[i] = number;
i++;
}
The same applys to the second loop
while (a < nextNumber.length) {
System.out.println(nextNumber[a]);
a++;
}
Problem 1
Change
int i = 0;
int number = input.nextInt();
while (i < nextNumber.length){
i++; //here is one problem. you not assigning the value to nextNumber[0].
nextNumber[i] = number;
number = input.nextInt();
}
to
int i = 0;
while (i < nextNumber.length){
number = input.nextInt();
nextNumber[i] = number;
i++;
}
Problem 2
change
int a = 0;
while (a < nextNumber.length){
a++; //here is one problem ..You never get nextNumber[0] value
System.out.println(nextNumber[a]);
}
to
int a = 0;
while (a < nextNumber.length){
System.out.println(nextNumber[a]);
a++;
}
You can easily do this as follows. Better use for loop since you know the maximum number of iterations.
Scanner input = new Scanner(System.in); // create a new scanner
System.out.print("Enter integers between 1 and 100\n ");
int[] nextNumber = new int[10];
int i = 0;
while (i < nextNumber.length) nextNumber[i++] = input.nextInt();
int a = 0;
while (a < nextNumber.length) System.out.println(nextNumber[a++]);

User input arrays

I am trying to create a program that sorts user inputted integers from greatest to least. Also I need to find a way to print the maximum and minimum numbers. The code was sorting fine when I had defined values but now that I have switched it to user input it sends back "0"s for some reason. This is my code
import java.util.Scanner;
public class SortInteger {
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Please input three numbers");
int num = input.nextInt();
int number [] = new int [num]; //Sorting Array
int temp;
boolean correct = false; // Forces the sorting to continue till the numbers are in order
while(correct ==false){
correct = true;
for(int i = 0; i>number.length-1; i++ ){
if(number [i] > number [i+1]){
temp = number [i+1];
number [i+1] = number[i];
number[i]= temp;
correct = false;
}
}
}
for(int i = 0; i<number.length-1; i++){ //outputs the array to user
System.out.println(number[i]);
}
}
}
you have taken only one number
int num = input.nextInt();
and you are using it for array size :
int number [] = new int [num];
but in rest of the code you haven't taken any input so your array is empty.
Code::
import java.util.*;
class test{
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Please input three numbers");
int num1 = input.nextInt();
int num2 = input.nextInt();
int num3 = input.nextInt();
int number [] = {num1,num2,num3}; //Sorting Array
int temp;
boolean correct = false; // Forces the sorting to continue till the numbers are in order
while(correct ==false){
correct = true;
for(int i = 0; i<number.length-1; i++ ){
if(number [i] > number [i+1]){
temp = number [i+1];
number [i+1] = number[i];
number[i]= temp;
correct = false;
}
}
}
for(int i = 0; i<number.length; i++){ //outputs the array to user
System.out.println(number[i]);
}
}}
Output::
Please input three numbers
1
5
4
1
4
5
you are only initializing your array. you never initialized your array with elements, thus your array elements get default values.
System.out.println("Please enter size");
int num = input.nextInt();
int number [] = new int [num]; //Sorting Array
for(int i=0; i<number.length; i++){
System.out.println("Please enter element at index " + i);
number[i] = input.nextInt()
}
As others pointed out you need to populate your array with multiple values, currently you are requesting only one int.
Also, are you sure that works? Should't your first for loop read:
for(int i = 0; i<number.length-1; i++ )
Instead of:
for(int i = 0; i>number.length-1; i++ )
On a side note, it looks to me that your sorting algorithm is O(n2) you may want to look into mergesort and quicksort.

how to automatically populate a 2d array with numbers

Hi i am trying to auto populate a 2d array based on user input.
The user will enter 1 number, this number will set the size of the 2d array. i then want to print out the numbers of the array.
for example , if the user enters the number 4 . the 2d array will be 4 rows by 4 colums, and should contain the number 1 to 16, and print out as follows.
1-2-3-4
5-6-7-8
9-10-11-12
13-14-15-16
But i am struggling to think of the right statement that will do this.
for the moment my code just prints out a 2d array containing *.
Has anyone any ideas how i could print out the numbers , i'm really stuck.
my code follows:
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.println("Enter room length");
int num1 = input.nextInt();
int num2 = num1;
int length = num1 * num2;
System.out.println("room "+num1+"x"+num2+"="+length);
int[][] grid = new int[num1][num2];
for(int row=0;row<grid.length;row++){
for(int col=0;col<grid[row].length;col++){
System.out.print("*");
}
System.out.println();
}
}
Read n value,
int[][] arr = new int[n][n];
int inc = 1;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
arr[i][j] = inc++;
Well, first of all you have to fill the array with the numbers. You can use your double for loop for this and a counter variable which you increment after each loop of the inner for loop.
int counter = 1;
for(int x = 0; x < num1; x++)
{
for(int y = 0; y < num2; y++)
{
grid[x][y] = counter++;
}
}
Afterwards you can output the array again with a double for loop.
I am not sure if I understand you right.
You have problem with the code printing *?
If yes, then the reason for that is this
System.out.print("*");
Should be
System.out.print(grid[row]);
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter room length");
int arraySize = input.nextInt();
System.out.println("Length: " + (arraySize*arraySize));
int[][] array = new int[arraySize][arraySize];
int count = 1;
for (int i=0;i<arraySize;i++) {
for (int j=0;j<arraySize;j++) {
array[i][j] = count;
if (j != (arraySize-1))
System.out.print(count + "-");
else
System.out.println(count);
count++;
}
}
}
This code should print out the numbers how you want them.

Categories

Resources