0
I need your help please .,java language
I want to reverse a char array And the word is given by the user. How can I read the char array from a user. https://code.sololearn.com/cZPrQL38O9Vj/?ref=app
28 Respuestas
+ 2
amal 01 use System.out.print(arr[i] ); for output the string without line break
+ 5
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String text = scanner.nextLine();
char[] arr = text.toCharArray();
for(int i=arr.length-1;i>=0;i--){
System.out.println(arr[i]);
}
}}
+ 3
amal 01 show your try
Attach your file here
Add a MWE
https://www.sololearn.com/post/75089/?ref=app
+ 3
amal 01
Everything works but just a small improvement in the for loop
for (int i = arr.length-1; i>=0; i--) {
Sopln(arr[I]) ;
} // you forgot this end bracket
+ 3
in the condition of the loop you should:
i>=0
also you forget this " } "
+ 2
How you read charecters? As a string or individual chars?
If later one, use a loop to read char by next().charAt(0) method of scanner object.
Try and post that tried code of unsolved....
+ 2
amal 01 i already given about it in my 2nd post..
May you not noticed.
System.out.println adds line break. While System.out.print() does not.
And use i>=0 condition.
+ 2
Jayakrishna🇮🇳
Thank you so much .
I did not noticed it.
+ 2
Inside for loop use i >= 0 then. Resolved
+ 2
amal 01 create a new question thread for your new question don't post all your questions in one post.
This way you can get help.
+ 2
amal 01
Strings are immutable (you cannot modify).
String Buffer is mutable version of strings. You can modify original. It has already answered lot times so search bar will give more answers. Creating new will become dublicate. Use search bar.
Hope it helps
Edit : for more info :
https://www.sololearn.com/discuss/724152/?ref=app
https://www.sololearn.com/discuss/577260/?ref=app
https://www.sololearn.com/discuss/141079/?ref=app
https://www.sololearn.com/discuss/2117193/?ref=app
https://www.sololearn.com/discuss/536275/?ref=app
https://www.sololearn.com/discuss/93036/?ref=app
https://www.sololearn.com/discuss/2100143/?ref=app
+ 1
amal 01 you forgot to add a link
+ 1
amal 01 scanner.next().charAt(0); reads a single charecter.
Ingores next charecters until spaee..
+ 1
Hasna
The problem is that of I entered for example amal.
The output should be lama.
So need an array of characters starting from the lenght -1 .
+ 1
hi amal 01 in your code:
in line 11 read array until index 0
change i>0 to i>=0
in line 12 change println to print for a single line
and line 13 close cicle for with }
+ 1
Consider the Java program below, and then answer the questions that follow
import java.util.Scanner;
public class question2{
Public static void main(String [] args){
Scanner in = new Scanner(System.in);
System.out.println(“Enter an Integer number”);
int n = in.nextInt();
int s = 0, i = 1;
do{
s + = Math.pow(i,2);
i++;
}
while(i <=n);
System.out.println(“Output = “+ s);
}
}
Explain what the Java program is doing 5 marks
Hence, give the Java class an appropriate name 2 marks
If n = 5, what will be the output from the program (show your working) 8 marks
Modify the program in 2(a) above to calculate S given that
n
S = ∑ i 3
i=i
I need someone to help with the answer to this question please?
0
Krish
Here is my code try.
I want to read a char array from the user but there isn't any way that I know.
0
Jayakrishna🇮🇳
As an individuals chars
Can I read char as a scanner object .
How so.???
0
Krish a link of what
0
amal 01 It works but Edit to this
for(int i=arr.length-1;i>=0;i--) //{ remove this brace.
System.out.print(arr[i]);
Condition use i>=0
And use System.out.print() method
Edit :
(to not put line break).