Results 1 to 17 of 17
  1. #1
    ducreative is offline Member
    Join Date
    Mar 2009
    Posts
    9
    Rep Power
    0

    Default Input technique for unknown lines of input

    I have a problem with input techniques from user. Suppose user can feed up program 3 lines as:
    Java Code:
    Department of Computer science
    and 
    engineering University of Dhaka
    But our programmer does not know how many lines are there and we have to process one line and then to next line. After reading all the lines i have to display all inputs accordingly. Output should be
    Java Code:
    Department of Computer science
    and 
    engineering University of Dhaka
    Here is my program
    Java Code:
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.Scanner;
    
    public class InputTechnique {
        public static void main(String args[]){
             Scanner scan = new Scanner(System.in);
             ArrayList<String> aList = new ArrayList<String>();
             while(scan.hasNextLine()){
                 aList.add(scan.nextLine());
             }
             Iterator it = aList.iterator();
             while(it.hasNext())
                 System.out.println(it.next());
        }
    }
    My program hangs on first while loop. How can solve this problem?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    What do you mean by "it hangs"?
    It'll accept your input, so it's not hanging...you just haven't given it an exit.

  4. #4
    ducreative is offline Member
    Join Date
    Mar 2009
    Posts
    9
    Rep Power
    0

    Default

    i mean it cannot break the first while loop and continue taking inputs from user.
    User can feed blank lines as
    Java Code:
    hello
    i am new to
    
    this forum
    Output should be
    Java Code:
    hello
    i am new to
    
    this forum
    Thanks.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    So, as Fubarable has asked, and (I think) was pointed out in the thread you tried to hijack, how does the user exit?

  6. #6
    ducreative is offline Member
    Join Date
    Mar 2009
    Posts
    9
    Rep Power
    0

    Default

    yes, my question is how does the program terminate? Is there any process to solve this. I need this requirement for solving a UVA online judge problem, and if i am failed to make you understand what i am trying to say, just visit uva online judge and the problem ID is 10815[Andy's First Dictionary].

    Thanks

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    OK...step back then...how do you want the user to say they've entered enough?

    In the real world I have never written a command line user input program...at least not in the past 15+ years. So, how do you want them to inform the program they have finished?

    Fubarable has given an option in his post, and there were one or two on the other thread.

  8. #8
    ducreative is offline Member
    Join Date
    Mar 2009
    Posts
    9
    Rep Power
    0

    Default

    How can i find the End Of Files? Would you like to mention the technique.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    What end of file?
    It's user input.
    You have to decide what the user needs to enter to say they've finished.

    There is no "technique"...what does the user have to do to say they've finished entering data?

  10. #10
    ducreative is offline Member
    Join Date
    Mar 2009
    Posts
    9
    Rep Power
    0

    Default

    Ok, Thanks.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Right, no problem.
    Do you now understand what you have to do to get this to work?

  12. #12
    ducreative is offline Member
    Join Date
    Mar 2009
    Posts
    9
    Rep Power
    0

    Default

    Yes, thanks a lot Tolls. I have to take the input from files rather than console.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    At least for me, your creeping requirements have confused me greatly and continue to confuse me. In your first post, you mention nothing about files and fact state this:

    I have a problem with input techniques from user. Suppose user can feed up program 3 lines as:
    and in your code you have
    Java Code:
    Scanner scan = new Scanner(System.in);
    With a Scanner that is constructed with System.in, you show that the program will take input from the console.

    So I have to ask, what the heck is going on? What are your real requirements here and why are you changing things?

    If you confuse your questions you will get nothing but confusing answers.
    Last edited by Fubarable; 09-22-2009 at 04:42 AM.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote Originally Posted by ducreative View Post
    Yes, thanks a lot Tolls. I have to take the input from files rather than console.
    As Fubarable says, that wasn't your requirement at the start. If you have a requirement to take input from the user then that's what you should be writing.

  15. #15
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    Hi,
    U just take the input
    "y" or "n" from the user .If they say "n" just come out.

    -Regards
    Ramya
    Ramya:cool:

  16. #16
    ducreative is offline Member
    Join Date
    Mar 2009
    Posts
    9
    Rep Power
    0

    Default

    Okay, i am explaining now. The first thing is that i am new in java. Actually i like C++. Look at this code bellow
    Java Code:
    #include<iostream>
    #include<cstring>
    using namespace std;
    
    struct x{
    	char array[100];
    }sample[100];
    int main(){
    	freopen("input.txt", "r", stdin);
    	char input[101];
    	int indexx = -1;
    	while(gets(input)){
    		strcpy(sample[++indexx].array, input);
    	}
    	
    	for(int i = 0; i <= indexx; i++)
    		cout << sample[i].array;
    	return 0;
    }
    In the above program all inputs are taken from the file. But if we close the freopen function as
    Java Code:
    //freopen("input.txt", "r", stdin);
    all the inputs are taken from console and in msdev when i use ctrl + z that means end of file and we ready to jump for loop and execute output. I want to know the same things i posted before in my thread.

    i wanted to write this code in java as
    Java Code:
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.Scanner;
    
    public class InputTechnique {
        public static void main(String args[]){
             Scanner scan = new Scanner(System.in);
             ArrayList<String> aList = new ArrayList<String>();
             while(scan.hasNextLine()){
                 aList.add(scan.nextLine());
             }
             Iterator it = aList.iterator();
             while(it.hasNext())
                 System.out.println(it.next());
        }
    }
    I want to replace this C++ code to java. In solving problem UVA online judge the "fropen()" function should be closed before submitting the solution.

    Now i think, you understand my words what i am trying to say. Now give me a solution.

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    And this is the problem.
    Java is not C++.
    You cannot simply take code from one language and expect to turn it into another one without actually learning the language itself.

    I am, however, not going to give you the solution. There is no "freopen" function in Java.

    Scanner is not freopen.
    You need to learn how Scanner works...especially with System.in.
    As has been said several times on this thread, you need to decide what value the user has to enter (and it will involve <enter> since that's how nextLine works) to indicate they have finished.

Similar Threads

  1. How to get input from Console
    By karma in forum New To Java
    Replies: 8
    Last Post: 08-13-2010, 09:32 PM
  2. Array Input
    By Rose88 in forum New To Java
    Replies: 2
    Last Post: 04-19-2009, 10:39 PM
  3. Count lines cointaining "word" in input file
    By gwithey in forum New To Java
    Replies: 5
    Last Post: 04-02-2009, 05:23 AM
  4. cant take input from user
    By new_1 in forum New To Java
    Replies: 6
    Last Post: 12-25-2007, 07:38 AM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 AM

Posting Permissions

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