-1

I need some help with some code. I am trying to reverse a string which is entered by the user. Could someone have some experience with repl.it please help me.

Here is the code that I am using (ignore the first part of the code that already works)

Main

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;
    }
}

here is the long list of errors that I am getting

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
Bashir
  • 2,057
  • 5
  • 19
  • 44
eRrOR SaNs
  • 19
  • 2
  • Hi eRrOR SaNs, welcome to StackOverflow. It looks like all the lines with an error are outside of a method. In Java, only variable assignments can be done outside, so `System.out.println()` or `for` loops are invalid. See: https://stackoverflow.com/questions/18795467/system-out-println-statement-outside-any-method-in-java – Brydenr Mar 13 '20 at 15:50
  • Does this answer your question? [system.out.println statement outside any method in java](https://stackoverflow.com/questions/18795467/system-out-println-statement-outside-any-method-in-java) – Brydenr Mar 13 '20 at 15:50

3 Answers3

1
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();
    }
}
Ryan
  • 1,762
  • 6
  • 11
1

here is the fixed and final result

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 in = new Scanner(System.in);
  System.out.print("In:");
    String word = in.nextLine();
    //write your code below
    int counter = word.length();
    while(counter>0){
      System.out.print(word.charAt(counter-1));
      counter--;
    }

  }

}
eRrOR SaNs
  • 19
  • 2
0

the reason behind this errors that you are putting code outside the main method. every code that you want to execute must belong to the method public static void main.

this is how to fix it:

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);

        System.out.print("In:");
        String word = inp.nextLine();
        int counter = word.length();
        for(counter>0){
            System.out.println(word.charAt(counter));
            int counter-1;
        }

    }    
}
Bashir
  • 2,057
  • 5
  • 19
  • 44