1

So, the code works just fine but I want to know if there's a way to read a string from terminal without reading the "enter", I first read a int, totalWords, let's say 2, then I want to read 2 Strings, so I put it in a Loop, but the first String to be read is always "". In C I usually would just use getchar().

import java.util.Scanner;

public class WayTooManyWords {
    public static void main(String[] args){
        Scanner readVar = new Scanner(System.in);
        String[] words;
        String trash;
        int totalWords = readVar.nextInt();   
        trash = readVar.nextLine();     
        words = new String[totalWords];
        for (int i = 0; i < totalWords; i++) {
            words[i] = readVar.nextLine();
        }
        
        for (int i = 0; i < totalWords; i++) {
            if(words[i].length() > 10){
            System.out.print(words[i].charAt(0));
            System.out.print(words[i].length() - 2);
            System.out.print(words[i].charAt(words[i].length()-1));
            System.out.println();
            }
            else{
                System.out.println(words[i]);
            }
        }
        //readVar.close();
    }
}

I created a variable just for the sake of getting rid of this but I'm not sure if that's good, it worked though. I tried resetting readVar as well but that didn't work.

Thay
  • 21
  • 2
  • 5
    `I created a variable just for the sake of getting rid of this` You don't need to create a variable. You can just read without storing the result. – tkausl Dec 16 '22 at 06:11

0 Answers0