0
import java.util.Scanner;
public class Project2 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String word;
        String c;
        int x, count, count1;
        System.out.println("Please enter a word:");
        word=in.nextLine();
        x=word.length();
        for(count=0;count<x;count++) {
            count1=x;
            count1--;
            c=word.substring((count1)-1,count1);
            System.out.println(c);
        }
    }
}

All this program does is print out the second to last character of the word that the user enters. I'm confused as to why it is doing this and want to know how to print out the whole word backwards. Someone help please.

Andreas
  • 2,455
  • 10
  • 21
  • 24

6 Answers6

2

You don't need a loop to reverse a string.

Ref - StringBuilder#reverse

Scanner in = new Scanner(System.in);
System.out.println(new StringBuilder(in.nextLine()).reverse());

If you want to print characters in reverse, then forget the substring-ing.

String word = in.nextLine();
int x = word.length();
for(count = x - 1; count >= 0; count--) {
    System.out.println(word.charAt(count));
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    this is not an answer to the question.. if an alternative was to be provided, we could direct the OP to [Reverse a string in Java](https://stackoverflow.com/questions/7569335/reverse-a-string-in-java) – Kartik Nov 14 '18 at 06:10
  • Perhaps, but they did ask ultimately say *want to know how to print out the whole word backwards*, so your choice... – OneCricketeer Nov 14 '18 at 06:11
1

Take count1=x; assignment out of the loop. Also make count--; after printing the letter.

Jignesh M. Khatri
  • 1,407
  • 1
  • 14
  • 22
1

You are correct up until x = word.length(). It is printing the second from last character because you keep setting the value of count1 to length of word and you substract it by 1. Therefore, it keeps referring to the second last character. To fix that, do the following instead:

count1=x;
for(count=0;count<x;count++) {
    c=word.substring((count1)-1,count1);
    System.out.println(c);
    count1--;
}
Andreas
  • 2,455
  • 10
  • 21
  • 24
  • Please remember to accept the answer that suits your question the most, so StackOverflow can archive it properly. – Andreas Nov 14 '18 at 06:11
1

If at all you want to do it the hard way by traversing, do the following changes.

for(count=x;count>=0;count--) {
   System.out.println(word.substring(count - 1,count));
}

Update: You can use charAt#String to easily get the character at some position.

for(count=x-1;count>=0;count--) {
       System.out.println(word.charAt(count));
}
jack jay
  • 2,493
  • 1
  • 14
  • 27
1

Every time the loop is running, you are resetting the count1 value to x (count1=x). So c will always be the same value. To make this work, try taking count1 = x out of the loop so that every time the loop is running, count1 value will be reduced as expected providing the required sub-string.

RrR-
  • 1,251
  • 3
  • 15
  • 32
1

Into the loop for(count=0;count<x;count++) Every loop you did the same thing

count1=x;
count1--;
c=word.substring((count1)-1,count1);
System.out.println(c);

This block has no relation with the loop!

Thats why you are getting the second last character!

To fix this:

Solution 1: (Just reverse the String)

word=in.nextLine();
System.out.println(new StringBuilder(word).reverse());

or Solution 2: (Using loop using your code)

x=word.length();
for(count= x-1; count >= 0; count--) {
    c = word.substring((count)-1, count);
    System.out.print(c);
}
Zico
  • 2,349
  • 2
  • 22
  • 25