Question:
java .class expected and ;expected error . i m new to java.below is the program i wrote .?
ujjwal
2009-09-02 14:12:26 UTC
import java.util.Scanner;
public class gm {
int a,b,x;
public int ab(int x) {
if(a>=0 && a<=12)
x=a;
return x;
}
public int bc(int x) {
if (b>=0 && b<=59)
x=b ;
return x;
}
public static void main(String[]args) {

Scanner kbd= new Scanner(System.in);
gm g=new gm();
g.a=kbd.nextInt();
g.b=kbd.nextInt();
System.out.print(+g.ab(int x));
System.out.println(":"+g.bc(int x);
}
}
Three answers:
Mark aka jack573
2009-09-06 13:04:32 UTC
You problems are, as you said in this piece of code.



public static void main(String[]args) {



Scanner kbd= new Scanner(System.in);

gm g=new gm();

g.a=kbd.nextInt();

g.b=kbd.nextInt();

System.out.print(+g.ab(int x));

System.out.println(":"+g.bc(int x);

}



Firstly, you are getting two ints from the user. You need a third int to be able to pass to the ab and bc methods.



Without this third int, it is hard to say what to do.



Before I get to that though, your print statements should look like this:



System.out.print(g.ab(x)); // Removed the + and the int

System.out.println(":"+g.bc(x)); // Added a ) at the end before the semi colon and removed the int



Now, as I said, you need a third int to act as the x.



The reason for this is, you are setting the g.a as one int, and g.b as the second int.



In your ab method, you either want x to be a, or x, but without a value for x, you do not know what x is. If you passed in a, you would just get a back anyhow. For instance,

System.out.print(g.ab(g.a));

would just give you a anyhow. The method would not have any function.



The same with bc.

System.out.println(":"+g.bc(g.b));



How to get the program to work as you seem to want it to?



Ok, what I would do is something like this.



public class gm {

int a,b; // Removd the x here.

public int ab() { // Removed the int x here.

// Added this code, removed your code.

if (a < 0)

a = 0; // You do not want a a negative number.

else {

while (a > 12) {

a = a - 12;

}

}

// You could have

// else {

// a = a % 12;

// }

// Instead of the else and while loop.

return a; // Return a, not return x.

}

public int bc() { // Removed int x here

// Added all of this, removed your original code

if (b < 0)

b = 0; // You do not want a negative minute.

else {

while (b > 59) {

b = b - 60;

a = a + 1;

}

}

return b; // Changed to return b.

}



Then in your main method,

System.out.print(g.ab());

System.out.println(":"+g.bc());



Good luck with it.
Jon
2009-09-02 14:32:34 UTC
1) In your second to last statement, there should be something before the +

2) In both of your last two statements, you don't need to tell it again that x is an int.

3) In fact, you seem to be doing double-duty here with your function params/return value and your a, b, and x. For instance, if you're returning an int...do you really *need* to set it as a class variable? If I want it, I can get it from one of those places or the other...I don't need it to be in both.



Also, as a matter of coding conventions:

1) Don't give your class a name that starts with a lowercase letter. Class names should always be capitalized.

2) Names like Gm, a, b, x, ab, bc, and g are meaningless. What do these things mean, or do? For instance, it looks like maybe you're writing a program to deal with times expressed in hours and minutes? I had to look at it for a good 30 seconds before I figured that out. Why not call "ab" something like "validateHour", and then it would be immediately obvious what it does?



Your program seems to have some other design problems as well, but this is a good starting point. Keep it up - this stuff is hard at first, but then it gets a lot easier.
mantyla
2016-10-19 05:26:58 UTC
extraordinarily much each little thing of this has some thing incorrect with it. i'm not sure particularly what you have been attempting to do. in case you have a string variable, you are able to evaluate it against a string value with this: if (stringvar.equals("stringvalue"))


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...