Results 1 to 17 of 17
- 09-21-2009, 03:32 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 9
- Rep Power
- 0
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:
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 beJava Code:Department of Computer science and engineering University of Dhaka
Here is my programJava Code:Department of Computer science and engineering University of Dhaka
My program hangs on first while loop. How can solve this problem?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()); } }
-
How do you know when the user is done inputting text? Do they send a blank line?
- 09-21-2009, 03:39 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 09-21-2009, 03:52 PM #4
Member
- Join Date
- Mar 2009
- Posts
- 9
- Rep Power
- 0
i mean it cannot break the first while loop and continue taking inputs from user.
User can feed blank lines as
Output should beJava Code:hello i am new to this forum
Thanks.Java Code:hello i am new to this forum
- 09-21-2009, 03:56 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
So, as Fubarable has asked, and (I think) was pointed out in the thread you tried to hijack, how does the user exit?
- 09-21-2009, 04:01 PM #6
Member
- Join Date
- Mar 2009
- Posts
- 9
- Rep Power
- 0
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
- 09-21-2009, 04:09 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 09-21-2009, 04:33 PM #8
Member
- Join Date
- Mar 2009
- Posts
- 9
- Rep Power
- 0
How can i find the End Of Files? Would you like to mention the technique.
- 09-21-2009, 04:42 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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?
- 09-21-2009, 04:48 PM #10
Member
- Join Date
- Mar 2009
- Posts
- 9
- Rep Power
- 0
Ok, Thanks.
- 09-21-2009, 04:52 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Right, no problem.
Do you now understand what you have to do to get this to work?
- 09-22-2009, 04:12 AM #12
Member
- Join Date
- Mar 2009
- Posts
- 9
- Rep Power
- 0
Yes, thanks a lot Tolls. I have to take the input from files rather than console.
-
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:
and in your code you haveI have a problem with input techniques from user. Suppose user can feed up program 3 lines as:
With a Scanner that is constructed with System.in, you show that the program will take input from the console.Java Code:Scanner scan = new Scanner(System.in);
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.
- 09-22-2009, 09:13 AM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
- 09-22-2009, 10:34 AM #15
Hi,
U just take the input
"y" or "n" from the user .If they say "n" just come out.
-Regards
RamyaRamya:cool:
- 09-22-2009, 06:20 PM #16
Member
- Join Date
- Mar 2009
- Posts
- 9
- Rep Power
- 0
Okay, i am explaining now. The first thing is that i am new in java. Actually i like C++. Look at this code bellow
In the above program all inputs are taken from the file. But if we close the freopen function asJava 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; }
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.Java Code://freopen("input.txt", "r", stdin);
i wanted to write this code in java as
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.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()); } }
Now i think, you understand my words what i am trying to say. Now give me a solution.
- 09-23-2009, 09:26 AM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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
-
How to get input from Console
By karma in forum New To JavaReplies: 8Last Post: 08-13-2010, 09:32 PM -
Array Input
By Rose88 in forum New To JavaReplies: 2Last Post: 04-19-2009, 10:39 PM -
Count lines cointaining "word" in input file
By gwithey in forum New To JavaReplies: 5Last Post: 04-02-2009, 05:23 AM -
cant take input from user
By new_1 in forum New To JavaReplies: 6Last Post: 12-25-2007, 07:38 AM -
how to take input and verify input in Java programs
By bilal_ali_java in forum Advanced JavaReplies: 0Last Post: 07-21-2007, 08:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks