Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I tried to put f1.start(); command at the main (when i marked it with B)
and it gives me an error and i am tryin to understand why.
so i changed it to where it is now, and my program compiles as it should be,
im just curious why.
thanks.
package Try;
import java.util.Random;
import java.util.Scanner;
public class Foo1 extends Thread {
private int min_, max_;
Foo1(int max, Integer min) {
max_ = max;
min_ = min.intValue();
}
public void run() {
Random rand_gen = new Random();
while(true) {
try {
Thread.sleep(rand_gen.nextInt(max_-min_) + min_);
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println("you got new message");
}
}
public static void main(String[] args){
System.out.println("Insert 1 to start"); // C
Scanner sc = new Scanner(System.in); // D
int i = sc.nextInt();
if (i == 1) {
Foo1 f1;
//f1.start(); // B
int max = 1000;
Integer min = new Integer(1000);
Foo1 f2 = new Foo1(max, min);
f1 = f2; // A
f1.start();
}
}
}
Because you declared f1 but you didn't initialize the variable with an instance of Foo1.
Have a look here for more information: A Guide to Creating Objects in Java
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
This java program is supposed to solve a puzzle. I don't understand what is going wrong in my program to cause these errors. Can anyone help?
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error, insert ". class" to complete Expression
The left-hand side of an assignment must be a variable
WRKR cannot be resolved to a variable
WRKR cannot be resolved to a variable
WRKR cannot be resolved to a variable
WRKR cannot be resolved to a variable
WRKR cannot be resolved to a variable
WRKR cannot be resolved to a variable
at MyClass.main(apartment puzzle.java:9)
public class MyClass {
public static void main(String args[])
{
boolean APART[] = new boolean [100];
for (int WRKR = 1; WRKR <= 100; WRKR++)
{
if (WRKR == 1)
{
for (int A = 0; A <= 100; A++)
{
APART[A] = true;
}
//System.out.println(Arrays.toString(APART));
}
else if (WRKR == 2)
{
for (int A = 1; A <= 100; A += 2)
{
APART[A] = false;
}
//System.out.println(Arrays.toString(APART));
}
else
{
int start = WRKR - 1;
for (int A = start; A <= 100; A += WRKR)
{
if (APART[A] == true)
{
APART[A] = false;
}
else
{
APART[A] = true;
}
}
//System.out.println(Arrays.toString(APART));
}
}
for (int i = 0; i <= 100; i++)
{
int A = i + 1;
if (APART[A] == true)
{
System.out.println("Apartment "+ A +" is open");
}
}
}
}
The code compiles properly. Your error message shows the file name as apartment puzzle.java. Every public java class needs to be in a file named after that class. Rename your file to MyClass.java and check it out.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I need to find the maximum and minimum statistics for the student marks from a list. The list contains the student ID and the student Mark. Im not too sure how to do this.
This is the code for reading in the file and creating the list:
public void readFile(Scanner in)
{
inputStudentID = null;
inputMark = 0;
try
{
File file = new File("Marks.txt");
in = new Scanner(file);
}
catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
System.out.println("in " + System.getProperty("user.dir"));
System.exit(1);
}
while (in.hasNextLine())
{
String studentRecord = in.nextLine();
List<String> values = Arrays.asList(studentRecord.split(","));
String inputStudentID = values.get(0);
String sInputMark;
sInputMark = values.get(1);
int inputMark = Integer.parseInt(sInputMark);
addStudent(inputStudentID, inputMark);
}
in.close();
}
Assuming you have some indexed list since addStudent doesn't really tell me much (else just apply the logic to whatever you have):
private int getMax(ArrayList<Integer> marks) {
int max = marks.get(0);
int index = 0;
for(int x = 1; x < marks.size(); x++) {
if(max < marks.get(x)) {
max = marks.get(x);
index = x;
}
}
return index;
}
Better solution:
You could simply do Collections.max(list) – Imaginary Pumpkin
Yet I'll still leave mine to show the logic of how to find a max.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
In my populateArrayEnemy method i populate array with 20 objects and at the end print them all.
But in my paint and update methods enemyShip.size returns 0.
Where did I go wrong?
Update:
My main method is below.
public class ActionEnemyShip {
EnemyShip es;
public ArrayList<EnemyShip> enemyShip = new ArrayList<EnemyShip>();
// Updating coordinates method //////////////
public void populateArrayEnemy(MainClass mc){
int Min = 0, Max=800, result;
for (int i =0; i <=20; i++){
x = Min + (int)(Math.random() * ((Max - Min) + 1));
y = 0 + (int)(Math.random() * ((600 - 0) + 1));
EnemyShip es = new EnemyShip(x,y);
enemyShip.add(es);
}
System.out.println(enemyShip);
}
public void update(MainClass mc, Player p){
for (int i = 0; i < enemyShip.size(); i++ ) {
EnemyShip es = new EnemyShip();
es = enemyShip.get(i);
es.setX(es.getX()+ENEMY_SHIP_SPEED);
enemyShip.set(i, es);
}
}
// Paint method //////////////////////
public void paint(Graphics g, MainClass mc) {
url = mc.getDocumentBase();
enemyShipImg = mc.getImage(url, "craft.png");
System.out.println(" paint "+enemyShip.size());
for (int i = 0; i < this.enemyShip.size(); i++){
EnemyShip es = new EnemyShip();
es = this.enemyShip.get(i);
g.drawImage(enemyShipImg, es.getX(), es.getY(), mc);
}
Here's my main method
public void init()
{
setSize(800, 600);
es = new EnemyShip();
as = new ActionEnemyShip();
//as.populateArrayEnemy(mc);
}
public void start() {thread.start();}
public void destroy() {running = false;}
public void stop() {running = false;}
public void run()
{
while(running)
{
as.update(this, p);
}
public void paint(Graphics g){
as.paint(g, this);
}
public void init()
{
setSize(800, 600);
es = new EnemyShip();
as = new ActionEnemyShip();
//as.populateArrayEnemy(mc); <== this is not happening
}
In general, this code is written pretty poorly and you should consider refactoring it. Your bug appears to be that as.populateArrayEnemy(..) is not being invoked because it's commented out.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I've been trying to write a simple program in java to find time complexity of a program.A program whih just searches for "for" loop or "while" loop and prints the no of iteration such as O(n) or O(2n) etc.
I got the i/p program in textarea.Is there any way by which i could do the opertaion?
Please any one help me.
This is not full proof, but would work for you
import java.util.StringTokenizer;
public class Complexity {
public static void main(String[] args) {
String input = "for(i=0;i<10;i++)\n{\nfor(i=0;i<10;i++)\n{\nfor(i=0;i<10;i++)\n{\n}\n}\n}\nfor(i=0;i<10;i++)\n{\n}\nfor(i=0;i<10;i++)\n{\nfor(i=0;i<10;i++)\n{\n}\n}";
int open_bracket=0;
StringTokenizer t = new StringTokenizer(input);
String result = "";
String token="";
int current = 0;
System.out.println("CODE \n"+input);
while(t.hasMoreTokens())
{
token = t.nextToken();
if(token.equals("{")) open_bracket++;
if(token.equals("}")) open_bracket--;
if(token.length()>=3) if(token.substring(0, 3).equals("for")) current++;
if(open_bracket==0&&token.equals("}"))
{
result += " n^"+current+" +";
current = 0;
}
}
if(result.length()>0) result = result.substring(0, result.length()-1);
result = "O( "+result+")";
System.out.println("RESULT = "+result);
}
}
OUTPUT
CODE
for(i=0;i<10;i++)
{
for(i=0;i<10;i++)
{
for(i=0;i<10;i++)
{
}
}
}
for(i=0;i<10;i++)
{
}
for(i=0;i<10;i++)
{
for(i=0;i<10;i++)
{
}
}
RESULT = O( n^3 + n^1 + n^2 )
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm a beginner in Java. I have the following situation:
The ArrayList contains {10234, 20233, 34546, 43546, 59865, 70002, 92435, 200354}
And the user inputs 20000 and 50000
And I want to get values from the ArrayList between 20000 and 50000, in this case I expect 20233, 34546, 43546 as answer.
How do I do this?
This is the code I have so far:
int t1 = 20233, t2 = 59865;
int i = 0;
boolean foundt1, foundt2;
foundt1 = false;
found20 = false;
while (i < a.size && (!foundt2)) {
if (a.get(i) == 10) {
foundt1 = true;
}
if (foundt1) {
System.out.println(a.get(i));
}
if (a.get(i) == 20) {
foundt2 = true;
}
i++;
}
Here is an example of what you are trying to do.
import java.util.ArrayList;
import java.util.Arrays;
public class Test{
public static void main(String[] args) {
ArrayList<Integer> numberList = Arrays.asList(10234, 20233, 34546, 43546, 59865, 70002, 92435, 200354);
for(int nbr : numberList){ //goes through the list
if( nbr > 20000 && nbr < 50000){
System.out.println(nbr);
}
}
}
}
Hope this helps!
Best Regards, Goatcat
You can use the Guava library.
The Iterables and Range classes can solve your task.
The sample:
public static void main(String[] args) {
List<Integer> values = Arrays.asList(10234, 20233, 34546, 43546, 59865, 70002, 92435, 200354);
Iterable<Integer> filteredValues = Iterables.filter(values, Range.closed(20000, 50000));
for (Integer val : filteredValues) {
System.out.println(val);
}
}
The output is:
20233
34546
43546
You cand find more info here