Java : Create 1 method to enter 5 numbers into a static array - java

I am stuck about an exercise. I have to create 2 methods.
For the first method, I would like to enter 5 numbers (via an input)
public static void enterNumber(int[] tab, int numeral){
Scanner input = new Scanner (System.in);
int number = 0;
int y = 0;
for(int i = 0; i<tab.length; i++){
System.out.print("Entrer number " + (i+1) + " : ");
number = input.nextInt();
tab[y++] = tab[i];
}
}
Then, I create display method to display the numbers.
public static void display(int[] tab, int numeral){
for(int x = 0; x<tab.length; x++){
System.out.println(tab[x]);
}
}
I called my methods:
int[] tab = new int[5];
int number = 0;
enterNumber(tab, number);
display(tab, number);
In my input I have this:
Entrer number 1 : 2
Entrer number 2 : 4
Entrer number 3 : 3
Entrer number 4 : 9
Entrer number 5 : 1
However, in my display, I only get the value 0 why ? I have to retrieve the values 2,4,3,9,1.
I don't understand.
0
0
0
0
0
Thank you for your help

You're never actually assigning the input value into your array.
tab[y++] = tab[i];
should be
tab[i] = input.nextInt();
You can get rid of the numeral parameter and the y and numebr variables, you're not using them. You should also close the scanner.

Related

How to record each input separately in a loop separately? (Java)

My problem is that only the 5th input gets printed while the rest is not
Scanner ns = new Scanner(System.in);
int n = 0;
int i=1;
while(i<=5)
{
System.out.println("enter a number");
n = ns.nextInt();
i++;
}
System.out.println(+n);
System.out.println(+n);
System.out.println(+n);
System.out.println(+n);
System.out.println(+n);
Let's say I typed 1, 2, 3, 4, 5 respectively,
it should look like this
1
2
3
4
5
But instead I get
5
5
5
5
5
You could store your input parameters into ArrayList
public static void main(String[] args) {
Scanner ns = new Scanner(System.in);
int n = 0;
int i = 1;
List<Integer> params = new ArrayList<>();
while (i <= 5) {
System.out.println("enter a number");
n = ns.nextInt();
params.add(n);
i++;
}
for (Integer param : params) {
System.out.println(param);
}
}
Output:
1
enter a number
2
enter a number
3
enter a number
4
enter a number
5
1
2
3
4
5
All the inputs are being printed, the problem is that the while loop sets n to the value of 5 and then you print 5 five times. The correct code you are looking for is:
int n = 0;
int i=1;
while(i<=5)
{
System.out.println("enter a number");
n = ns.nextInt();
i++;
System.out.println(+n);
}
Please print number in loop. See code below :
Scanner ns = new Scanner(System.in);
int n = 0;
int i=1;
while(i<=5)
{
System.out.println("enter a number");
n = ns.nextInt();
System.out.println(n);
i++;
}

