Results 1 to 11 of 11
  1. #1
    synister1337 is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default User string input?

    Alright. I'm working on a homework assignment for my CS class, and am having problems getting my program to read user input into a String. I've seen multiple things on JavaDocs and other places about the nextLine method for the scanner class, including examples that I've followed, but this just doesn't work for me. It completely skips that part and doesn't ask for user input.

    Here's the important parts of my code. It's kinda messy and uncommented, but I'm a beginner. The purpose of this program is to read a user's sentence, then convert it to all lower case and replace all vowels with spaces. Line 11 has the .nextLine() method. Anyone know what I'm doing wrong here?
    Java Code:
    import java.util.*;
    
    class stringConverter
    {
        public static void main (String[] args)
        {
    
    Scanner in = new Scanner(System.in);
    
    System.out.println("Please enter a sentence:");
    String sentence = in.nextLine();
    String lowercase = sentence.toLowerCase();
    String str1 = lowercase.replace("a", " ");
    String str2 = str1.replace("e", " ");
    String str3 = str2.replace("i", " ");
    String str4 = str3.replace("o", " ");
    String str5 = str4.replace("u", " ");
    System.out.println("You entered: " + sentence);
    System.out.println("The converted text is: " +str5);
    
         }
    }

  2. #2
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: User string input?

    This code works. Whats the problem?

  3. #3
    synister1337 is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: User string input?

    That's really weird. That small bit of my full code works, but it won't work in the full program, even though I can find nothing wrong with it. Here's the full program. That small bit came just copied and pasted from case 3 in the switch statement. Maybe there's a problem i'm not seeing in it.
    Java Code:
    import java.util.*;
    
    class homework1
    {
      public static void main (String[] args)
      {
        Scanner in = new Scanner(System.in);
        int input;
        
        do {
          
        System.out.println("Main Menu");
        System.out.println("Please select one of the following (by typing the number):");
        System.out.println("1 - Circle Calculations");
        System.out.println("2 - Print AND table for 3 variables");
        System.out.println("3 - Vowel Removal Utility");
        System.out.println("4 - Quit");
        
        input = in.nextInt();
        if ((input == 1) || (input == 2) || (input == 3) || (input == 4))
        {
         
        int a,b,c,y;
        switch (input)
        {
          case 1:
            System.out.print("Enter the radius of the circle: ");
            double radius = in.nextDouble();
            double circumference = 2.0*Math.PI*radius;
            double area = Math.PI*radius*radius;
            System.out.printf("The circumference of this circle is %.2f.\nThe area of this circle is %.2f.\n\n", circumference, area);
            break;
          case 2:
            System.out.println("A B C  Y");
            System.out.println("--------");
        
            for (a=0;a<=1;a++)
            {
              for(b=0;b<=1;b++)
              {
                for (c=0;c<=1;c++)
                {
                  y=(a&b&c);
                  System.out.printf("%d %d %d  %d\n", a,b,c,y);
                }
              }
            }
            System.out.println();
            break;
          case 3:
            System.out.println("Please enter a sentence:");
            String sentence = in.nextLine();
            String lowercase = sentence.toLowerCase();
            String str1 = lowercase.replace("a", " ");
            String str2 = str1.replace("e", " ");
            String str3 = str2.replace("i", " ");
            String str4 = str3.replace("o", " ");
            String str5 = str4.replace("u", " ");
            System.out.println("You entered: " + sentence);
            System.out.println("The converted text is: " +str5);
            System.out.println();
          break;
        }
        }
        else
        {
          System.out.println("Invalid entry. Please try again.");
        }
        }
        while (input != 4);
          
      }
    }

  4. #4
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: User string input?

    The code still works. What's the problem? what kid of problem am I looking at ????????????

  5. #5
    synister1337 is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: User string input?

    Every time I run the option for case 3, it skips the entire input and doesn't prompt me for it. It's really weird. D:

  6. #6
    synister1337 is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: User string input?

    What it's supposed to do is prompt you to enter a sentence, it then makes it all lowercase and removes the vowels. Instead, all it's doing is skipping that prompt and going back to the menu, like it does when you finish the other options.

  7. #7
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: User string input?

    Read about the .nextline(), and tell me what will happen when you add a new line with sentence = in.nextLine(); under the current one. See if you can figure out the problem.

  8. #8
    synister1337 is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: User string input?

    Alright. I figured out what the problem was. For some reason unknown to me, it wasn't keeping the Scanner object through case 3. I made a new scanner called console within the case, and used it to read the nextLine input. Thanks for the feedback, at least. lol. Here's the finished code that works.
    Java Code:
    import java.util.*;
    
    class homework1
    {
      public static void main (String[] args)
      {
        Scanner in = new Scanner(System.in);
        int input;
        
        do {
          
        System.out.println("Main Menu");
        System.out.println("Please select one of the following (by typing the number):");
        System.out.println("1 - Circle Calculations");
        System.out.println("2 - Print AND table for 3 variables");
        System.out.println("3 - Vowel Removal Utility");
        System.out.println("4 - Quit");
        
        input = in.nextInt();
        if ((input == 1) || (input == 2) || (input == 3) || (input == 4))
        {
         
        int a,b,c,y;
        String sentence;
        switch (input)
        {
          case 1:
            System.out.print("Enter the radius of the circle: ");
            double radius = in.nextDouble();
            double circumference = 2.0*Math.PI*radius;
            double area = Math.PI*radius*radius;
            System.out.printf("The circumference of this circle is %.2f.\nThe area of this circle is %.2f.\n\n", circumference, area);
            break;
          case 2:
            System.out.println("A B C  Y");
            System.out.println("--------");
        
            for (a=0;a<=1;a++)
            {
              for(b=0;b<=1;b++)
              {
                for (c=0;c<=1;c++)
                {
                  y=(a&b&c);
                  System.out.printf("%d %d %d  %d\n", a,b,c,y);
                }
              }
            }
            System.out.println();
            break;
          case 3:
            Scanner console = new Scanner(System.in);
            System.out.println("Please enter a sentence:");
            sentence = console.nextLine();
            String lowercase = sentence.toLowerCase();
            String str1 = lowercase.replace("a", " ");
            String str2 = str1.replace("e", " ");
            String str3 = str2.replace("i", " ");
            String str4 = str3.replace("o", " ");
            String str5 = str4.replace("u", " ");
            System.out.println("You entered: " + sentence);
            System.out.println("The converted text is: " +str5);
            System.out.println();
          break;
        }
        }
        else
        {
          System.out.println("Invalid entry. Please try again.");
        }
        }
        while (input != 4);
          
      }
    }

  9. #9
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: User string input?

    This was not the problem, now you have 2 scanner objects. I have seen this task from 3 different users today, your classmates? Never mind do as you like or check my previous post ;) I'm going to bed.

  10. #10
    synister1337 is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: User string input?

    Even when I ran it in debug mode straight through, it just skipped prompting the user for input. I don't know what the problem was that happened, but when I created the second scanner object and used that, it worked just fine.

  11. #11
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: User string input?

    You should find out, you will learn from it... Now its like you had a car, but a light did not work on your car, so you brought a new one in staid of finding out why it does not work.
    if you add a new like with sentence = console.nextLine(); in the other version i't will also work. why?

Similar Threads

  1. User Input???
    By jonytek in forum New To Java
    Replies: 8
    Last Post: 01-13-2013, 03:52 PM
  2. How do i delete a section of a user inputed string that i input
    By DevenGosalia514 in forum New To Java
    Replies: 8
    Last Post: 03-18-2011, 05:02 AM
  3. Help with user input
    By sconniegorilla in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 03:00 PM
  4. User Input
    By brmcdani in forum New To Java
    Replies: 2
    Last Post: 02-05-2010, 02:59 AM
  5. Prompting user input of a string.
    By apfroggy0408 in forum New To Java
    Replies: 3
    Last Post: 03-09-2008, 07:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •