Pretty new here in java. I'm trying to add methods to my otherwise currently working program to print out amount of characters from an args array. Trying to install a new method, I'm having trouble with calling args. This is my current code, and the red outline is.
static void amountOfCharsInSentence() {
int sum=0;
for (String s: args) { //args on this line is marked red
sum+=s.length();
}
}
public static void main(String[] args) {
amountOfCharsInSentence();
}
Any hint or tips in the right direction would be appreciated.
args is an argument to main, which means you can only use that argument (which is like a local variable) within main.
If you want to use it elsewhere, you have two options. The most suitable is to pass it to your other function as an argument:
static void amountOfCharsInSentence(String[] args) {
// Declare argument here -----------^^^^^^^^^^^^^
int sum=0;
for (String s: args) { // `args` here is the argument to *this* function
sum+=s.length();
}
}
public static void main(String[] args) {
amountOfCharsInSentence(args);
// Pass it here --------^^^^
}
Your other option is to create a static member of your class, which all methods in your class have access to, and assign args to that static member:
class YourClass {
static String[] commandLineArgs;
static void amountOfCharsInSentence() {
int sum=0;
for (String s: commandLineArgs) { // Use the static member here
sum+=s.length();
}
}
public static void main(String[] args) {
commandLineArgs = args; // Assign the static member here
amountOfCharsInSentence();
}
}
...but that's probably not appropriate in this case.
You have not passed the args.
static void amountOfCharsInSentence(String[] args) {
int sum=0;
for (String s: args) { //now it will not give red line.
// red line is indicated because there is no args inside this method
sum+=s.length();
}
}
public static void main(String[] args) {
amountOfCharsInSentence(args);
}
Hope this solves your problem
Related
I'm searching for modifier in Java that has the same exact purpose as Static in C++ has. I mean, that variable is only initialized once in function, then every time we call that function again, values from the previous call are saved. That's how code looks in C++:
void counter()
{
static int count=0;
cout << count++;
}
int main()
{
for(int i=0;i<5;i++)
{
counter();
}
}
and the output should be 0 1 2 3 4, is there something that has the same purpose, but in Java?
looks like you are starting with java. To help you understand the concept i wrote the same code with comments for your understanding.
package yourXYZpackage;
public class yourABCclass{
//Declare in class body so your methods(functions) can access
//and the changes made will be global in C++ context.
static int count=0;
void counter()
{
count = count++;
System.out.println(count);
//thats how you can display on console
}
//this is the main method like C++
public static void main(String[] args){
for(int i=0;i<5;i++)
{
counter();
}
}
}
hope this will help..//
u need to create a constructor first.
public class answer2 {
static int count =0;
answer2() {
System.out.println(count);
count++;
}
public static void main(String[]args) {
for(int i=0;i<5;i++) {
new answer2();
}
}
}
just define static variables as a class member normally. java developers promote object oriented programming so even your main function is a method defined in another class whose name is same as your program name.
now for your question if you want to define a static variable:
public class abc
{
public static int count = 0;
}
public class xyz
{
public void increment()
{
abc.count++;
}
public static void main(String[] args)
{
System.out.println(abc.count);
increment();
System.out.println(abc.count);
}
}
Hope it helps.
Im new in Java why is my method not returning the variable I put. I want to use the return thing I dont want to use sysout but its not working.
public static void main(String[] args) {
counter("this is a test");
}
public static int counter(String sentence) {
int count = 0;
while (CODE DELETED){
count=count+1;
MORE CODE DELETED
}
CODE DELETED
return count;
}
It does return it, but you neither assign the value, nor do you use it.
Try this:
public static void main(String[] args) {
int result = counter("this is a test");
System.out.println("result = " + result);
}
The method is returning the value, but you are not doing anything with the returned value.
Maybe you misunderstand what "returning a value" means. It does not mean that the returned value is automatically printed to the console.
You'll have to put the returned value in a variable in the main method, and then you can for example print it:
public static void main(String[] args) {
int value = counter("this is a test");
System.out.println(value);
}
You can also print it directly, not storing it in a variable:
public static void main(String[] args) {
// Print whatever the call to the method 'counter(...)' returns
System.out.println(counter("this is a test"));
}
I try to learn JAVA language. I write simple program, but it don't work. Could somebody give to me advice how solve problem.
My program:
class WriteOut {
private static int sumas;
public void sud(int sds) {
sumas = sds;
}
public static void main(String arg[]) {
WriteOut sum =new WriteOut();
sum.sud(5);
System.out.println("suma: "+sum);
}
}
Output I get "suma: bandymas.WriteOut#70dea4e";
I want get answer "suma: 5"
System.out.println() calls toString() method, which comes from basic Object class.
There are two ways of fixing this code:
1) Override the standart toString() method:
class WriteOut {
private static int sumas;
#Override
public String toString(){
return String.valueOf(sumas); // returns a string with sumas value
}
public static void main(String arg[]) {
WriteOut sum =new WriteOut();
sum.sud(5);
System.out.println("suma: "+sum);
}
}
2) Or just change the System.out.println() call:
class WriteOut {
public static int sumas; // To allow System.out.println() to see this variable
public void sud(int sds) {
sumas = sds;
}
public static void main(String arg[]) {
WriteOut sum =new WriteOut();
sum.sud(5);
System.out.println("suma: "+sum.sumas);
}
}
change what's inside System.out.println() to :
System.out.println("suma: "+sum.sumas);
Exmple:
class WriteOut {
private static int sumas;
public void sud(int sds) {
sumas = sds;
}
public static void main(String arg[]) {
WriteOut sum =new WriteOut ();
sum.sud(5);
System.out.println("suma: "+sum.sumas);
}
}
Output :
suma: 5
I've written up a code with several different methods. I can't seem to find the correct way to get the last method I have into the main method so it can print out the correct output.
CODE:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a number:");
int num = in.nextInt();
} // this is my main method
public void results (int num) {
for (int i = 1; i < num; i++) {
System.out.print(space(num - i));
System.out.println(method1(i));
}
for (int i = 0; i < num; i++) {
System.out.println(method2(num-i));
System.out.print(space(i));
}
} //this is the method that I want inside my main method
I thought I could simply put System.out.println(results(num)); into my main method but that doesn't work. Can anyone explain what I'm doing wrong and help me with this problem?
Your main method is static, but your results method is not. Either make results be static, or declare a new instance of your class to use inside of main.
public class MyClass
{
public static void main(String[] args) {
results(1);
}
public static void results (int num) {
}
}
or
public class MyClass
{
public static void main(String[] args) {
new MyClass().results(1);
}
public void results (int num) {
}
}
You can't pass what results returns to anything such as System.out.println, because results has a void return type. But results already is printing out information, so just call results.
results(num);
I am having to write a program that loads the first 10 integers' into an array. I've tried it every way and have not been able to make it work with an array. This is as far as I have gotten.
public class midtermcube2 {
public static void main(String[] args) {
int totz;
for (int numz=0; numz<=9;numz++)
{
totz=numz*numz*numz;
System.out.println(totz);
}
}
}
try this:
public class AddArray
{
public static void main(String[] args)
{
int arr[]=new int[10];
for(int i=0;i<10;i++)
{
arr[i]=i;
System.out.println(arr[i]);
}
}
}