Does anyone have an idea of printing the values in reverse in this code I wrote?

Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, the program should: output the individual digits of 3456 as 3 4 5 6 and the sum as 18, output the individual digits of 8030 as 8 0 3 0 and the sum as 11, output the individual digits of 2345526 as 2 3 4 5 5 2 6 and the sum as 27, and output the individual digits of 4000 as 4 0 0 0 and the sum as 4.
Moreover, the computer always adds the digits in the positive direction even if the user enters a negative number. For example, output the individual digits of -2345 as 2 3 4 5 and the sum as 14.
This is the question I'm having minor difficulties with, the only part I can't figure out is how can I print the single integers in the order that he wants, from what I learned so far I can only print them in reverse. Here's my code:
import java.util.*;
public class assignment2Q1ForLoop {
static Scanner console = new Scanner (System.in);
public static void main(String[] args) {
int usernum, remainder;
int counter, sum=0, N;
//Asaking the user to enter a limit so we can use a counter controlled loop
System.out.println("Please enter the number of digits of the integer");
N = console.nextInt();
System.out.println("Please enter your "+N+" digit number");
usernum = console.nextInt();
System.out.println("The individual numbers are:");
for(counter=0; counter < N; counter++) {
if(usernum<0)
usernum=-usernum;
remainder = usernum%10 ;
System.out.print(remainder+" ");
sum = sum+remainder ;
usernum = usernum/10;
}
System.out.println();
System.out.println("the sum of the individual digits is:"+sum);
}
}
You have to storeremainder variables in an array and then print them in the loop from last index to first as shown in this tutorial.
You can either store digits in array and then print them, or you can try something like that:
final Scanner console = new Scanner(System.in);
System.out.println("Please enter your number");
final int un = console.nextInt();
long n = un > 0 ? un : -un;
long d = 1;
while (n > d) d *= 10;
long s = 0;
System.out.println("The individual numbers are:");
while (d > 1) {
d /= 10;
final long t = n / d;
s += t;
System.out.print(t + " ");
n %= d;
}
System.out.println();
System.out.println("the sum of the individual digits is:" + s);
An idea would be : convert int to string and write a method
getChar(int index) : String
which gives you for example 4 from 3456 with
getChar(2);
See Java - Convert integer to string
Here, I wrote a code for your problem with using a stack. If you want a simple code, you can comment my solution and I will wrote another one.
Scanner c1 = new Scanner(System.in);
System.out.print("Enter the number: ");
int numb = c1.nextInt();
numb = Math.abs(numb);
Stack<Integer> digits = new Stack<Integer>();
while(numb>0){
int n = numb%10;
digits.push(n);
numb = numb/10;
}
int sum = 0;
while(!digits.isEmpty()){
int n = digits.pop();
sum+=n;
System.out.print(n+" ");
}
System.out.print(sum);

How to generate a right angle Fibonacci Series pyramid in Java?

I want to generate an output like this :-
0
0 1
0 1 1
0 1 1 2
0 1 1 2 3
0 1 1 2 3 5
However, i am trying in this way to achieve , but some piece of logic is missing which i am unable to decipher.
Here's what i am trying :-
import java.util.Scanner;
class Fibonacci
{
public static void main(String arr[])
{
System.out.println("Enter a no.");
Scanner input=new Scanner(System.in);
int num=input.nextInt();
int x=0,y=1;
for(int i=0;i<=num;i++)
{
for(int j=0;j<i;j++)
{
System.out.print(j);
}
System.out.println("");
}
}
}
And it generates the output like this (consider num=6)
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
What logic is required to get the desired output ? Would be thankful if anyone can explain me this :)
Thanks in advance !!
You need to change logic of inner loop like this by adding two previous number to current number and swap them like this.
import java.util.Scanner;
class Fibonacci
{
public static void main(String arr[])
{
int x = 0, y = 0, c = 0;
System.out.println("Enter a no.");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
for (int count = 0; count < num; count++) {
System.out.print(0);
x = 0;
y = 1;
c = 0;
for (int i = 1; i <= count; i++) {
c = x + y;
y = x;
x = c;
System.out.print(" " + c);
}
System.out.println();
}
}
}
First two numbers 0 and 1 are given , no need to caculate .
Use a String to save previous line string .
Caculate next numbers , add it into your previous line string .
Here is an example :
int x = 0 , y = 1;
int num = 6;
System.out.println("0");
System.out.println("0 1");
String str = "0 1";
for(int i = 2 ; i < num ; i ++){
int amt = x + y ;
x = y;
y = amt;
str += " " + amt;
System.out.println(str);
}
In a Fibonacci series only the first two numbers are provided which are 0 and 1. The next number of the series are calculated by adding the last two numbers. The series is limited by the user by providing the number of integers it wants in the series.
Logic: The logic behind creating a Fibonacci series is to add the two integers and save them in a new variable z = x+y and then replace the first integer value by the second integer and second integer value by their sum to move one step ahead in the series x=y adn y=z.
In your problem you want the series to be printed in a right angled triangle so you need to save the series that is already printed in a string
int n = 10;
System.out.println("0\n");
System.out.println("0 1\n");
int x = 0, y=1;
int i=2, z=0;
String str = "0 1";
while(i!=10)
{
z = x+y;
str += " " + z;
x=y;
y=z;
i++;
System.out.println(str);
}
Hope this helps

Using a while loop to compare to a value in a array

I need the program to run in a loop until 0 is entered. My code will end with 0 entered but when attempting to run the program with numbers entered it still ends the program. instead of running the numbers entered. The while loop is to keep the program running unless a 0 is entered.
import java.util.Scanner;
public class CountCompare {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the integers between 1 and 100 (0 to end, 0 < to exit): ");
int[] counts = new int[100];
// Count occurrence of numbers
count(counts);
while(counts[0] > 0){
// Display results
for (int i = 0; i < counts.length; i++) {
if (counts[i] > 0)
System.out.println((i + 1) + " occurs " + counts[i] +
" time" + (counts[i] > 1 ? "s" : ""));
}
System.out.print("Enter the integers between 1 and 100 : ");
// Count occurrence of numbers
count(counts);
}
System.out.print("\nEnd of run");
}
/** Method count reads integers between 1 and 100
* and counts the occurrences of each */
public static void count(int[] counts){
Scanner input = new Scanner(System.in);
int num; // holds user input
do {
num = input.nextInt();
if (num >= 1 && num <= 100)
counts[num - 1]++;
} while (num != 0);
}
}
I have posted the entire program.
output looks like this
Enter the integers between 1 and 100 (0 to end, <0 to exit):
23 23 4 5 6 7 8
0
4 occurs 1 time
5 occurs 1 time
6 occurs 1 time
7 occurs 1 time
8 occurs 1 time
23 occurs 2 times
Enter the integers between 1 and 100:
Your program still ends because:
int[] counts = new int[100];
You have defined the limit of the counts here. This means your loop will run
for (int i = 0; i < counts.length; i++)// counts.length=100;
So as far as you code suggest you want to end the user input when user input 0. So you might do this:
int x=1;
int y;
Scanner sc= new Scanner(System.in);
while(x!=0){
System.out.println("Enter your values");
y=sc.nextInt();
if(y==0){
x=0;
}
else{
System.out.println("You entered "+y);
}
int count=new Scanner(System.in).nextInt();
int myarray[]=new int[count];
for(int tmp=0; tmp<count;)
myarray[tmp]=++tmp;
while(count != 0){
for(int inc=1; inc<=count; inc++){
System.out.println(inc + "times occur");
}
System.out.println("Enter 0 to exit");
count=new Scanner(System.in).nextInt();
}
You should take a look at the line
while(counts[0] > 0) {
and try to figure out what is the purpose of this while loop in the main method.

Taking input from user in a combined way

im working on a question where the input is given in the below format:
5
7
121
123
7
121
###
4
3
3
2
5
Explanation of the input:
The first number is N, here in example N=5 (N>1 and N<100000). the next are N lines with different numbers ranging from 1 to 100. then comes ###. then the next number is K, here in example K=4. the next are K lines with different values ranging from 1 to 100. the '###' is used to separate N and K inputs.
my question here is how do i take the input from the user using the '###'. and later how can i differentiate between N and K. Kindly help.
please find my code below. but im not able to figure the format to write this type of input. kindly help.
int n=0,N=0,k=0,y=0,K=0;
System.out.println("Enter 'N': ");
n=in.nextInt();
if(n>=1 && n<=100000) {
N=n;
}
int[] a1 = new int[N];
int[] x = new int[N];
System.out.println("Enter values of N: ");
for (int i=0;i<a1.length;i++) {
x[i] = in.nextInt();
}
for (int i=0;i<a1.length;i++) {
if(x[i]>=1 && x[i]<=5000) {
a1[i] = x[i];
}
}
System.out.println("Enter 'K': ");
k=in.nextInt();
if(k>=1 && k<=100) {
K=k;
}
int[] a2 = new int[K];
System.out.println("Enter values of K: ");
for (int i=0;i<a2.length;i++) {
a2[i] = in.nextInt();
}
You should be able to simply use in.skip("###") before reading k to skip the line with the separator.

Categories

Resources