954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need Help Calling a Method

I'm a newbie to Java, so I'm sorry if this is a silly question. I'll post the code first, then explain my problem.

import java.util.Scanner;

class CharacterOperations{

    public void run(){
        System.out.println("1. toUpper");
        System.out.println("2. toLower");
        System.out.println("3. Alphabet");
        System.out.println("4. Quit");
        
        int input;
        Scanner sc = new Scanner(System.in);
        input = sc.nextInt();
        
        while (input == 1 || input == 2 || input == 3){
            input = sc.nextInt();
            if (input == 1){
                System.out.println(toUpper());
            }
        }
    }
    public void toUpper(char lowerCase){
        int decimal;
        decimal = (int) lowerCase - 32;
        if(decimal >= 65 && decimal <= 90){
            System.out.println( (char) decimal);
        }
        
    }
    public void toLower(char upperCase){
        int decimal;
        decimal = (int) upperCase + 32;
        if(decimal >= 97 && decimal <= 122){
            System.out.println( (char) decimal);
        }
    }
public void alphabet(char start, char end){
        int decimalStart = (int) start;
        int decimalEnd = (int) end;
        
        while(decimalStart != decimalEnd){
            
            if(( (int) start >= 65 && (int) start <= 90) && ( (int) end >= 97 && (int) end <= 122)){
                decimalStart = (int) start + 32;
                System.out.println( (char) decimalStart);
                start++;
            }
            else if(( (int) start >= 97 && (int) start <= 122) && ( (int) end >= 65 && (int) end <= 90)){
                decimalEnd = (int) end + 32;
                end = (char) decimalEnd;
                System.out.println( (char) decimalStart);
                start++;
            }
             else {
               decimalStart = (int) start;
                System.out.println( (char) start);
                start++;
            }
                
        }
        
    }
}


When the user inputs 1, 2, or 3 into the terminal window, I want the run method to call the toUpper, toLower, or alphabet method, respectively. The methods work properly when selected from an object (I'm using BlueJ) but I can't seem to properly call the methods within the run method. As the code stand now, I get the following error when I try to compile: "toUpper(char) in CharacterOperations cannot be applied to ()". I can't use the class Character from the Java library, nor can the class contain properties.
Can anyone tell me what I'm doing wrong?

Si_Long
Newbie Poster
5 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

The method toUpper has a parameter, when you call the method you are not sending it a parameter. Line 18 is where you call the toUpper method.
This is how is should look.

toUpper('a');

The reason why I took the System.out.println out of the code is because the method is void, which means it doesn't return anything that can be printed out. And because there is a print statement in the method.

Mattox
Junior Poster in Training
57 posts since Jan 2011
Reputation Points: 10
Solved Threads: 15
 

Thanks, that fixed the error!

Si_Long
Newbie Poster
5 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

No problem, Do you need to read in a char from the user?

Mattox
Junior Poster in Training
57 posts since Jan 2011
Reputation Points: 10
Solved Threads: 15
 

Yes, actually. I didn't realize that calling toUpper doesn't seem to do anything until I've called the alphabet method, and toLower doesn't seem to do anything when called through the run method.

Si_Long
Newbie Poster
5 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

You can use the scanner to read in a char,

import java.util.*;

public class readInChar
{
    public static void main(String[] args)
	{
		Scanner scan = new Scanner(System.in);
		String s;
		char u; 
		System.out.println("Enter you char: ");
		s = scan.next();
		u = s.charAt(0);
		System.out.println(u);
	}
}



scan.next() returns a String, that's why I used the charAt(0) method. The 0 in this method refers to the first element in the String.
Include reading in the userChar in the while loop and send that a parameter in the method calls and you should be good to go.

Mattox
Junior Poster in Training
57 posts since Jan 2011
Reputation Points: 10
Solved Threads: 15
 

That works for the first two methods, but I'm having trouble applying to the alphabet method.

else if(input == 3){
                String str;
                str = sc.next();
                char letter1 = str.charAt(0);
                String str2 = sc.next();
                char letter2 = str.charAt(0);
                alphabet(letter1, letter2);
                input = sc.nextInt();
                
            }


That yields a vertical blinking line that just keeps accepting user inputs.

Si_Long
Newbie Poster
5 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Thanks! I managed to figure out the rest. :)

Si_Long
Newbie Poster
5 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Have a good one.

Mattox
Junior Poster in Training
57 posts since Jan 2011
Reputation Points: 10
Solved Threads: 15
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: