I wrote a program that find adjacent value for -1 .ex:
input
1
3 3
1 2 2
1 1 -1
5 5 0
output
1
code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
import java.util.Scanner;
Assignment: Given an array of scores sorted in increasing order, return true if the array contains 3 adjacent scores that differ from each other by at most 2, such as with {3, 4, 5} or {3, 5, 5}.
public boolean scoresClump(int[] scores) { for (int i = 0; i < scores.length; i++) { int a = scores[i]; int b = scores[i + 1]; int c = scores[i + 2]; if (Math.abs(b-a) + Math.abs (c-b) <=2) return true; } return false; }
I got it right for some of the input, but not one of them. I tried to use the for loop and if statement on a specific input that I got wrong:
I suspect it has something to do with the for loop, but I don't see the problem with it. It should work, shouldn't it? But anyway, here is the error for {4,5,8} :
I'm not sure why, but my program does not produce an output. It is supposed to output total charges.
/*This program provides the user with the price of their monthly internet service based on two inputs.The program asks users which subscription they have. The options are A, B, or C. This is the first input.
The program then will ask the user how many hours they used the internet for. This is the second input.Then, based on the package number, the program will compute their monthly bill. This is the output.
The program calculates the price based on the following prices:
Package A: For $9.95 per month, 10 hours of access are provided. Additional hours are $2.00 per hour. Package B: For $13.95 per month, 20 hours of access are provided. Additional hours are $1.00 per hour. Package C: For $19.95 per month, access is unlimited.*/ import java.util.Scanner; public class Lab03SV
My program successfully reads a file, reports back what it finds and creates an output file. However, I cannot get it to write to the output file, it is always blank!
Here's my code:
import java.util.Scanner; import java.io.*; public class Ex19 { public static void main (String [] args) { //Variables
Write a program to create an integer array of size 20. Then, the program should generate and insert random integers between 1 and 5, inclusive into the array. Next, the program should print the array as output.
A tremor is defined as a point of movement to and fro. To simulate it, the program will generate a random number between 0 and 19, which represents the location in the array (i.e. index number). Then, the 3 numbers to the left and right of this location should be reset to the value 0. If there isn't 3 numbers to the left and right you may assume a lesser number depending on the boundaries of the array.
Then, the final array should be printed as output. There is no user input for this program.Your program must include, at least, the following methods:
-insertNumbers, which will take as input one integer array and store the random numbers in it. -createTremor, which will generate the random number as the location and return it.
So I'm pretty much done with this project except I am unable to get the println statements of "Produced" & "Consumed" to display in my JTextArea on my GUI. I am completely lost as to exactly why the program just continues to create new windows repeatedly crashing. I will include the code to the 3 classes I'm working with - I'm capturing with the fac.println("") on both consumer and producer and have a method in the Factory class that tells it to print in JTTextArea
import java.util.Date; public class Producer implements Runnable { private Buffer<Date> buffer; private Factory fac = new Factory(); public Producer(Buffer<Date> buffer)
I am trying to write a program in Java that uses threads.Below is the program requirements:The goal of this assignment is to create a routine which creates multiple threads, has them do work in parallel, and terminates when the last thread has finished.
The Scenario: There are several groups of people in a bar watching the Olympics cheering for their country. Each group will cheer for their country some given number of times, with a random pause (between 2 and 5 seconds) between each cheer. There is enough room at the bar for up to ten different groups to sit (each would be cheering for a different country).
The Program: The task is to write a program that will simulate these cheers using threads. The program should be called cheer.X (X being the language of choice). You may use any language that supports threading. When the program is run it should ask for the number of countries and then the name and how many times it will be cheered for. The main function will then create a thread for each team and each thread is responsible for cheering the specified number of times for the correct team at the random interval. You will submit the proper source code file for me to open and compile myself, not an executable.
An example run would look something like this: How many countries are supported at the bar? 3 Enter the first: China How many cheers? 2 Enter the second: USA How many cheers? 4 Enter the third: Russia
I have printed the result of my program into an output file.
For some reason I can't figure out how to get the file output.txt to actually print.
I've tried printing it like I would normally print a file but it's not working.
Java Code:
final PrintStream console = System.out; File file = new File("output.txt"); PrintStream out = new PrintStream(new FileOutputStream(file)); System.setOut(out); System.out.println(collection.toString()); System.setOut(console);
What do I do after this? mh_sh_highlight_all('java');
I have written java program which can extract online data from url and i want to store it in data base in new token row in table so than i can use in my application . how could it be done using type4 drive ..
These are my code ...
import javax.swing.text.html.parser.*; import javax.swing.text.html.*; import javax.swing.text.*; import java.io.*; import java.util.StringTokenizer; import java.net.*; public class ParseTest extends HTMLEditorKit.ParserCallback {
I am aware that the default value of char is 0. But in this program I am getting some unexpected output
public class TestClass12{ static int[] ia = new int[1]; static char ch; public static void main(String args[]) throws Exception{ System.out.println("ch:"+ch+" ia[ch]:"+ia[ch]); } }
Output: ch: ia[ch]:0
If you see the above output ch is printed as a blank space instead of 0, while the array default value is correctly printed by taking the char default value as index. Why is ch printed as blank space?
Write a program to request a student number and their marks in 4 subjects. The program must print the student number, total marks and average mark. All outputs should be printed with suitable labels
This is the error am getting
Scanner get = new Scanner (System.in) - got a red line
int student number, total marks, average mark - got a yellow bulb with a red exclamation mark on it.
I just dont understand where the errors is where what the system dont have ... I am using netbeans ....
I have made a program which finds the number of occurrence of the words. It is as follows :
public class B { public static void main(String[] args) { Map<String, Integer> mp = new LinkedHashMap<String, Integer>(); String s = "This is me tarun ohri This"; Scanner p = new Scanner(s); int i = 0;
[Code] .....
Output is coming {This=0, is=1, me=1, tarun=1, ohri=1}
I have to write a program that find the sum of two numbers 62 and 99 and stores them in a variable named total. However, I have one error that I just can't get rid of and can't tell what it is. I'm using jGrasp and here's what it says:
Programming Challenge #5.java:14: error: class SumofTwoNumbs is public, should be declared in a file named SumofTwoNumbs.java public class SumofTwoNumbs { ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete.
and here: is my code:
import java.util.Scanner; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** // This program has variable of several of the integer types. public class SumofTwoNumbs { public static void main(String[] args) {
I have to write a program the calculates the MPG and display's the result on the screen. However, I have three errors I just can't seem to figure out. jGrasp points to the following error, but doesn't tell me what it is:
Programming Challenge #9.java:34: error: cannot find symbol gallons = keyboard.nextInt(); ^ symbol: variable keyboard location: class MPG 3 errors
----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete.
My code is as follows:
import java.util.Scanner; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */
// This program has variable of several of the integer types. public class MPG { public static void main(String[] args) {
im trying to do a program to find if numbers are consecutive or not! if they are consecutive i need a true as a return and a false when they are not... i did this program and im sure i did something wrong because i keep only true returns ..
Example: (3,5,7) (1,2,2) (7,7,9) should return a false! Java Code: import java.util.*; public class Consecutive{ public static void main (String [] args){ Scanner console= new Scanner(System.in); System.out.println("Enter three numbers"); String numbers = console.nextLine(); System.out.println( "The numbers (" + numbers + ") is '" + consecutive( numbers ) + "'" ); }//end of main
the below program is to read the time intervals (HH:MM) and to compare system time if the system time between your time intervals print correct time and exit else try again to repeat the same thing. By using StringToknizer class. and i have written like this
import java.io.*; import java.util.*; public class Main { static int k1,k2,v1,v2; public static void main(String args[]) throws IOException { DataInputStream o=new DataInputStream(System.in);
[Code] ....
but is showing correct for some inputs and wrong for some inputs ....
I'm having some trouble with getting this program to read an input of morse code and then produce an output of English. When typing in morse code for the phrase 'the string', the output looks something like this:
So I am trying to write the output of two different java class files to one txt file while the program runs. The file name is determined before the program is ran, through the command prompt as arguments. How can I get the second class file to edit the same txt file without running into compile errors.
For right now I'm just going to send everything that the second file outputs to a message String variable, so that the Main class outputs to the the text file. I still want to learn how to write to the same text file directly from the second class file.
import java.io.*; public class Test{ public static void main(String[] args) throws IOException{ int x; //create a new file internally called doc. But externally labelled the user input File doc = new File(args[0]); if (doc.exists()){
I finished a game in Java and sent it to a friend. Launching the program in my computer worked just fine.
But he got this error : "Could not find the main classs: Main. program will exit"
My JRE version is the most updated one. His JRE was version 1.6. He updated his JRE, and the problem was solved.
This is a bit worrying for me, because as far as I know, 1.6 isn't a very old version of the JRE. It's not the most recent one, but not that old.
This is worrying because I'm planning on sending my game to a lot of friends, and trying to distribute it on the internet.
A lot of people don't have the most updated JRE. And they are mostly non-programmers, so I can't expect them to update to the newest version of Java upon downloading my game. They might not know what Java is, even though they got it on their computer, and upon receiving an error, they'll just give up on the game.
If my game wouldn't work with a significantly old JRE, that would be reasonable. It's part of the nature of working with Java. But the fact that a relatively updated JRE, 1.6, doesn't work with my game, is worrying.
*(Please note: My game isn't implementing anything "special". Swing and KeyBindings are the 'newest' additions to Java that I can think of inside my game)*.
In short, I'd like to know that my game works on most of the computers it tries to run on. Knowing that it doesn't work on a relatively new JRE, is worrying.
So I have two questions:
1. Is it normal, for a Java program, to have such "high" demands for the JRE version? Do a lot of Java games demand at least version 1.6 of the JRE? Is this common?
2. How can I find out the minimum JRE version requirement for my program? Is there a methodical way to do this, or do I just have to go through all the libraries I use in my game and figure out what's the JRE release version for each one?