Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-12-2012, 09:21 PM   PM User | #1
AllNewMachine
New to the CF scene

 
Join Date: Sep 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
AllNewMachine is an unknown quantity at this point
New Coder... Help my Code

I already have to programs that I have made. Very simple some of the first things i've ever written. Now I am trying to combine the two.

The first Code is.............

import java.util.Scanner;

public class FirstProject {

public static void main(String[] args) {

System.out.println("Hello out there.");
System.out.println("I will add two numbers for you.");
System.out.println("Enter two whole numbers on a line");

int n1, n2;
Scanner keyboard = new Scanner(System.in);
n1 = keyboard.nextInt();
n2 = keyboard.nextInt();

System.out.println("The sum of those two numbers is");
System.out.println(n1 + n2);

}

}
.................... I taught myself about simple input and output.

The second code.....................


import javax.swing.JApplet;
import java.awt.Graphics;

public class HappyFace extends JApplet
{

public void init() {
setSize(1000,1000);
}

public void paint (Graphics canvas)
{
// Draw outline
canvas.drawOval (100, 50, 200, 200);

// Draw eyes
canvas.fillOval (155, 100, 10, 20);
canvas.fillOval (230, 100, 10, 20);

// Draw mouth
canvas.drawArc (150, 160, 100, 50, 180, 180);
}
}

.................... is the first time I used a Java applet



Now Im trying to combine the two Programs to greet you with the promt. Then give your imput presents you with a choice of two applets. Im starting to get overwhelmed and I need help. I dont know how to write this thing.

So far I have.........

import javax.swing.JApplet;
import java.awt.Graphics;
import java.util.Scanner;

public class HappyWild extends JApplet {
public void init() {
setSize(500, 500);
}
public void paint(Graphics canvas) {
System.out.println("Are you feeling Happy or Wild?");
...............

I dont know how to read an answer that is a string not an interger.

String answer = keyboard.......
System.out.println(answer);

I dont understand about branching and I need help writing my code from here on. Thank You for your help guys
AllNewMachine is offline   Reply With Quote
Old 09-12-2012, 09:36 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
This question needs to be moved from JavaScript to Java
devnull69 is offline   Reply With Quote
Old 09-12-2012, 09:38 PM   PM User | #3
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 960
Thanks: 7
Thanked 100 Times in 100 Posts
WolfShade is an unknown quantity at this point
Please read #1 of the following: http://www.codingforums.com/showthread.php?t=2090
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 09-12-2012, 09:39 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This isn't javascript. Moving from Javascript to Java forum.
These are somewhat incompatible. Scanner is designed to tokenize and parse input; when you specify that it comes from System.in, that allows easy interaction with the keyboard.
An applet is a gui component, in this case a swing component. In order to create input you typically use input fields such as JTextField, or prompts using JOptionDialog classes.
If you use a JTextField, you have to add an event listener to it. FocusListener tends to be a good one when the field looses focus. This then is called and invokes the code specified by the event handler and does whatever it needs to do with the input. In the case of a number, a try/catch is used to cast an integer using the Integer.parseInt() method since all input from a text area .value() is a string. Same check needs to go on a JOptionDialog (or another JDialog type), but you don't need an event listener since a thread is used on the dialog and halts the processing of the program until it receives a response, similar to the nextLine() method of the scanner.

Needless to say, you cannot just go from a cli application to a gui. You need to learn the processes first, then write the gui. Oracle's documentation tutorials are pretty good: http://docs.oracle.com/javase/tutori...ing/index.html

Also, in the future please wrap your code in [php][/php] or [code][/code] tags.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:00 PM.


Advertisement
Log in to turn off these ads.