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.
Code Java:
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.
Re: Buffered Reader to J Option
Quote:
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.
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.
Code :
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.
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.
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;
Quote:
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.
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.
Quote:
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);
}
}
Re: Buffered Reader to J Option
Quote:
it goes wrong again
Please explain.
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.
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.
Re: Buffered Reader to J Option
Found the problem. We want to send it a value, where you have been sending it the count.
Code :
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.
Re: Buffered Reader to J Option
The idea is to show the OP how to find the problem. He'll need those skills to write programs.
Re: Buffered Reader to J Option
Quote:
Originally Posted by
Norm
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.
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.
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).