0

I need to have the user input a size of the array in the variable n and then create a single dimension array of Strings and have it filled with sentences. I also need to make sure that each sentence ends with a full stop (a . ). so here's what I have:

import java.util.*;
class encryption{
        void main(){
                Scanner sc=new Scanner(System.in);
                System.out.println("Enter the number of sentences to be encrypted");
                int n=sc.nextInt();
                String[] a=new String[n];
                System.out.println("Enter the sentences");

                for(int i=0; i<n; i++){
                    a[i]=sc.nextLine();
                    while(a[i].charAt(a[i].length()-1)!='.'){
                        a[i]=sc.nextLine();                                                
                    }
                    if(i%2==0){
                        a[i]=rev(a[i]);
                    }
                    else{
                        a[i]=frwd(a[i]);
                    }

                }
                for(int i=0; i<n; i++){
                    System.out.println(a[i]);
                }
            }

After the inputs, I will work on the Strings using the functions rev(...) and frwd(...), and then print the changed strings.

It compiles fine, however upon execution, the first line in the first for loop a[i]=sc.nextLine(); doesn't seem work. It gives me a straight indexOutOfBounds error meaning it goes straight onto the while loop.

How can I make it accept input there?

JavaPilgrim
  • 65
  • 1
  • 6
  • 2
    You can add an extra `sc.nextLine()` to consume the last newline character. You might find reading [this](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) helpful. – Hardik Modha Jan 07 '17 at 08:17
  • @HardikModha that solved it! – JavaPilgrim Jan 07 '17 at 08:24

1 Answers1

0

After you take the number of sentences in the variable n, write this line of code:

int n=sc.nextInt();
sc.nextLine();
artenson.art98
  • 193
  • 1
  • 10