Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Asking Console

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Asking Console

    Hi all, I hope someone can help me because I'm gonna get crazy with this problem...


    This is the idea of what the program must do: reading a txt file where every line there is a String and ask the user if it's a person name or no.

    So the problem is:

    I have a class that read a txt file; then every text line the program ask the user (via eclipse console) to say if it's a person name or not (y/n).

    For this I have simply done : while ((str = text.readLine()) != null) and then I ask the user with the normal scanner(system.in) to write y or n and then I check the scanner input : if y I save str in an ArrayList...if not the while loop and a new str is created and a new question is done...

    This is the idea for the console program and it work...now I want to make this work out of eclipse...using a simple GUI console

    Now I have made a new class called AskConsole() that create a simple Window with a JtextArea and a JtextFiled to use as a console... in the textArea the program must print the line of text that is currently read;
    then the program mus stop, wait for the user to write in the text field y or n (then enter to say ok I've written the answer) and now the program can continue the while loop as before...

    I hope that the problem is clear enough...if not ask me more details...

    thanks all


  2. #2
    Junior Member
    Join Date
    Sep 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Asking Console

    Hi all...I post here my solution...after I few try I got the answer...

     public class Program {
    public static void main(String[] args) {
     
    	ReadTxt reader = new ReadTxt();
     
    }
    }
     
    class ReadTxt{
    	private Scanner scan= new Scanner(System.in);
     
    	public ReadTxt(){
    		Thread t= new Thread("askT");
     
    		GuiConsole guiCon= new GuiConsole(t);
    		char response=' ';
    		File f = new File("C:/name.txt");
    		try {
    			FileInputStream fis = new FileInputStream(f);
    			BufferedReader buff= new BufferedReader(new InputStreamReader(fis));
    			String str="";
    			while((str=buff.readLine())!=null){
    				guiCon.printText(str + " : is a name?  (y / n)");
     
    //				response= scan.next().charAt(0);
    				synchronized (t) {
    					try {
    						t.wait();
    					} catch (InterruptedException e) {
    						throw new RuntimeException(e);
    					}
    				}
    				response=guiCon.getResp().charAt(0);
     
    				switch(response){
    				case'y': guiCon.printText("Name !");
    				break;
    				default: guiCon.printText("no...");
    				}
     
    			}
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
     
    	}
    }
     
     
     
    public class GuiConsole {
     
    	private JFrame window;
    	private JScrollPane scroll;
    	private JTextArea console;
    	private JTextField answerField;
    	private JPanel textPanel;
    	private String resp = "";
     
    	private Thread wT = null;
     
    	public GuiConsole(Thread waitingT) {
    		window = new JFrame("GUI CONSOLE");
    		wT=waitingT;
    		try {
    			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    		} catch (Exception e) {
    			System.out.println("Error setting native LookAndFeel: ");
    		}
     
    		console = new JTextArea("Started:\n**********");
    		console.setLineWrap(true);
    		scroll = new JScrollPane(console);
    		scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    		scroll.setWheelScrollingEnabled(true);
    		answerField = new JTextField();
    		textPanel = new JPanel();
     
    		GridBagLayout layout = new GridBagLayout();
    		GridBagConstraints lim = new GridBagConstraints();
    		textPanel.setLayout(layout);
    		lim.insets.top = 10;
    		lim.insets.bottom = 5;
    		lim.insets.left = 10;
    		lim.insets.right = 10;
    		lim.fill = GridBagConstraints.BOTH;
     
    		lim.gridx = 0;
    		lim.gridy = 0;
    		lim.weightx = 1;
    		lim.weighty = 1;
    		layout.setConstraints(scroll, lim);
    		textPanel.add(scroll);
    		lim.insets.bottom = 30;
    		lim.gridx = 0;
    		lim.gridy = 1;
    		lim.weightx = 1;
    		lim.weighty = 0;
    		lim.fill = GridBagConstraints.HORIZONTAL;
    		layout.setConstraints(answerField, lim);
    		textPanel.add(answerField);
     
    		answerField.addKeyListener(keyLis);
     
    		window.add(textPanel);
     
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		window.setSize(new Dimension(500, 500));
    		window.setVisible(true);
     
    	}
     
    	private KeyListener keyLis = new KeyListener() {
     
    		@Override
    		public void keyTyped(KeyEvent t) {
     
    		}
     
    		@Override
    		public void keyReleased(KeyEvent r) {
     
    		}
     
    		@Override
    		public void keyPressed(KeyEvent p) {
    			if (p.getKeyCode() == KeyEvent.VK_ENTER) {
    				console.append("\n" + answerField.getText());
    				resp = answerField.getText();
    				answerField.setText("");
    				synchronized (wT) {
    					wT.notify();
     
    				}
    			}
    		};
     
    	};
     
    	public void printText(String m) {
    		console.append("\n" + m);
    	}
     
    	public String getResp() {
    		return resp;
    	}
    }

    By all...
    I hope this post can help someone else...
    Last edited by righi; June 20th, 2012 at 08:09 AM.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Asking Console

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Sep 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Asking Console

    Sorry.... Now is better....

    by...

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Asking Console

    Thanks that's better.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Asking Console

    If you aren't using it as a homework assignment, I have made a class that is very similar to this that acts similarly to console except more user-friendly, for this very purpose. Feel free to check it out at Windows 7 Console

Similar Threads

  1. get console to work
    By clankill3r in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 22nd, 2011, 07:31 PM
  2. A simple BOX in Console! Please Help!
    By rainexel in forum Java Theory & Questions
    Replies: 9
    Last Post: September 26th, 2011, 11:37 AM
  3. From Console to GUI : problem !!
    By hexwind in forum AWT / Java Swing
    Replies: 33
    Last Post: August 20th, 2011, 10:50 PM
  4. Console box with scanner?
    By Hallowed in forum Java Theory & Questions
    Replies: 1
    Last Post: May 26th, 2011, 12:50 AM
  5. Console Application - How to run it?
    By mirzahat in forum AWT / Java Swing
    Replies: 3
    Last Post: November 16th, 2010, 12:21 AM