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 1 year ago.
Improve this question
I need to print an array's element with it's index next to it. If the array element is even, it must display the element and its correlating index number, if the element is odd, it must display the element and "1".
For example:
Input: int[] array = new int [25]
Output: 0:0, 1:1, 2:2, 3:1, 4:4, 5:1, 6:6, 7:1 and so on
How would I do this?
You could do something like, You can check if a number is even then print the number and index separated by a colon else print number and 1 separated by a colon, This can be achieved by using for loop construct.Would recommend you to follow proper guide/tutorial as you look like a beginner in java.
public static void main(String[] args) {
int[] array = {1,2,3,4,5};
int counter = 0;
for(int i : array) {
if(i % 2 == 0)
System.out.print(i+":"+counter);
else
System.out.print(i+":1");
if(counter<array.length-1)
System.out.print(", ");
counter++;
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
If there is a given
Inputs
String "abcdaa/efgh/hidjk/lmno/pqrs/tuvw" and if
int slashCounter=3,
The desired Output should be -
Output: abcdaa/efgh/hidjk/lmno
(Basically if slashCounter=3 only alplabets upto 4th '/' is allowed. From fourth '/'everything is ignored. Below is the few Input and Output. (There may be any number of alphabets between '/' to '/'). Below is few more inputs
Input:
String aaabcd/efgh/hidjk/lmno/pqrs/tuvw
if int slashCounter=2
Output: aaabcd/efgh/hidjk
Input:
String aaabcd/efgh/hidjk/lmno/pqrs/tuvw
if int slashCounter=4
Output: aaabcd/efgh/hidjk/lmno/pqrs
Could someone help me with the logic of this in JAVA. Thanks in advance.
This is how you do.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
// till how much we want the string
int display = sc.nextInt();
// splits the String when "/" is encounter and then stores it in 0 index and then increases the index
String aplhabets[] = input.split("/");
// to add the String we want
StringBuilder sb = new StringBuilder();
for(int i = 0 ; i <= display; i++) {
sb.append(aplhabets[i]+"/");
}
// as we dont want the last "/" of the String we just print from 0 to string length - 1, this will remove teh last "/"
System.out.print(sb.substring(0, sb.length()-1));
}
Output:
aaabcd/efgh/hidjk/lmno/pqrs/tuvw
2
aaabcd/efgh/hidjk
String aplhabets[] = input.split("/") this splits the String and puts it in the array whenever / is encounter.
sb.substring(0, sb.length()-1) this cause when we were appending String builder from he loop it's adding / in the end.
So in order to remove last / we do sb.substring(0,sb.length-1) where first parameter is start index and second index is end index
in substring(int start, int end) start in inclusive and end is exclusive.
this is how you do the problem. Suggest you learn how to use arrays and String manipulation. this will surely benefit you.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I've tried looking for the answer to this question, but I haven't had a lot of luck. Basically I am asked to create the Fibonacci Code, and then allow a user input to look for there input in the sequence. If its in the sequence then it shows what index. If its not then it shows the two closest numbers to it.
So if a user inputs 4, the nearest elements would be 3 and 5 and the indexes would be 4 and 5.
I'm basically struggling with finding the nearest elements. I'm not exactly sure how to do it.
****update****
So I did figure it out thank you
1.Store the previous Fibonacci number in a buffer (you can initialize with -1)
2.update the buffer after every new number is calculated.
3.if the current number is not equal to the new number
3.A check if the number is greater than buffer and less than the new number
3.A.1)If yes, those two are your nearest numbers.
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
int userInput;
Fibonacci.fibonacciAlgor(5);
}
public static void fibonacciAlgor(int userInput)
{
int i=0;
int buffer=-1;
int x=0,y=1;
System.out.println("Input: " + userInput);
while(i<1000000){
if(x==userInput){
System.out.println("Belongs to sequence: Yes "");
break;
}
else{
if(userInput>buffer&&userInput<x){
System.out.println("Belongs to sequence: No ");
System.out.println("Nearest Elements: " + buffer+","+x);
break;
}
}
buffer=x;
int temp=y;
y=x+y;
x=temp;
i++;
}
}
}
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 new in Java and I have a task that's going like this:
Generate one number between 0-9 and a second number between 10-99.
You need to check how many times the first number appears in the second number.
(For example: The first number is 5 and the second one is 55 so the first number (5) apears 2 times in the second number (55) ).
Btw, you need to make it without arrays.
package randomNumber_01;
import java.util.Random;
public class third {
public static void main (String[] args){
Random n = new Random();
int first = n.nextInt(9)+1;
System.out.println("First numnber: "+first);
int second = n.nextInt(99)+10;
System.out.println("Second number: "+second);
System.out.println("I stuck here");
}
}
int count=0;
if((second%10)==first){
count++;
}
if(Math.round((second/10))==first){
count++;
}
System.out.println(first+" occurs "+count+" times in "+second);
Maybe this can help you ;), replace your last println by this 8 lines ;)
but as shmosel sait in comment, be carreful it's
int first = n.nextInt(10);
int second = n.nextInt(90)+10;
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 8 years ago.
Improve this question
I am trying to validate input values to pass only integers that are divisible by 10. The code below is failing.
public static void main(String args[]) {
Scanner scan =new Scanner(System.in);
ArrayList<Integer> liste = new ArrayList<Integer>(); // I have filled my array with integers
int x=scan.nextInt();
int y=x%10;
do{
if(y==0){
liste.add(x);}
else if(y!=0){
System.out.println("It is not valid"); continue;
}
else
{System.out.println("Enter only integer"); continue;
}
}while(scan.hasNextInt()); }
System.out.println(liste);
System.out.println("Your largest value of your arraylist is: "+max(liste));
You're calling scan.nextInt() twice. Each time you call it, it will read another int from the input. Thus, if your input was something like
10
5
13
then the 10 would pass the scan.nextInt()%10==0 check, and then 5 would be added to the list. Store the result of scan.nextInt() in a variable first, so the value won't change.
Instead of
if(scan.nextInt()%10==0){
liste.add(scan.nextInt());}
do
int num = scan.nextInt();
if(num%10 == 0){
liste.add(num);
}
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 9 years ago.
Improve this question
i have been working on a project that would scan a textfile line per line and at each line, each word in the line will be stored in an array
here is my code as of now. when it comes to the storing from the Answer.txt file, an error occurs. can someone please help me?
try
{
String s = sc.nextLine();
//System.out.println(s);
String[] Question = s.split(" ");
for(int i=0;i<=Question.length;i++)
{
System.out.println(Question[i]);
}//debug
s = sc2.nextLine();
//System.out.println(s2);
String[] Answer = s.split(" ");
for(int c=0;c<=Answer.length;c++)
{
System.out.println(Answer[c]);
}//debug
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("...");
}
You're probably getting ArrayIndexOutOfBounds exception.
for(int i=0;i<=Question.length;i++)
Should be:
for(int i=0;i<Question.length;i++)
^
(The same for the other loop).
Why?
Remember that arrays are zero-based in Java. So if you have an array of size N, the indexes will be from 0 to N - 1 (total sum of N).
You can avoid the index counting all together with an "foreach" loop.
for (String s: Question){
System.out.println(s);
}