I'm trying to pass a String via Command Line in java, but it returns only the first value i.e. args[0]
Below is what I've done
public class CommandLine
{
public static void main(String[] args)
{
int i;
i = args[0].length(); //throws error here if args.length();
System.out.println(i); //checking length, return with args[0] only
while(i>0)
{
System.out.println(args[0]);
i++;
}
}
}
What should I do to improve this and make it working?
Here are few things to be addressed
In your logic command line arugment length is taken in wrong way.
Loop condition is not apt for your requirement, moreover It is an infinite loop or never ending loop which reduces the performace of the code.Should never use infinite loops in the code.
3.you are printing the same index ie.. args[0] every time inside loop.
Code:
public static void main(String[] args)
{
int i=0;
int len = args.length; //use length only in this case;
System.out.println(len); // this will return it properly now
while(i<len)
{
System.out.println(args[i]);
i++;
}
}
Related
public static void main(String args[]){
extract("e:\\");
}
public static void extract(String p){
File f=new File(p);
File l[]=f.listFiles();
int counter = 0;
for(File x:l){
if(x.isDirectory()) extract(x.getPath());
else if(x.getName().endsWith(".mp3"))){
counter = counter + 1;
}
}
// I want to count and return value at last
}
Using this method(above), resets the counter every time when for loop ends.
So here, I want to count even when the for loop ends so that I can keep track of the number of .mp3 files.
I want to count and return value at last.
Return the current counter and make sure you add the response when you call recursively back to the counter.
public static void main(String args[]) {
int count = extract("../");
}
public static int extract(String p) {
File f = new File(p);
File l[] = f.listFiles();
int counter = 0;
for (File x : l) {
if (x.isDirectory()) {
counter += extract(x.getPath());
} else if (x.getName().endsWith(".mp3")) {
counter++;
}
}
return counter;
}
You should not use recursion like that. (Recursion should always have a termination condition to avoid running in a loop forever!
I think the problem is, that you create new Integers every time you call your method, so you will eventually override your old value or even use many different Integers to count.
You can wrap everything in a class in which you keep track of one integer.
Some pseudo-code:
public class Mp3Counter {
private int numberOfMp3 = 0;
public void extract(...) {
if (foundMp3) {
this.numberOfMp3 += 1;
}
}
}
public class Dupzero {
static int dup =0;
public static void duplicateZeros(int[] arr) {
//read all the elememts in arrays
for(int i=0;i<arr.length;i++) {
if(arr[i]==0) {
for(int j=arr.length-2;j>=i;j--) {
arr[j+1]=arr[j];
}
i=i+1;
}
}
System.out.println(Arrays.toString(arr));
}
public static void main(String[] args) {
int []arr = {
1,0,2,3,0,4,5,0
};
duplicateZeros(arr);
}
}
I wrote this code and i get expected output. Previously i was getting wrong output as i missed the statement i=i+1; .. Can you explain me here in the code what is i=i+1 doing ? is it in the right place ?
i = i+1 increments the counter of the outer for-loop by 1. When you copy a 0 to the right you need to ignore that copied 0 value as you continue to step through. Otherwise, you will just fill out the array with 0s.
Is it in the right place? Well, you say its working so it seems to be. Write some tests to verify the expected behavior.
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 2 years ago.
Improve this question
I had a code I needed to submit and every time I try to run it, I get the same errors over and over again.
Here's the question
Write the following Java methods:
(a). readValues to input ten integer values into an array of integers
TAB from a text file “Values.txt”. This array TAB is passed to the
method as parameter. Assume that the number of students in the file is
equal to the length of the array.
(b). oddValues that takes the array TAB as parameter and returns the
number of odd values found in TAB.
(c). replaceOdd that takes the array TAB as a parameter. It should
replace every odd value in TAB by the sum of all odd values.
Hint: your method must first compute the sum of all odd values.
(d). printValues that takes the array TAB as a parameter and prints
its content on the screen.
(e). main that declares the array TAB and calls the above four
methods.
N.B.: In your program, use the methods and variable names as mentioned
above.
And this is the code:
import java.util.*;
import java.io.*;
public class Finalexam
{
public static void main (String [] args ) throws FileNotFoundException
{
int sum=0;
int [] TAB=new int [10];
ReadValues(TAB);
oddValues(TAB);
replaceOdd(TAB);
printValues(TAB);
System.out.println("The sum is" + sum);
}
public static void ReadValues (int [] TAB)
{
{ int i;
for(i=0; i<10; i++){
Scanner s = new Scanner ("Values.txt") ;
TAB[i]=s.nextInt();
}
}
s.close();
}
public static double oddValues(int[] TAB)
{
int i;
double odd=0;
int fn=0;
for(i=1; i<odd; i++){
while(odd % 2 !=0)
{
odd = fn;
}
break;
}
return fn;
}
public static int replaceOdd(int[] TAB)
{
int re=0;
for(int i=0; i<TAB.length; i++){
re = re/TAB.length;
}
return re;
}
public static void printValues(int[] TAB)
{
int i;
for(i=0; i<10; i++){
System.out.println(TAB[i]+"\t");
}
System.out.println();
}
}
In which part I'm doing wrong? I cant even run it.
Firstly there is a compilation error in your code.
In your method
public static void ReadValues (int [] TAB)
{
{ int i;
for(i=0; i<10; i++){
Scanner s = new Scanner ("Values.txt") ;
TAB[i]=s.nextInt();
}
}
s.close();
}
You have too many extra brackets, well thats not the problem though, the problem is the scanner object s is declared inside the for loop where as you are closing it later outside the loop, since the scope of the variable is not outside the loop, hence the error.
The correct way should be
public static void readValues (int [] tab){
int i;
Scanner s = new Scanner ("Values.txt") ;
for(i=0; i<10; i++){
tab[i]=s.nextInt();
}
s.close();
}
Also there are many thing that will work in your code but is a bad practice or is not following conventions.
Variable names (e.g tab) should always be in camel case. It should only be a capital if it is a constant, which is not in your case.
The method names starts with small letter.
Also you are calling the two methods replaceOdd(TAB) and oddValues(TAB) But the return value is not being used anywhere.
FileNotFoundException will never be thrown
If you closely look at this method below
public static double oddValues(int[] TAB) {
int i;
double odd = 0;
int fn = 0;
for (i = 1; i < odd; i++) {
while (odd % 2 != 0) {
odd = fn;
}
break;
}
return fn;
}
The loop will never execute as odd is 0 so i<odd will always be false. Also the logic for odd is wrong.
public static int replaceOdd(int[] TAB){
int re=0;
for(int i=0; i<TAB.length; i++){
re = re/TAB.length;
}
return re;
}
This method will always return zero, the logic is wrong.
There are many more logical errors. I would suggest you to look into them as well
Hi I'm a complete newbie on programming and I try to search for a certain String in an array. When it's found the method should return the index but if the String is not found it should return -1.
public int poitionOfWord(String testWord) {
for (int i = 0; i < wordArray.length; i++) {
if (wordArray[i].equals(testWord)) {
return i;
}
}
return -1;
}
would this method return always -1 or would it actually terminate when finding a word and would return i.
Your method is correct and it will return the index in case it finds a match else if it doesn't find the match, it will come out of loop and return -1.
Just to make code crisp and concise, you can use something like this,
public static String[] wordArray = new String[]{"a", "b"};
public static int poitionOfWord(String testWord) {
return Arrays.asList(wordArray).indexOf(testWord);
}
Then test it with some code,
public static void main(String args[]) {
System.out.println(poitionOfWord("a"));
System.out.println(poitionOfWord("z"));
}
This prints,
1
-1
In general, when your function reaches a return statement, it will terminate and return the given value.
The following program is a recursive program to check for repeated entries in an array. The program compiles with no errors, however after i input the command-line arguments and hit enter, it doesn't proceed. The cursor just blinks! It doesn't return any Runtime errors too! If someone explains why this is happening, it'd be very helpful! Thanks! :)
import java.io.*;
class RepeatEntries_Recursive
{
static int i=0,flag=0;
public static void main(String[] args) throws IOException
{
int[] inp = new int[6];
for(int k=0;k<args.length;k++)
inp[k] = Integer.parseInt(args[k]);
boolean hasItRepeated = Repeating(inp,i);
if(hasItRepeated == true)
System.out.println("\nYes, there are entries that repeat in the array!");
else
System.out.println("\nNo, entries don't repeat in the array");
}
static boolean Repeating(int[] inp,int i)
{
for(int j=0;j<inp.length;j++)
{
if(inp[i] == inp[j])
flag = 1;
while(i<inp.length-1)
Repeating(inp,i+1);
}
if(flag==1)
return true;
else
return false;
}
}
while(i<inp.length-1)
Repeating(inp,i+1);
Your program can't ever escape from this loop.