I am new at programming and currently in our classes we are learning java. I am trying to create a routine in which I need to use String variables only. Below it is the code in which I am working with:
public static void main(String[] args) throws java.io.IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System. in ));
PrintStream out = System.out;
String hair.equals("damagedHair");
cutHair(marvin);
cleanHair(michelle);
for (int i = 0; i < 2; i++) {
static void cutHair(String marvin) {
String cabello;
marvin.equals(hair);
if (marvin.equals("damagedHair")) {
cabello.equals("newHaircut");
result(hair);
}
static void cleanHair(String michelle) {
String hair;
michelle.equals(hair);
if (michelle.equals(newHaircut)) {
hair.equals("putShampooAndConditioner");
result(hair);
}
static void result(String pHair) {;
PrintStream out = System.out;
out.println("=============");
out.println(pHair);
out.println("=============");
}
}
Jcreator is giving me an error that says Illegal start of expression and also java 50 error ';' expected.
I am not sure why is this coming up and I am a little confused as to whether I am doing something I am not supposed to and how to correct it. Sorry about the double posting, this is the right message. Need some help from you guys to figure this out.
Thanks in advanced!
This is in your main:
for(int i=0; i<2; i++)
{
static void cutHair(String marvin)
{
String cabello;
marvin.equals(hair);
if(marvin.equals("damagedHair"))
{
cabello.equals("newHaircut");
result(hair);
}
}
You cannot define methods inside of main. Also, hair is not in scope here, ie it's in your main, not your method. Additionally, you're constantly only declaring variables, and then using them without them ever having been initialized. For example, in the above method, you have:
cabello.equals("newHairCut")
but cabello was never initialized, this should give you a might not have been initialized warning. Or earlier in your code, you have:
String hair.equals("damagedHair");
Again, this doesn't make any sense. You just declared hair here, you cannot call methods on it until you initialize it. I suggest that you review some tutorials.
Related
This code is supposed to count from an inputted parameter from a user, pass it to a function and it counts it down to one. I believe the program acts as it should, the problem is that it doesn't pass Mooc.fi's tests
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number = Integer.valueOf(scanner.nextLine());
printFromNumbertoOne(number);
}
public static void printFromNumbertoOne(int number) {
for (int i = number; i > 0; i--) {
System.out.println(i);
}
The error I'm receiving is:
Method printFromNumberToOne(int) of class FromParameterToOne missing
What am I missing? And is there a way to check the test cases? Mooc.fi seems like it's very picky on what it takes for answers.
Thanks!
CAMELCASE of the method!
After beating my head against this problem all day it came to me after taking a break.
It should be printFromNumberToOne
I'm having a little bit of a play around with the Java Robot function.
However as i think it's a major pain in the backside i was wondering if i could abstract the function and create a faster way.
Now Firstly i have done the following:
public abstract class Cyborg {
public static void Cyborg(KeyEvent args[]) throws AWTException{
try {
Robot robot = new Robot();
for(KeyEvent k:args){
robot.keyPress(KeyEvent.k);
}
} catch(AWTException e){
e.printStackTrace();
}
}
}
Which i am currently a little baffled with i am currently getting an error that k cannot be resolved. However in the KeyEvent args[] section should this be classed as a KeyEvent or should this just be a string? As i am from a PHP background i am starting to get to grips with java. Can anyone advise if this is the best way to go about this? And also as to why KeyEvent.k won't resolve whether it is a KeyEvent or if it is a String? Any other advise on if this will work/won't work or issues to try and avoid would be great.
Thank you
robot.keyPress(KeyEvent.k);
This won't work, because this will attempt to find a static member of the class KeyEvent called k. You've named your incremental object k, so use k.
robot.keyPress(k.getKeyCode());
More thorough Explanation
When you say the following:
String[] strs = {"hello", "there", "my", "friend"};
for(String str : strs)
{
// Do something.
}
What you're making the computer do has the same effect as:
for(int x = 0; x < strs.length; x++)
{
String str = strs[x];
}
So you're defining a String object named str that will contain the current String object that is contained within the strs array.
This line
robot.keyPress(KeyEvent.k);
Should be like this
robot.keyPress(k.getKeyCode());
I am trying to run this program but I cannot, the compiler is sending me a ".class" error.
Can somebody help me with my problem and if it is possible a general tip about ".class" error?
Here is the program:
import java.io.*;
class Bus
{
private int kostos;
private int plithos;
private int typepiv;
Bus(int x,int y,int z)
{
kostos=x;
plithos=y;
typepiv=z;
}
public void KB(int[] x)
{
try{
for(int i=1;i<5;i++)
{
if(typepiv==2)
{
plithos=plithos+plithos/2;
kostos=kostos-kostos/2;
}
if(typepiv==3)
{
plithos=plithos-plithos/5;
kostos=kostos-kostos*25/100;
}
if(typepiv==1)
{
plithos=plithos;
kostos=kostos;
}
x[i]=plithos*kostos;
}
} catch(Exception ex){
ex.printStackTrace();
}
}
}
class testBus
{
public static void main(String args[])
{
String leof[]=new String[4];
int leof1[][]=new int[4][3];
for(int i=1;i<5;i++)
{
System.out.println("dwste onoma leoforiou");
leof[i]=UserInput.getString();
System.out.println("dwste kostos thesis enilika");
leof1[i][1]=UserInput.getInteger();
System.out.println("dwste plithos thesewn");
leof1[i][2]=UserInput.getInteger();
System.out.println("dwste tupos epibath gia enilikes=1,gia
paidia=2,gia suntaksiouxous=3");
leof1[i][3]=UserInput.getInteger();
Bus leof2=new Bus(leof1[i][1],leof1[i][2],leof1[i][3]);
}
int KostEnoik[]=new int[4];
----->leof2.KB(KostEnoik);
System.out.print("onoleo");
System.out.print(" ");
System.out.print("plithos");
System.out.print(" ");
System.out.print("kost(EURO)");
System.out.print("typepiv");
System.out.print(" ");
System.out.print("apotelesma kostEnoik");
for(int g=1;g<5;g++)
{
System.out.print(leof[g]);
System.out.print(leof1[g][2]);
System.out.print(leof1[g][1]);
System.out.print(leof1[g][3]);
System.out.print(KostEnoik[g]);
}
}
}
the compiler message says :
testBus.java:56:error:cannot find symbol
leof2.KB(KostEnoik);
symbol:bariable leof2
location:class testBus
1 error
Remove the array brackets [] when invoking KB
leof2.KB(KostEnoik);
and remove the preceding enclosing brace }.
Aside: Java naming conventions indicate that variables start with a lowercase letter e.g. kostEnoik. Also consider giving the method KB a meaningful name, e.g. calculateCost
Read Java naming conventions
concern is with your access
leof2.KB(KostEnoik[]);
You are trying to access the "leof2" variable outside of the scope in which it is defined i.e. outside for loop and scope is upto for loop and that's why the compiler will not be able to find that varialble .
leof1[i][3]=UserInput.getInteger();
Bus leof2=new Bus(leof1[i][1],leof1[i][2],leof1[i][3]);
}
int KostEnoik[]=new int[4];
leof2.KB(KostEnoik[]);
You are trying to access the "leof2" variable outside of the scope in which it's defined (in this particular case, the for loop) and that's not allowed.
method KB takes an int array as argument, but you don't have to add the [] when passing the argument. The correct line is
leof2.KB(KostEnoik);
However, there's something pretty odd with you logic: you're repeatedly (for loop) setting leof2, but only the last iteration of the loop will have any effect. I'm almost certain that that's not what you actually want, but the correct answer to where Bus leof2 should actually be defined depends on the correction of that issue.
leof2.KB(KostEnoik); this is the main culprit. whether you have imported UserInput.
Also try to go through the Java Basics
any method can be invoked using object when it is non static or class name when it is static. Please consider this link
Get leof2 object out side the For Loop.
Don't type [] when you pass the array as argument "leof2.KB(KostEnoik[]);".
In this book, there is this example of how to use static variables and methods. I dont understand what is going on. explain why there has to be static in front of the methods and variables. There are two seperate classes called Virus and VirusLab. The VirusLab.java takes in a command line argument and makes the amount of Virus objects, then spits out the number of Virus objects. Thanks
Virus:
public class Virus {
static int virusCount = 0;
public Virus() {
virusCount++;
}
public static int getVirusCount() {
return virusCount;
}
}
VirusLab:
public class VirusLab {
public static void main(String[] args) {
int numViruses = Integer.parseInt(args[0]);
if (numViruses > 0) {
Virus[] virii = new Virus[numViruses];
for (int i = 0; i < numViruses; i++) {
virii[i] = new Virus();
}
System.out.println("There are " + Virus.getVirusCount()
+ " viruses.");
}
}
}
A web search would have given you hundreds of links to explain 'static' keyword in Java.
Please refer to the following documentation: http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
Also, please refer a text book for your further learning, that will help a lot.
I will keep it pretty short
You need static variables if you want that variable to be shared
across all the objects of your class,so that if one of the object
changes its value it would be reflected in other objects as well,which
is what is exactly done in above example.
A static variable is one which is not associated with an instance of a class.
This means you don't have to create a new instance of the class to access the variable from another class. Consider this:
public class Login {
public static String loggedInUser
public void setLoggedInUser(String name){
this.loggedInUser = name;
}
}
To access the String loggedInUser in another class, you wouldn't have to say
Login login = new Login();
String username = login.loggedInUser;
You'd just have to say
String username = Login.loggedInUser;
This can be useful in accessing variables outside of the class they were set in.
Hope that helps.
try reading this answer I gave in a previous question:
Accessing Static variables
and make google and wikipedia ur friend they'll save u time coming on here posting questions ,waiting and refreshing ur page to check if any answers were given.
I'm learning about constructors.
When I try to compile the following code, I get the error "variable input and shape are not initialized."
Could anyone tell me why and how to solve it?
public class Try {
public static void main(String[] args)
{
String input;//user key in the height and width
int shape;//triangle or square
Count gen = new Count(input , shape);//is this the right way to code?
gen.solve();
}
}
public class Count {
public Count(String inp, int shp) {
String input_value = inp;
shape_type = shp;
}
public void solve () {
if shape_type==3{
//count the triangle
}
else if shape_type==4{
//count the square
}
}
}
You haven't given shape or input values yet before you try using them. Either you can give them dummy values for now, like
String input = "test";
int shape = 3;
Or get the string and integer from the user; in that case, you might want to take a look at how to use a Scanner.
By leaving input and shape without values, at:
String input;
int shape;
they are uninitialized, so Java doesn't know what their values really are.
I assume this is some kind of homework. I took the liberty of reformating and fixing your code a little.
You have to initialize any variable you are going to use. The only exception is when you are using class members (those are initialized automatically to some default value). See below that the members of the Count class aren't explicitly initialized.
This is some working code. Also note that i change the solve method a little (the if blocks should have had () around the expression. But what you are trying to do is usually better done with a switch block as shown below. Also I declared two members inside the Count class to remember the values provided at construction time in order to be able to use them when calling the solve() method.
public class Try {
public static void main(String[] args) {
String input = null; //user key in the height and width
int shape = 0; //triangle or square
Count gen = new Count(input, shape);//is this the right way to code?
gen.solve();
}
}
class Count {
String input_value;
int shape_type;
public Count(String inp, int shp) {
this.input_value = inp;
this.shape_type = shp;
}
public void solve() {
switch (this.shape_type) {
case 3:
// count the triangle
break;
case 4:
// count the square
break;
}
}
}
Proper formatting of the code usually helps :).