problems with my first job programming in Java [scanner keyboard] [scanner system]

Q: We have only just learned the basics, so I want it as simple as possible

Here is the code I wrote so far: 977 503 977 503 “public class Degrees
(977 503 public static void main (String [] args) 977 503 (977 503 int DegreesC , DegreesF,

= new Scanner (System.in); 977 503 System.out.println (“Enter temperature in degrees Fahrenheit:”); 977 503 DegreesF keyboard.nextInt = (); 977 503 977 503 DegreesC = 5 * (DegreesF – 32) / 9; 977 503 977 503 System.out.println (DegreesF + “degrees Fahrenheit =”); 977 503 System.out.println (DegreesC + 977 503 977 503 977 503 977 503 .”);) degrees Celsius)
Im getting errors when I try to compile

thanks


Best Answer: Swing works on the concept of "containers". You set the Layout on a container. JFrame is already default as BorderLayout. You don't have to set it, but it does not hurt to setLayout(). JPanel is default FlowLayout. So, in the grand scheme of things you add a JPanel to the center of BorderLayout, setLayout on the Panel to what you want, add other panels, set those layouts and add more and more widgets.

Your first problem, you violated a name. BorderLayout is a Java API class. I did the class name as TBorderLayout.

import java.awt.*;
import javax.swing.*;

class TBorderLayout
{
public static void main(String args[])
{

JFrame f = new JFrame("Border Layout");

JPanel p = new JPanel();
f.add( p, BorderLayout.CENTER);

// add this, to stop the program with [x] window btn
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

// JFrame is a top-level, you can size with numbers
f.setSize(200,200);
//p.setSize(200,200); this won't work, Swing sizes by ratios, not nums
p.setLayout(new BorderLayout(20,5));
p.add(new JButton("Down"),BorderLayout.SOUTH);
p.add(new JButton("Up"),BorderLayout.NORTH);
p.add(new JButton("CENTER"),BorderLayout.CENTER);
// f.setSize (100,100); you already have set size
f.setVisible(true);
}
}

don't use .setContent(). That is for something different.
Get familiar about looking through the Java docs for API. Lookup JFrame and look at all its methods


Re:For formatting output, doesn't Java have printf(or similar functionality) now? I think it's part of java 1.5

Re:Also, when you use System.out.println(), it puts a newline at the end of the string. If you instead used System.out.print() for the first statement, and then System.out.println() for the second, you'd end up with the formatting you're looking for (in terms of lines). An alternative would be to put both into one println() statement – System.out.println(DegreesF + "degrees Fahrenheit =" + DegreesC + "degrees Celsius");

Re:use double and not int for your types.

Re:Originally posted by: guy
Because your not including the Scanner package into your class.

import java.util.Scanner;

I assume?

thanks that fixes the compiling problem

However, I'm wondering how I can get this output?

Enter a temperature in degrees Fahrenheit: 72
72 degrees Fahrenheit = 22.2 degrees Celsius.

Mine looks like this:
Enter a temperature in degrees Fahrenheit:
72
72 degrees Fahrenheit =
22 degrees Celsius.

How do I get 22.2 with the decimal point?
and why is my format so off? I don't know how to get the 72 degrees F = 22 degrees C on the same line


Re:Because your not including the Scanner package into your class.

import java.util.Scanner;

I assume?


Re:it says cannot find symbol
symbol: class Scanner
location class Degrees
= new Scanner <System.in>;

2 errors


Related posts

Leave a comment

0 Comments.

Leave a Reply


click to changeSecurity Code

[ Ctrl + Enter ]