• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

populating an array via the user

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an array nums[][], which I would like the user to enter 16 different integers into that array. Any help would be greatly appreciated.
Thanks,
Michael
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you developing an applet or an application? If it's an application, are you trying to create a GUI or is this a command line application?
 
Michael Taylor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to create a GUI.
 
Michael Taylor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is the code I have so far.

[ edit to replace [\code] tag with [/code] -ds ]
[ June 24, 2002: Message edited by: Dirk Schreckmann ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you imagine that you'd like the application to get the information from the user? Are you thinking of a TextField or a TextArea?
 
Michael Taylor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am really not sure. I have an assignment where the instructor wants the users to input integers and for my program to output the matrix and the sums of each diagonal. Secondly he wants the matrix to be populated by randomly generated numbers. I am lost and confused.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the application have to use Swing?
Have you already created a few Java applications? Perhaps one that says "Hello World!". Have you used a Frame or a JFrame yet?
 
Michael Taylor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have done a few applications, which included "Hello World". I am not familiar with Frame of JFrame.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the application have to use Swing components?
 
Michael Taylor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Swing components are not a requirement of the assignment.
 
Michael Taylor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Swing components are not a requirement of the assignment.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that creating a program with a GUI is quite a bit more involved than simply creating a command line based program. With the GUI, you'll have to (or get to) learn about Frames, and components, and layout managers, and events, and graphics. With a command line based program, this could just be a quick and simple input process, some quick math, and then a simple text output.
If you're sure that you'd like to create a GUI, let's begin to walk that path and just create a very simple program that says "hello" from a graphical user interface. In the end, I'm imagining a program with either one or multiple TextFields, and a Button or two, and an area to display results.
Probably the simplest way to accomplish this is to construct a plain Frame with the word "hello" displayed on it.
<SideNote>
In case you haven't discovered it yet, the documentation for the standard Java API is a very useful tool when figuring out what does what in Java. This documentation is located online at http://java.sun.com/j2se/1.4/docs/api/index.html and can also be downloaded from http://java.sun.com/j2se/1.4/download.html - the link for the documentation is towards the bottom of the page.
Another very useful set of documents from Sun, is Sun's Java Tutorial at http://java.sun.com/docs/books/tutorial/index.html .
</SideNote>
This displays a blank white frame:What we've done here is create a very basic Java program that can be invoked in this fashion:
java Whatever
after it has been successfully compiled. This program creates an instance of a Frame and displays it.
It's a rather annoying program, because the Frame cannot be directly closed using the 'X' in the upper right hand corner. You have to kill it through the console that created it, or, if that console is suppressed by some IDE, then perhaps you'd have to go so far as to press Ctrl+Alt+Delete and kill the appropriate Java process.
Here's a quick solution to allow the Frame to be closed:Now, the program has the ability to close the Frame. We can discuss the details of how this works later on (it involves event handling).
It would be arguably easier to manipulate the Frame, if the main program class itself were a Frame (by extending the Frame class) rather than just creating an instance of it in our program. Are you already mildly familiar with inheritance in Java?
Let's adjust our class to be a Frame and create and display an instance of itself:By extending the class Frame, our class inherits all of the Frame's functionality and then allows us to easily use, adjust, and add to that functionality. Right now, I'd like to override the paint method of the Frame in order to customize the display of our program. Are you familiar with the term override?
This is what I'm suggesting:When compiled and run, this program should now display a white Frame with the word "hello" displayed inside of it.
This paint method can be used for basic displaying of graphics (another subject that we could later explore and discuss).
If this would seem to be the path that you'd like to walk, then just say so and I'd be glad to help you figure these concepts out and you'd do well to take a look at the documentation for the various classes that I've used in these examples (remember the link above?). Otherwise, I would not be surprised if the instructor is expecting that you simply create a command line based program - that's what many instructors expect during the beginning of learning to program in Java.
What'll it be pardner?
 
Michael Taylor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that you hit the nail on the head. The instrustor, I'm sure, wants the command line based program. I appreciate all of your information as it was very informative, but I don't think that is what I need for this project. As I stated earlier, I am new to JAVA and the termonolgy associated with it. Forgive me for leading you down the wrong path. Can you help me in the command line program?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem. If I recall correctly, the initial question was about how to get input from the user.
You'd probably do well to be familiar with two different styles of text input. One way to get text input is as arguments to the main method when the Java application is initially invoked by the user. Remember that the main method takes an array of Strings?
Here's a simple program that displays any arguments passed to the main method:If this program is invoked like this:
java Whatever one two three
then it will output the following to the command line:
one
two
three
The basic concepts that this program uses to accomplish this task should be covered in the first few chapters of most any introductory book on Java programming. These concepts are also covered in Bradley Kjell's free on-line Java Tutorial Introduction to Computer Science using Java. Basic input and output coverage begins in chapter 10, for-loop coverage begins in chapter 41, array coverage begins in chapter 46, and when it comes time to change the input into numbers, it will be helpful to be familiar with the information in chapter 11.
Another way to get input from the user is through the standard input stream. Just as System.out is the standard output stream, System.in is the standard input stream. It's seemingly a little more complicated to use than the standard output stream. Read chapter 10 of the previously mentioned tutorial. Bradley Kjell does a decent job of describing how to use it and what is going on. He also provides an example program in the explanation.
Are things making sense yet? Any questions?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic