Results 1 to 9 of 9
  1. #1
    chrisLU5 is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default Problem with JOptionPane

    Hiya ive got a question in this code below is there any way i can get the part of the code in bold to show in a joptionpane window, or can it only print to the console??

    Java Code:
    // demonstrates reading an unout file
    import Barnfield.Dialogs;
    import java.io.*;
    
    
    
    class ShowFile{
    	public static void main(String args[]) throws IOException{
    		
    		int i;
    		
    		FileInputStream fIn;
    		String fileName = "C:\\users\\chris\\desktop\\java\\week 12 - IO\\text.txt";
    		try{
    			fIn = new FileInputStream(fileName); // opens the file for input
    		}
    		
    		catch (FileNotFoundException exc){
    			System.out.print("File not found");
    			return; //exit from call to main
    		}
    		
    		// read bytes untill EOF is enountered
    		do{
    			i = fIn.read(); // reads a single byte from the file and returns it as an int value
    			if (i != -1){
    				[B]System.out.print((char) i);[/B]
    			}
    		} while (i != -1); // -1 signifies End of file
    		System.out.println("");
    		
    		fIn.close(); // close file
    		
    	}
    	
    	}
    So instead of
    Java Code:
    System.out.print((char) i);
    i want
    Java Code:
    Dialogs.outputMessage((char) i);
    This is the dialog i am using:

    Java Code:
    	
    static public void outputMessage(String message){
    		JOptionPane.showMessageDialog(null, 
    			message,  
    			"Output",
    			JOptionPane.PLAIN_MESSAGE);
    	}

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    you answered the question yourself...

  3. #3
    chrisLU5 is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    C:\Users\Chris\Desktop\Java\Week 12 - IO\ShowFile.java:27: cannot find symbol
    symbol : method outputMessage(char)
    location: class Barnfield.Dialogs
    Dialogs.outputMessage((char) i);
    ^
    1 error
    Process javac exited with code 1

    But i get this error message?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    A char is not a String

    db

  5. #5
    chrisLU5 is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    So is there any way for me to be able to display the characters in the text file into a messagebox??

  6. #6
    milovan is online now Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    299
    Rep Power
    4

    Default

    Read all answers once again, you are so near:

    Quote Originally Posted by ozzyman View Post
    you answered the question yourself...

    C:\Users\Chris\Desktop\Java\Week 12 - IO\ShowFile.java:27: cannot find symbol
    symbol : method outputMessage(char)

    Quote Originally Posted by Darryl.Burke View Post
    A char is not a String

    db

    Java Code:
    static public void outputMessage([B]String[/B] message){

    Can you see what you have to do?

  7. #7
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Java Code:
    JOptionPane.showMessageDialog(null, i.toString());
    Last edited by Solarsonic; 05-05-2011 at 10:04 PM.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Quote Originally Posted by Solarsonic View Post
    [Code]JOptionPane.showMessageDialog(null, i.toString());[code/]
    You can't dereference a primitive. Did you read through the thread before responding?

    db

  9. #9
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by DarrylBurke View Post
    You can't dereference a primitive. Did you read through the thread before responding?

    db
    Oh, my bad.
    Last edited by Solarsonic; 05-05-2011 at 10:07 PM.

Similar Threads

  1. update contentpane and jOptionPane problem
    By Jhovarie in forum Threads and Synchronization
    Replies: 4
    Last Post: 03-02-2011, 03:59 AM
  2. Problem JOptionPane try catch
    By jason99 in forum New To Java
    Replies: 1
    Last Post: 07-10-2009, 10:39 PM
  3. [SOLVED] JOptionPane.ShowConfirmDialog() problem
    By jon80 in forum New To Java
    Replies: 15
    Last Post: 06-03-2009, 02:37 AM
  4. JOptionPane problem
    By MomenT in forum New To Java
    Replies: 22
    Last Post: 10-02-2008, 05:56 PM
  5. Problem with JOptionPane.showInputDialog()
    By romina in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 05:33 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
  •