Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is my main code which gets the String from the user:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner inp = new Scanner(System.in);

        System.out.println("enter a number");

        int i = inp.nextInt();

        int o =i+i;

        System.out.println(o);

    }

    Scanner inp = new Scanner(System.in);

    System.out.print("In:");

    String word = inp.nextLine();

    int counter = word.length();

    for(counter>0){

        System.out.println(word.charAt(counter));

        int counter-1;

    }

}

This are the errors I get:

exit status 1

Main.java:16: error: <identifier> expected    

    System.out.print("In:");   

                    ^    

Main.java:16: error: illegal start of type    

    System.out.print("In:");    

                     ^    

Main.java:19: error: illegal start of type   

    for(counter>0){    

    ^    

Main.java:19: error: <identifier> expected    

    for(counter>0){    

               ^    

Main.java:19: error: ';' expected    

    for(counter>0){    

                ^    

Main.java:19: error: illegal start of type    

    for(counter>0){    

                 ^    

Main.java:19: error: <identifier> expected    

    for(counter>0){    

                  ^    

Main.java:19: error: ';' expected    

    for(counter>0){    

                   ^    

Main.java:20: error: illegal start of type    

      System.out.println(word.charAt(counter));    

            ^    

Main.java:20: error: ';' expected    

      System.out.println(word.charAt(counter));   

                ^    

Main.java:20: error: invalid method declaration; return type required    

      System.out.println(word.charAt(counter));    

                 ^   

Main.java:20: error: <identifier> expected    

      System.out.println(word.charAt(counter));    

                                    ^    

Main.java:20: error: ';' expected    

      System.out.println(word.charAt(counter));    

                                     ^    

Main.java:20: error: illegal start of type    

      System.out.println(word.charAt(counter));   

                                            ^   

Main.java:20: error: <identifier> expected    

      System.out.println(word.charAt(counter));   

                                             ^   

Main.java:21: error: ';' expected    

    int counter-1;    

               ^   

Main.java:24: error: class, interface, or enum expected  

    }   

    ^    

17 errors

Can anyone tell me how to reverse a String on repl.it? 

1 Answer

0 votes
by (19.7k points)

Check the below code: 

public static void main(String [] args) {

    try (Scanner scanner = new Scanner(System.in)) {

        System.out.println("In: ");

        String word = scanner.nextLine();

        System.out.println(new StringBuilder(word).reverse());

    } catch (Exception e) {

        e.printStackTrace();

    }

}

Interested in Java? Check out this Java tutorial by Intellipaat.    

Related questions

0 votes
1 answer
asked Jul 18, 2019 in Java by Shubham (3.9k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Feb 8, 2021 in Java by dante07 (13.1k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...