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 14 of 14

Thread: Buffered Reader to J Option

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Buffered Reader to J Option

    Hello guys, I wanted to convert my buffered reader to j option pane, can you help me convert this? Thanks.

    This must be in J option pane style output but i did it in buffered reader style.

    import java.util.Scanner;
    import java.util.Arrays;
     
    public class hello {
     
     public static void main(String[] args) {
     
      int largest;
      int smallest;
     
      int numOfInt = 0;
      int[] vars;
      int count = 1;
     
      Scanner input = new Scanner(System.in);
     
      System.out.println("\t\n Finding the largest & smallest number.");
      System.out.println("\t\n How many int values?");
      System.out.println("");
     
      numOfInt = input.nextInt();
      vars = new int[numOfInt];
     
      for (int a = 0; a < numOfInt; a++) {
     
       System.out.println("");
       System.out.println("Enter value for int #" + count + ":");
       input = new Scanner(System.in);
       vars[a] = input.nextInt();
       count++;
     
      }
     
      System.out.println("");
      System.out.println("All " + numOfInt + " int values entered.");
      System.out.println("");
     
      Arrays.sort(vars);
     
      smallest = vars[0];
      largest = vars[numOfInt - 1];
     
      System.out.println("SMALLEST int value is " + smallest);
      System.out.println("LARGEST int value is " + largest);
     }
    }

    Thank you.
    Last edited by helloworld922; July 18th, 2010 at 10:54 AM.


  2. #2
    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: Buffered Reader to J Option

    convert my buffered reader to j option pane
    Did you post the right code? I don't see a buffered reader. Also readers have nothing to do with output.

    If you want to know how to use the JOptionPane class, look in the Java Tutorial or Search on this forum for code examples or use Google.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Buffered Reader to J Option

    Ok, so JOptionPane has 4 main dialog boxes. Confirm Dialog (YES,NO,CANCEL), Input Dialog (Text Field and Buttons), Message Dialog (Label), Option Dialog (Label and Designer-Chosen buttons).

    We create these by using the associated JOptionPane method, which returns an appropriate type.

    So, to create and receive data from a Confirm Dialog:
    int result = JOptionPane.showConfirmDialog(null, "Message", "Title", JOptionPane.YES_NO_OPTION);

    To create an Input Dialog:
    String result = JOptionPane.showInputDialog(null, "Message", "Title", JOptionPane.PLAIN_MESSAGE);

    To create a Message Dialog:
    JOptionPane.showMessageDialog(null, "Message", "Title");

    To create an Option Dialog:
    JOptionPane.showOptionDialog(null, "Message", "Title", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, Object[], null);


    So, I'll get you started and you can try to finish. If you need help, just ask.
    import javax.swing.JOptionPane;
     
    public class hello 
    {
     
         public static void main(String[] args) 
         {
     
         int largest;
         int smallest;
     
         int numOfInt = 0;
         int[] vars;
         int count = 1;
     
         //We don't need this anymore
         //Scanner input = new Scanner(System.in);
     
         //This gets replaced with the line under it
         //System.out.println("\t\n Finding the largest & smallest number.");
         JOptionPane.showMessageDialog(null, "Finding the largest & smallest number.", "Finding..."); 
     
         //This gets replaced with the line under it
         //System.out.println("\t\n How many int values?");
         String amount = JOptionPane.showInputDialog(null, "How many int values?", "Values", JOptionPane.QUESTION_MESSAGE);
         //We dont need this anymore
         //System.out.println("");
     
         //This gets replace with the line under it
         //numOfInt = input.nextInt();
         numOfInt = Integer.parseInt(amount);
     
         vars = new int[numOfInt];
     
    for (int a = 0; a < numOfInt; a++) {
     
    System.out.println("");
    System.out.println("Enter value for int #" + count + ":");
    input = new Scanner(System.in);
    vars[a] = input.nextInt();
    count++;
     
    }
     
    System.out.println("");
    System.out.println("All " + numOfInt + " int values entered.");
    System.out.println("");
     
    Arrays.sort(vars);
     
    smallest = vars[0];
    largest = vars[numOfInt - 1];
     
    System.out.println("SMALLEST int value is " + smallest);
    System.out.println("LARGEST int value is " + largest);
    }
    }

    See if you can continue from therre.

  4. The Following User Says Thank You to aussiemcgr For This Useful Post:

    jk_0821 (July 18th, 2010)

  5. #4
    Junior Member
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Buffered Reader to J Option

    This is my edited code, it works but:

    import javax.swing.JOptionPane;

    public class hello
    {

    public static void main(String[] args)
    {

    int largest;
    int smallest;

    int numOfInt = 0;
    int[] vars;
    int count = 1;

    //We don't need this anymore
    //Scanner input = new Scanner(System.in);

    //This gets replaced with the line under it
    //System.out.println("\t\n How many int values?");
    String amount = JOptionPane.showInputDialog(null, "How many int values?", "Values", JOptionPane.QUESTION_MESSAGE);
    //We dont need this anymore
    //System.out.println("");

    //This gets replace with the line under it
    //numOfInt = input.nextInt();
    numOfInt = Integer.parseInt(amount);

    vars = new int[numOfInt];

    for (int a = 0; a < numOfInt; a++) {


    JOptionPane.showInputDialog("Enter value for int #" + count + ":");

    vars[a] = Integer.parseInt(amount);
    count++;

    }

    System.out.println("All " + numOfInt + " int values entered.");

    smallest = vars[0];
    largest = vars[numOfInt - 1];

    JOptionPane.showInputDialog("SMALLEST int value is " + smallest);
    JOptionPane.showInputDialog("LARGEST int value is " + largest);
    }
    }

    When i input 1 as first number, 2 as second and three as third,my output reads 3 as smaller number and 3 as larger that should be 1 as smaller and 3 as bigger, and i want it to loop as it ends and loop again as it says invalid input. (inputting other variables not equal to an integer).

    Thanks.
    Last edited by jk_0821; July 18th, 2010 at 08:54 AM.

  6. #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: Buffered Reader to J Option

    Your labels of smallest and largest are sort of misleading. There is no logic in your program to detect which is which.
    I think your labels should be "firstNumberEntered" and "lastNumberEntered". The first number went in the array at position 0 and the last number was at numOfInt-1;

    vars[a] = Integer.parseInt(amount);
    This puts the value of amount into the array. I only see where you set the value of amount one time.

    Your naming standards could be confusing you here. If you named the variable to hold the number of numbers by a name such as nbrOfNbrs instead of amount, perhaps you wouldn't be confused and reuse it incorrectly.
    Then have a new variable: nextUserNbr that you read from the user via the JOptionPane and save in the array.

  7. #6
    Junior Member
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Buffered Reader to J Option

    I edited it and it goes wrong again. What else should i do to make it loop as it ends and loop again as it says invalid input.

    import javax.swing.JOptionPane;

    public class hellow
    {

    public static void main(String[] args)
    {

    int firstNumberEntered;
    int lastNumberEntered;

    int numOfInt = 0;
    int[] jk;
    int count = 1;

    String nextUserNbr = JOptionPane.showInputDialog(null, "How many int values?", "Values", JOptionPane.QUESTION_MESSAGE);

    numOfInt = Integer.parseInt(nextUserNbr);

    jk = new int[numOfInt];

    for (int a = 0; a < numOfInt; a++) {


    JOptionPane.showInputDialog("Enter value for int #" + count + ":");

    jk[a] = Integer.parseInt(nextUserNbr);
    count++;

    }

    System.out.println("All " + numOfInt + " int values entered.");



    firstNumberEntered = jk[0];
    lastNumberEntered = jk[numOfInt - 1];

    JOptionPane.showInputDialog("SMALLEST int value is " + firstNumberEntered);
    JOptionPane.showInputDialog("LARGEST int value is " + lastNumberEntered);
    }
    }

  8. #7
    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: Buffered Reader to J Option

    it goes wrong again
    Please explain.

  9. #8
    Junior Member
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Buffered Reader to J Option

    This should display the minimum and the maximum integer i entered.
    But I guess this only reads the length of my input.

    For example, I inputted 3 values, the output goes like it only reads the number of values instead of the lowest and the highest value of the integers I entered.

    How many int values?
    3

    Enter value for (int #) = 1st
    1

    Enter value for (int #) = 2nd
    2

    Enter value for (int #) = 3rd
    3

    SMALLEST int value is:
    3
    LARGEST int value is:
    3

    That should be..

    SMALLEST int value is:
    1
    LARGEST int value is:
    3

    Sorry for this one guys.

  10. #9
    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: Buffered Reader to J Option

    Look at the variable: nextUserNbr
    Where is it given a value?
    What value is it given?

    To make a better test, use numbers for input like 22, 33 and 44 instead of 1, 2, 3 and see what the program does.

  11. #10
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Buffered Reader to J Option

    Found the problem. We want to send it a value, where you have been sending it the count.

    import javax.swing.JOptionPane;
     
    public class hellow
    {
     
    public static void main(String[] args) 
    {
     
    int firstNumberEntered;
    int lastNumberEntered;
     
    int numOfInt = 0;
    int[] jk;
    int count = 1;
     
    String nextUserNbr = JOptionPane.showInputDialog(null, "How many int values?", "Values", JOptionPane.QUESTION_MESSAGE);
     
    numOfInt = Integer.parseInt(nextUserNbr);
     
    jk = new int[numOfInt];
     
    for (int a = 0; a < numOfInt; a++) {
     
    //JOptionPane.showInputDialog("Enter value for int #" + count + ":");
    [B]String value = JOptionPane.showInputDialog("Enter value for int #" + count + ":");[/B]
    //jk[a] = Integer.parseInt(numOfInt );
    [B]jk[a] = Integer.parseInt(value);[/B]
    count++;
     
    }
     
    System.out.println("All " + numOfInt + " int values entered.");
     
     
     
    firstNumberEntered = jk[0];
    lastNumberEntered = jk[numOfInt - 1];
     
    JOptionPane.showInputDialog("SMALLEST int value is " + firstNumberEntered);
    JOptionPane.showInputDialog("LARGEST int value is " + lastNumberEntered);
    }
    }
    See, you were sending it what we received from our first inputDialog, but we wanted to collect the values from the next inputDialogs and send those to the array.

    Norm is correct, if you chose larger values, like 11,22,33, you would have gotten 3 as your answers. If you gave it values like 11,22,33,44, you would have gotten 4 as your answers. Try other test cases when you hit a wall, you can usually find what you overlooked when the test cases are varied the most.

  12. #11
    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: Buffered Reader to J Option

    Found the problem
    The idea is to show the OP how to find the problem. He'll need those skills to write programs.
    Last edited by Norm; July 19th, 2010 at 12:58 PM.

  13. #12
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Buffered Reader to J Option

    Quote Originally Posted by Norm View Post
    The idea is to show the OP how to find the problem. He'll need those skills to write programs.
    Figured he wasnt going to find it since he overlooked the creating a new variable thing that he doesnt really have to worry about in Scanner. If he was doing something that I felt he had a grasp of, I would have let it go, but since I remember making that same mistake over and over and over again when I first started with JOptionPane, I figured I'd explain to him what was happening. I felt the problem wasnt that he wasnt collecting the new String, but rather that he was assuming the new String was already being collected when he prompted the user. I made that mistake SO many times when I first started it.

  14. #13
    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: Buffered Reader to J Option

    You've got a big advantage over me there. I learned programming a while back and have forgotten some of the problems.
    I do see lots of OPs not being able to figure out what their code is doing. They don't know how to play computer / desk check their programs. More time is wasted writing and rewriting code without any design than it would take if they took a piece of paper and pencil and worked their way thru their code.

  15. #14
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Buffered Reader to J Option

    Ya, Java has been my first language and I started learning it in High School 2 years ago and I have yet to start college. So naturally, I still run into countless issues on a daily basis. The only difference is that I learned how to spot where potential issues may occur (NullPointers, infiinate loops, ect.) and now whenever I run into an issue I spam my code with System.out.println statements based on my problem and where the problem could be occuring.

    Admittingly, I do far less paper and pencil designing than I should because I usually have a clear picture of what I'm trying to do and how to do it before I start coding. However, there are plenty of times when I say, ok, stop and write down exactly what needs to be done, plan out the classes/methods, plan out how it will be done, plan out the most vital variables, and find the points of failure. But that is only on the most complex things that when i try to wrap my head around it, I can feel my head exploding (like my Sudoku solving program I made a year ago).

  16. The Following User Says Thank You to aussiemcgr For This Useful Post:

    jk_0821 (July 19th, 2010)

Similar Threads

  1. Is thread the best option?
    By 256mxr in forum Threads
    Replies: 1
    Last Post: May 22nd, 2010, 08:24 PM
  2. Problems with File Reader (Strings and 2D Array Storage)
    By K0209 in forum File I/O & Other I/O Streams
    Replies: 44
    Last Post: January 12th, 2010, 10:48 AM
  3. Buffered Reader is not reading my file properly... HELP!
    By mannyT in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: November 8th, 2009, 08:14 PM
  4. greetings and a file reader problem
    By chileshe in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: October 6th, 2009, 03:45 AM
  5. Code to read a character in the file
    By Truffy in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 19th, 2009, 06:11 PM