Java: Variable is already defined in method main

So I have been the following error every time I try to compile my .java file

"error: variable max is already defined in method main(String[]) int max = j; "

And I haven't been able to figure out what is the issue or how to fix it. Been stuck at it for about 2 hours now. What I'm trying to ultimately do is enter one integer into my array and sorting the digits in that integer from least to greatest to provide context.

Here is the relevant part from my code:

int[] wholeNumber = new int[1]; //Sorting algorithm beginning int n = wholeNumber.length; System.out.println("Length of array is :" + n); //Array length displayed for(int i = 0; i < 1; i++) { System.out.println("Hello!"); int max = i; for(int j = i+1; j < 1; j++) { if (wholeNumber[j] > wholeNumber[max]) { int max = j; } } if (max != i) { wholeNumber[i] = wholeNumber[max]; wholeNumber[max] = wholeNumber[i]; } } //Sorting algorithm end

-------------Problems Reply------------

You only need to declare int max once at the highest scope you require it. Remove the int keyword from inside of your if statement; this will turn it into an assignment statement instead of a declaration statement.

As an aside, this statement is pointless:

if (max != i) {
wholeNumber[i] = wholeNumber[max];
wholeNumber[max] = wholeNumber[i];
}

wholeNumber[i] will be set to whatever is in wholeNumber[max], which you then attempt to set right back into wholeNumber[max]. You were probably going for a swap, which would require (in this case) a third variable:

if (max != i) {
int swp = wholeNumber[i];
wholeNumber[i] = wholeNumber[max];
wholeNumber[max] = swp;
}

As the error says, you have int max declared twice. You need to change the second one to a variable assignment, not declaration:

int max = i;

for(int j = i+1; j < 1; j++)
{
if (wholeNumber[j] > wholeNumber[max])
{
max = j; // NOT: int max = j;
}
}

Category:java Views:1 Time:2015-02-05

Related post

  • in Java, how do I define different methods for an interface array? Using hash tables without java library 2012-03-14

    I have a programming problem in Java: Can I define a different method for any different interface array element? myInterface[] op = new myInterface[4]; Now "myInterface" only has 1 method: public static int doSomething(int a, int b);. I need to defin

  • In UML, do you include variables defined in methods? 2015-03-13

    I know the basic format of UML and I know you typically include the variables defined for the class. My question is, do you include variables that are defined within methods of the class? In my program I have two classes, BabyName and NameFinder. Bab

  • Using underscores in Java variables and method names 2008-09-29

    Even nowadays I often see underscores in Java variables and methods, an example are member variables (like "m_count" or "_count"). As far as I remember, to use underscores in these cases is called bad style by Sun. The only place they should be used

  • How to define a method that can take arbitrary arguments in java? 2010-03-21

    How to define a method that can take arbitrary arguments in java? Is there a demo? --------------Solutions------------- varargs were introduced in Java 5. For instance: public String join(String... parts); This is actually a shortcut for: public Stri

  • Define singleton methods on initialization using instance variables 2010-11-16

    I'm trying to optimize some code and I want to instead on checking a value on every method call just define the method to respond with the checking already pre-calculate, because this checking doesn't change on the whole live of the instance. I decid

  • How would I use generics in Java to create a generic interface that defines a method that only accepts the implementation as a parameter? 2011-01-30

    Is there a way to define a method in an interface where implementations can be typed to the implementing class? Here's a concrete example of my initial attempt to solve it, but I'll point out where it fails. public interface ErrorMeasurable<T exte

  • define class methods and class variables in R5 reference class 2011-04-22

    I want to know the correct way to define the class methods and class variable in R5 reference class. Here is an example: > # define R5 class XX > # member variable: ma > # member method: mfa > XX <- setRefClass("XX", + fields = list(ma

  • Defining a method that uses an out-of-scope variable in Ruby 2011-05-09

    I want to make a Test::Unit test_helper method that I can call to wipe a bunch of tables after the tests execute. Here's the general idea I have: def self.wipe_models(*models) def teardown models.each do |model| model = model.to_s.camelize.constantiz

  • Can you define class variables from within a class method in PHP? 2011-07-10

    I want to pull all info about a file from a files table, but that table's structure might change. So, I'd like to pull all the field names from the table and use them to generate the class variables that contain the information, then store the select

  • Java reflection API and invoking a method with variable number of arguments 2011-10-30

    I have a problem trying to solve a situation, when I use Java Reflection API to invoke a method that accepts a variable number of parameters. Every time I try to do that I get an "NoSuchMethodException". My declaration of method to invoke: public voi

  • Best way to define private methods for a class in Objective-C 2008-10-05

    I just started programming Objective-C and, having a background in Java, wonder how people writing Objective-C programs deal with private methods. I understand there may be several conventions and habits and think about this question as an aggregator

  • javascript - accessing private member variables from prototype-defined functions 2009-01-12

    Is there any way to make "private" variables (those defined in the constructor), available to prototype-defined methods? TestClass = function(){ var privateField = "hello"; this.nonProtoHello = function(){alert(privateField)}; }; TestClass.prototype.

  • How to access variables from class using static methods? 2009-08-18

    //my class function myClass() { this.pubVar = 'hello public'; var priVar = 'hello private'; } myClass.staticMethod = function() { //how do i access thos variables here } I tried alert(this.pubVar) and alert(priVar) with no success. What am I doing wr

  • Possible to prevent Java's pass by value in methods? 2010-07-01

    Java is a 'pass by value' language, meaning that sending in a variable into a method, pointing the variable to a new object, does not effect the outer variable. public void one() { String s = "one"; two(s); System.out.println(s); } public void two( S

  • What is the scope of a java variable in a block? 2010-10-05

    I know in c++ variables have block scope, for example, the following code works in C++ void foo(){ int a = 0; for(int i = 0; i < 10; ++i){ int a = 1; //re-define a here. } } but this snippet doesnt work in java, it reports "duplicate local variabl

  • In Java, can I use a single method to act as a timer for other methods? 2011-04-08

    When testing runtime, I use System.nanotime() in the following way: startTime = System.nanotime(); // some statements System.out.println("Runtime: " + (System.nanoTime() - startTime)); Is there a way to reuse this test model for other blocks of code

  • Defining class methods in PHP 2008-09-16

    Is it possible in PHP (as it is in C++) to declare a class method OUTSIDE the class definition? --------------Solutions------------- No, as of PHP 5.2. However, you may use __call magic method to forward call to arbitraty function or method. class A

  • defined? method in Ruby and Rails 2008-10-26

    I have a quite old templating system written on top of ERB. It relies on ERB templates stored in database. Those are read and rendered. When I want to pass data from one template to another I use the :locals parameter to Rails render method. For sett

  • Java: In what order are the methods called in an Applet? 2008-12-22

    Of all these methods what's being run and in what order?? I guess the first question to ask is whats being run first? And why does th.start() start run()? import java.applet.*; import java.awt.*; import javax.swing.JFrame; public class BallApplet ext

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.153 (s). 11 q(s)