Methods to modify an array - Java help

Joined
Mar 13, 2012
Messages
2
Reaction score
0
Hey, all! I recently started a Java class (my first) and have run into a problem with an assignment on arrays. We're supposed to get user input for an array and then write methods to modify the array. I'm having trouble figuring out how to make the method actually change the array and am getting all sorts of errors with return type. Any help would be appreciated.

Code:
import java.util.*;


public class Unit04_A2
{
  
  
  
    //PART 1
    
    public static void main(String[] args)
    
    {
    
    if (args.length == 0)
    {
        System.out.println("You must enter at least one String at the command line.");
    }  
    
      
    
    if(args.length <=10)
    {  for (int i = 0; i < args.length; i++)
            {
                System.out.println("[" + (i) + 
                "] : " + args[i] + " is "+ args[i].length() + " characters long.");
            }
    }
        
        
        if (args.length >10)
        {
            System.out.println("You have entered " + args.length + " Strings. The last " + (args.length - 10) + " will not be processed.");
            
            for (int i = 0; i < 10; i++)
            {
                System.out.println("[" + (i) + 
                "] : " + args[i] + " is "+ args[i].length() + " characters long.");
            }
        }
        
     
    System.out.println("");
 
    
    
  }
  
  //PART 2
 
public String reverseIt(String [])
  
  {

   
    
  if(args.length <= 10)
  {
 
    for(int i = 0; i < args.length/2; i++)
        {
            String temp = args[i];
            args[i] = args[args.length-1-i];
            args[args.length - 1 -i] = temp;    
        }
  }
    
  if (args.length > 10) 
  {
    for(int i = 0; i < 10/2; i++)
        {
            String temp = args[i];
            args[i] = args[10-1-i];
            args[10 - 1 -i] = temp; 
        }
  }   
 
  return args;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top