Results 1 to 17 of 17
  1. #1
    madatlis is offline Member
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default reading in 2 sentence for java, I am lost ...

    First of all i am using scanner class and string class, I have to ask the user how many scentence I have to use, witch that part is easy and have no poroblem with that, after that I enter a for loop and just ask the sentences, but the problem I am having is that:

    public static void main(String [] args)
    {
    Scanner console = new Scanner(System.in);
    int n;
    String a;
    String b;

    System.out.print("How many sentence would you like to type? ");
    n=console.nextInt();
    for (int i=1;i<=n;i++)
    {
    System.out.print("Please Enter your "+i+" sentence: ");
    if (i==1)
    { System.out.print("Please enter your 1 Number: ");
    b.console.nextLine();
    a=console.nextLine();
    System.out.println(a);
    }
    else
    a=console.nextLine();
    System.out.println(a);

    is the "a" string in the if statement when it outputs will not reconznie the "B" string and still prints out 2 lines, I jsut want it to print out 1 line, this particular code wont even print out b, it will go stright to a and print it 2 times in a row:

    Please Enter your 1 sentence: Please enter your 1 Number: today is great
    today is great
    today is great
    Please Enter your 2 sentence: 3as
    3as
    Please Enter your 3 sentence: f
    f

    Process completed. I just kind of trying to figure this out ... and im kind of lost, so please help me if you can.... the rest of them work, just the 1st sentence does not work, I have tried putting a.console.Next < --- and I also have tried to just put 1 string in there and it will not work right. Please Help me.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default

    Some comments:
    Add a unique comment at the beginning of all println()s so that you know which one printed the output.
    Add a println() to force the output to a new line
    The end of your program is missing.

    b.console.nextLine();
    Does this compile?

  3. #3
    madatlis is offline Member
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default

    it does compile, but this is the code:

    public static void main(String [] args)
    {
    Scanner console = new Scanner(System.in);
    int n;
    String a;
    String b;

    System.out.print("How many sentence would you like to type? ");
    n=console.nextInt();
    for (int i=1;i<=n;i++)
    {
    System.out.print("Please Enter your "+i+" sentence: ");
    if (i==1)
    { System.out.print("Please enter your 1 Number: ");

    a=console.nextLine();
    System.out.println(a);
    }
    else
    a=console.nextLine();
    System.out.println(a);


    and this is the output:
    How many sentence would you like to type? 3
    Please Enter your 1 sentence: Please enter your 1 Number:

    Please Enter your 2 sentence: what happenend ...
    what happenend ...
    Please Enter your 3 sentence: today is great
    today is great

    Process completed.

    trying to figure out why the 1st senctiece doesn't really work, I have tried adding another a=console.nextLine(); where the b.console.nextLine(); and hte output just will repeat the retarded senctences 2x, for example:

    How many sentence would you like to type? 3
    Please Enter your 1 sentence: Please enter your 1 Number: today is great
    today is great
    today is great
    Please Enter your 2 sentence: what the f
    what the f
    Please Enter your 3 sentence: asdf
    asdf

    Process completed.
    if someone could please help

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default

    it does compile, but this is the code:
    The end of the program is missing!!!!

    Add a unique comment at the beginning of all println()s so that you know which one printed the output.

  5. #5
    madatlis is offline Member
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default

    import java.util.*;
    public class Prog4test
    {

    public static void main(String [] args)
    {
    Scanner console = new Scanner(System.in);
    int n;
    String a;
    String b;

    System.out.print("How many sentence would you like to type? ");
    n=console.nextInt();
    for (int i=1;i<=n;i++)
    {
    System.out.print("Please Enter your "+i+" sentence: ");
    if (i==1)
    { System.out.print("Please enter your 1 Number: ");
    b=console.nextLine();
    a=console.nextLine();
    System.out.println(a);
    }
    else
    a=console.nextLine();
    System.out.println(a);

    }
    }


    } thats the whole program

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default

    The ending else is missing {}.
    Is the println() following a= console... part of the else or what?
    It is better to ALWAYS use {} with if{}else{} statements.
    Also don't put any code following a {. Leave the rest of the line blank.


    What are the following three lines of code supposed to do?
    b=console.nextLine();
    a=console.nextLine();
    System.out.println(a);
    What do you do with the contents of b?

  7. #7
    madatlis is offline Member
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default

    b=console.nextLine();
    a=console.nextLine();
    System.out.println(a);


    basicly the b= is a dummy line,

    I have to do a dummy line for it to actually read the whole thing ,but I am kinda lost on how to do a dummy line ..

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default

    What is a dummy line? Why do you need it? Perhaps if you commented the line:
    b=console.nextLine(); // skip this line because .....
    then everyone would know and not ask you about it.
    You don't need an extra variable b to do that. The following would do just as well:
    a=console.nextLine(); // skip this line because ...
    a=console.nextLine(); // get the data I need here

  9. #9
    madatlis is offline Member
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default

    when I just have 1 a=console.nextLine(); it does not output right .. it out puts like this:

    How many sentence would you like to type? 3
    Please Enter your 1 sentence: Please enter your 1 Number:

    Please Enter your 2 sentence: toda is
    toda is
    Please Enter your 3 sentence: grea d
    grea d

    Process completed.
    wich is not right ... and then when I put in 3 it doesn't work either it prints 2

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yous should read the Java doc, about nextLine()

    Java Code:
    public class ReadUserInputs {
        
        public static void main(String [] args) {
            Scanner console = new Scanner(System.in);
            int n;
            String a, b; // Mutiple variables in same type declaration in simple way
    
            System.out.print("How many sentence would you like to type? ");
            n = console.nextInt(); // Number of user inputs
            
            for(int i = 1; i <= n; i++) {
                System.out.println("Please Enter your " + i + " sentence: ");
                a = console.next(); // Read ith sengtence
                
                // Print results.
                System.out.println("Sentence is: " + a);
                
                if (i == 1) { // I'm not clear what you are trying to do here
                    System.out.print("Please enter your 1 Number: ");
                    b = console.next();
                    System.out.println("Number is: " + b);
                }
            }
        }
    }

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default

    wich is not right
    You need to explain WHY it is not right.
    What's wrong with it?

    Look at Eranga's code and see how it is commented. Comments help others understand what the code is supposed to be doing.
    Also see the use of "id strings" in the println()s:
    System.out.println("Number is: " + b);

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Again Norm, I think most of people doesn't read any materials before ask a question. I don't know how they going to learn new things in that way.

  13. #13
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    I don't know how they going to learn new things in that way.
    But reading and learning is hard. I think they expect enlightenment to fly in the window and make them smart.

    I wish it as well, but its never worked that way.

    I have to beat my head at new things for a while.

    I think we need a FAQ or sticky that explains that before posting, you should try engaging brain.

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Thanks for your thoughts fishtoprecords. Actually I really hate that. The way I learn is different. Read a lots of books and printed documents. Major reason to get that way is, in my child hood and the time I start to learn Java, actually programming, I don't have a computer, nor internet connection though. Simply I write codes in peace of paper and run it in my mind and see the result. Can you imagine how it is. :) That's my success.

  15. #15
    madatlis is offline Member
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default

    thanks guys for all your help, but I finally figured it out, to get it to do what I wanted I had to actually do a dummy line, so the nextInt would drop the \n < --- and the nextLine(), did not pick up the \n and not get some of my statement. Thanks again enjoyed reading all your comments

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    So that's means you didn't read the Java docs about those two. Why you need a dummy statement there? Do you have an answer for that?

  17. #17
    madatlis is offline Member
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default

    yes, the reason u needed a dummy statement is because when you are going from a .nextInt or just a .next inpute, it does not take the whole like for example, lets say u input, "Today is a great day" and the out put would be the same, but what is not showed is the \n < ------ the new line character, so it actaually reads, "Today is a great day \n" so basicly .next() or nextInt() will have the \n at the end. So when you do a nextLine(), the first thing it will read is the \n < ---- so if I type the same thing it will " is a great day" and skip Today, because of the \n line character. So basicly if you add a dummy satement wich can be done just by delcaring your scanner class once again. like lets say your class is called console, to do a dummy class you would just have to type console.nextLine();, so when you actually decide to input the real line, the dummy statement will take out the \n from the previous statement. so your statement will output right.

Similar Threads

  1. String/sentence to unicode convertion
    By sandeepvreddy in forum New To Java
    Replies: 5
    Last Post: 11-20-2008, 04:33 PM
  2. Has Java lost its way?
    By alexj44 in forum Advanced Java
    Replies: 4
    Last Post: 09-23-2008, 02:26 PM
  3. Replies: 1
    Last Post: 09-04-2008, 10:07 AM
  4. I am so lost in my java class
    By taliGLM in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 11:02 PM
  5. How to extract info from a sentence
    By luisarca in forum XML
    Replies: 1
    Last Post: 06-07-2007, 06:43 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
  •