Write a program that will display all the odd number between 1 and 15. here is the code that i use :
Scanner scan = new Scanner (System.in);
int i, num=0, count_odd = 0;
for(i=1; i<=15; i++) {
System.out.println("please enter number ");
num = scan.nextInt();
The code below is just the GUI for a slot machine. Currently when play is clicked it displays the random number, I'm trying to make it so each random number displays an image in the GUI instead. For example if the random numbers were 2,2,3 it would display Cherry, Cherry, Bells. I started changing field1 to the image but realised I was on the wrong track as it would just display the same image not one allocated to a number. I've left in what I started doing.
how to do it?
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SlotMachineGUI { JPanel mainPanel; SlotMachine slotEngine;
tfrreee i want to display given series in ascending order in java prog here is an error : E:Javajdkin>java ascend 505671Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at ascend.main(ascend.java:9)
Java Code:
class ascend { public static void main (String args[]) { int s[]={ 50,71,81,21,7}; int i; for(i=0;i<=s.length;i++)
I'm trying to output a ten digit phone number as a string on one file, and write another program to write it as a long because I don't know what value I need to use in order to get the code to run properly on either one.
I've chosen to build a GUI that will initially display a window with 'x' number of buttons for different subject areas of physics (kinematics, waves, sound, circuits...etc). I would like to use an Actionlistener for each button so that when its pressed, the JFrame will extend a sidepanel or expand the JFrame with a new JPanel that contains variable Jtextfields so the user can enter data to calculate.
I'd like to know if its possible to do this with the JFrame or should I just open a new window altogether? I have some other features I plan to add to the program, but this is where I am right now.
how difficult is it to add a banner image and imageIcon? By banner image I mean create a Jpanel that will contain a header for the window and will have some cool physics jpg image.
I am working with a program I wrote for class. I got it to compile and do what I want, But I was wondering how I can format my methods to to display a decimal with 1-2 decimal places. Would I create a method in my NumberAnalysis class to do it for me? Or would I declare an instance of the DecimalFormat class in my main method?
import java.util.Scanner; //Needed for Scanner Class import java.io.*; //Need for File and IOException import java.text.DecimalFormat; public class Ex8_11 { public static void main(String[] args) throws IOException{ { DecimalFormat decformatter = new DecimalFormat("#0.00");
[Code] .....
This is my output:
Lowest Number: 1.09 Highest Number: 82.76 Total Number: 367.89000000000004 Total Average Number: 30.657500000000002
Think I just solved the answer to my own question, I did it by declaring double variables in my main method and called the methods and instantiated the variables into the methods... Is there a better way to do this?
package lesson4.skowronek; import java.util.Scanner; //Needed for Scanner Class import java.io.*; //Need for File and IOException import java.text.DecimalFormat; public class Ex8_11 { public static void main(String[] args) throws IOException{
I have returned with yet another problem but I think this one might be a bit easier to fix.
So the whole MO of this program is to take user input, and display the sum of the digits of the number they entered. I am supposed to do this by utilizing methods. So far this is what I have and when I compile it tells me that it "cannot find symbol", which I don't understand as I define what "m" is in the for loop. The other error is that it says:
"Exercise6_2.java:22: error: incompatible types: possible lossy conversion from long to int return result; ^
I don't understand why it's giving me this error nor do I understand why result seems to inherently be an int. (Also the public static int sumDigits(long n) method was given to me by the book so that is what I am supposed to use).
import java.util.Scanner; public class Exercise6_2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter a integer"); long n = input.nextLong();
I am new to java programming and using bluej for programming and i have tried this question what i have have given in title ... how to find out the errors in the following program:-
class Primedig { public void getinput(int n) { int ctr=0; while(n!=0) { int r=n%10; for(int i=1;i<=r;i++)
[code] .....
output:-
For example input is 243 the output comes only 3 not 2& 3 both.
I have been working on this for hours and cannot get it to work properly. The program is to count the number of characters contained in both strings and display the results. It is also to display the final statement, but only once. This version has a complete statement for each matching character.
import java.util.Scanner; public class CountMatches { public static void main(String[] args) { String firstStr = (""); String s1 = firstStr;
I am trying to write a program that asks the user to enter a number from 1 through 10 and then the program will display the roman numeral for that number.
I am also adding a error message in which i haven't yet because im still trying to figure out how to the program will do the roman numeral.'
I have used the if and else if. but when i input a number it just repeats the number back to me.
The program cimpiles but it doesn't do what i want. here is what i have so far. how can i get the program to display the roman numeral after the number is entered.
import javax.swing.JOptionPane; public class Romannumeral { public static void main(String[] args) { double number;
Write a program using a while-loop (and a for-loop) that asks the user to enter a string, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the string. (So, you will have two separate program codes, one using a while-loop and the other one using a for-loop.)
Example: Enter a string: "Hello, JAVA is my favorite programming language." Enter a character: e The number of times the specified character appears in the string: 3
I don't even know where to begin I've only got this
import java.util.Scanner; public class letterCounter { public static void main(String [] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter a string"); String myString = sc.nextLine(); System.out.println("Enter a letter"); String letter = sc.nextLine(); } }
I have to store the name SD array and display the room and floor no. whose name is entered but i am having a problem with the logic part. The answer is coming to be wrong
import java .io.*; class hotel { public static void main()throws IOException { InputStreamReader read = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(read); String ar[][] = new String[5][10];
So I'm trying to make an applet that displays images of cards. The applet has a deck of 52 playing card images. It should shuffle the deck with a random number generator and display the first 10 cards of the shuffled deck.
With this code I have about 100 errors and I'm not sure what I'm doing wrong: I know I have to use arrays in order to ultimately display the images, but do I have to create Image objects for the Image [] cards array? I am getting many "class, interface, enum expected" errors as well as others.
import java.util.Random; import java.awt.Image; import java.applet.Applet; import java.awt.Graphics; public class unit12 extends Applet {
I'm creating a program that searches a txt file for a given string, then return the number line and the line itself. However, my testFile class isn't detecting my searchWord methods.
The searchWord and recursiveSearch is written in a java class called BasicFile
public List<String> searchWord(String key) throws Exception { LineNumberReader lnr = new LineNumberReader(new FileReader(f)); return recursiveSearch(lnr.readLine(), key, lnr); } public List<String> recursiveSearch(String currentLineText, String key, LineNumberReader lnr)
[code]....
Is it because I'm using a list instead of a string?
I am not getting the result I am looking for, but I'm sure I'm on the right track. I have an array, names + times of marathon runners, and should end up with a program that displays the slowest runner, and the next-slowest runner.
Currently, I get this: The lowest time is 273. The lowest time is 243.
Changing names.length to times.length changes nothing. for (i = 1;... ) changes nothing.
The displayed number should currently only be 243. Why is it displaying 273 first, and at all?
The code:
class Marathon { public static void main (String[] arguments) { String[] names = {"Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex", "Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda", "Aaron", "Kate"}; int[] times = {341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299, 343, 317, 265}; int min = times [0]; for (int i = 0; i < times.length; i++) { if (times[i] < min){ min = times[i]; System.out.println("The lowest time is " + min +"."); }
Write a class named FileDisplay with the following methods:
1.) constructor: the class's constructor should take the name of a fil as an arugment. 2.) displayHead: This method should display only the first five lines of the file's contents
Here is the following code I have made so far
import java.io.*; public class FileDisplay { private String filename; public FileDisplay(String Filename) throws IOException
[Code] ....
First, in my constructor I have taken in an argument and used that argument to open up an output file. Meanwhile, I'm trying to work n the displayhead method to print out information and to read data to. I haven't opened up my input file yet, but I'm not understand how can I read a print data to an output file. in
public void displayHead() {FileWriter file=new FileWriter(Filename)}
do I make create another instance of the filewriter class to output data?
In simple words, suppose to I want to display some messages on my output file in the displayhead function. Since I already have opened up the file in the constructor, how do I combine that in this method...
I am developing a product where the client needs to display the content of XML in a JSP page. This XML will be extracted from database and will be temporarily sotred in a String object and will then be flushed to JSP page for display. This was working fine unitl now. But now the customer wants display each tag with one color. attribute in another color and data in another color.
For ex: <?xml version="1.0" encoding="UTF-8"?>
this should be displayed in one color. Then
<ichicsrmessageheader>Data</ichicsrmessageheader>
In the above one the tag should be displayed in one color and "Data" should be displayed in another color
In the above sample tag should be in one color and attribute should be in one color ex: red and value of the attribute should be in another color ex: blue and "Data" should be in one color ex: green.
Here is the sample screen shot of how xml should is currently being displayed.
I have a beginning Java Program I have been working on that creates a number guessing program where the computer guesses a number that the user is thinking of within a certain range. I so far have the program below, but need getting rid of a few kinks/ adding features.
-First, I need to set it up so that the user will be prompted and give a range. It should run and produce something like this:
Welcome to this first-ever mind-guessing program!
Please pick a range (higher than 1 and no larger than 50): 32
You will choose a number between 1 and 32... and I will try to guess it.
With each of my guess, you will tell me whether I am too high (h or H), too low (l or L), match (m or M), or you want to quit (q or Q). My objective is to find the number using as few guesses as possible.
-Second, the game is supposed to give up and restart after failing the five, guesses, but for some reason, after it fails the fifth time, it prompts a fifth guess once again instead, then restarts after- I need to prevent this, so that it should look something like this:
My fourth guess is 17: h My guess is too high?
My fifth guess is 16: h *** I am unlucky this round. I give up.
Let's play!
My first guess is 10: etc..
import java.util.*; import java.lang.Math; public class numguessprac1 { // Declaring variables public static String input; public static int quit; public static int guess; public static int wins;
So I am currently writing my first assignment and have run into problems with my coding. The task was to have someone enter a 5 digit number and in return, I list each number on their respective lines. We also must create an error if a number other than 5 digits was entered. My problem is that when they enter a 1 or 2,3,4,6,7,8 digit number.. the error message occurs along with the rest of the messages (listing the numbers, etc). I want the program to end (or even re-ask to enter the numbers) if they incorrectly enter the data.
trying to write a program that takes a user inputted number and converts it to a binary number.
Here's what I have:
package com.java2novice.algos; import java.util.Scanner; public class Converter { static Scanner console = new Scanner(System.in); public void printBinaryFormat(int number){ int binary = console.nextInt();
In a forest, there are some bamboo trees .The length of each tree get doubled during winter and increases by one unit in summer , write a Java program to calculate the total length of n number of bamboo trees in M number of seasons. The season always starts with winter.
import java.util.Scanner; public class Tree { public static void main(String args[]) { int length; int season;
We have triangle made of blocks. The topmost row has 1 block, the next row down has 2 blocks, the next row has 3 blocks, and so on. Compute recursively (no loops or multiplication) the total number of blocks in such a triangle with the given number of rows.