-5

Here is the code, below. I really need help with this, as I have no clue what is wrong with it. It compiles cleanly, as I said, but whenever I run it, it fails to execute. Thanks!

import java.util.Scanner;
public class whatThe
{
    public static void main(String[ ] args){

     Scanner scan = new Scanner(System.in);

     int i;
     String s;
     char c;

     System.out.print("Please enter a number: ");
     i = scan.nextInt( );
     System.out.println(i);
     System.out.println( );

     System.out.print("Please input three words separated by spaces: ");
     s = scan.next();
     System.out.println(s);
     System.out.println( );

     System.out.print("Please input something:  ");
     s = scan.nextLine();
     System.out.println(s);
     System.out.println( );

     System.out.print("Please enter another number: ");
     i = scan.nextInt( );
     System.out.println(i);
     System.out.println( );

     System.out.print("Please input a word: ");
     s = scan.nextLine();
     c = s.charAt(0);
     System.out.println(c);
     System.out.println( );

     System.out.println("Good bye");

    }
}
MadProgrammer
  • 323,026
  • 21
  • 204
  • 329
arsb48
  • 513
  • 4
  • 9
  • 4
    *"but whenever I run it, it fails to execute"* - How? Does it generate a error? How are you running it? – MadProgrammer Oct 31 '15 at 02:15
  • Ugly, not DRY at all. You can do much better. – duffymo Oct 31 '15 at 02:20
  • What do you mean by dry? I'm new to java. – arsb48 Oct 31 '15 at 02:27
  • Also, when I say it fails to execute, it says : java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(String.java:646) at whatThe.main(whatThe.java:41) I have no clue what this means, and would be grateful if you could help me sort it out. – arsb48 Oct 31 '15 at 02:29
  • What did you enter? `StringIndexOutOfBoundsException` means that you tried to access a part of the string that didn't exist. If the index 0 doesn't exist, the string must be empty. – Arc676 Oct 31 '15 at 02:31
  • Possible duplicate of [Skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods) – resueman Oct 31 '15 at 02:32
  • I do not know what it means, just that is does not work. – arsb48 Oct 31 '15 at 02:36
  • 1
    DRY is an acronym for **Don't Repeat Yourself**, alluding to the many similar lines of code in your main function. – Erik Oct 31 '15 at 03:42

2 Answers2

0
package javaapplication9;
import java.util.Scanner;
import java.io.BufferedInputStream;
public class dumb
{
     public static void main(String[ ] args){

         Scanner scan = new Scanner(System.in);

         int i;
         String s;
         char result;        


         System.out.print("Please enter a number: ");
         i = scan.nextInt( );
         System.out.println(i);
         System.out.println( );

         System.out.print("Please input three words separated by spaces: ");
         s = scan.next();
         System.out.println(s);
         scan.next();


         System.out.print("Please input something:  ");
         scan.nextLine();
         s = scan.nextLine();
         System.out.println(s);
         System.out.println( );

         System.out.print("Please enter another number: ");
         i = scan.nextInt( );
         System.out.println(i);
         System.out.println( );

         System.out.print("Please input a word: ");
         scan.nextLine();
         s = scan.nextLine();
         result = s.charAt(0);
         System.out.println(result);

         System.out.println( );

         System.out.println("Good bye");

        }
    }
0

Try this:

    Scanner scan = new Scanner(System.in);
int i;
String s;
char c;
System.out.print("Please enter a number: ");
i = scan.nextInt( );
scan.nextLine();
System.out.println(i);
System.out.println( );

System.out.print("Please input three words separated by spaces: ");
s = scan.nextLine();

System.out.println(s);
System.out.println( );

System.out.print("Please input something:  ");
s = scan.nextLine();
System.out.println(s);
System.out.println( );

System.out.print("Please enter another number: ");
i = scan.nextInt( );
scan.nextLine();
System.out.println(i);
System.out.println( );

System.out.print("Please input a word: ");
s = scan.nextLine();
c = s.charAt(0);
System.out.println(c);
System.out.println( );

System.out.println("Good bye");

So, after each scan.nextInt(), do scan.nextLine() so you can proceed.

Sekula1991
  • 278
  • 4
  • 16
  • Thank you very much! This worked well. I see now what I did wrong, and I appreciate it a lot. – arsb48 Oct 31 '15 at 14:06