1

I tried to make a "shortcut" to the print, println, and the scanner. So I created a different class, and that's what it had in it:

import java.util.*;
public class ShortCutClass {
    Scanner input = new Scanner(System.in);
    public void p (String text){
        System.out.print (text);
    }
    public void pl (String text){
        System.out.println (text);
    }
    public String is (){
        String a= input.nextLine();
        return a;
    }
    public double id (){
        double b= input.nextDouble();
        return b;
    }
    public int ii (){
        int c= input.nextInt();
        return c;
    }
}

So, I tried to check if it works- using this:

import java.util.*;
class ShortcutTest{
    Scanner input = new Scanner(System.in);
    public static void main(String args[]){
        ShortCutClass scc = new ShortCutClass();
        scc.pl("So ummmm... How ya doin?");
        scc.p("Connected1 ");
        scc.p("Connected2 ");
        String a=scc.is;
        double b=scc.id;
        int c=scc.ii;
        scc.pl (a);
        scc.pl (b);
        scc.pl (c);        
    }
}

and it gives me the following Error: "cannot find symbol- variable is

What am I doing wrong? How can I fix this?

*** Edit:

I fixed it, and ran into another problem... I can not print out b or c because the print I wrote requires a String, and b and c can only be double and int, so that won't work. is there any way of changing the print (scc.p and scc.pr) so it will be able to receive any kind of parameter?

7
  • () is missing. double b=scc.id; should be double b=scc.id();. Commented Nov 27, 2012 at 18:51
  • is, id and ii are methods, not variables. Commented Nov 27, 2012 at 18:51
  • 1
    These comments should really be answers. A good comment would be something like this: "Why would you ever want to make your code this hard to read?" Commented Nov 27, 2012 at 18:53
  • What is the purpose of this code? Commented Nov 27, 2012 at 18:59
  • @Roman C - The OP says, "I just started learning Java" Commented Nov 27, 2012 at 19:10

3 Answers 3

3

Here:

String a=scc.is;
double b=scc.id;
int c=scc.ii;

... you're trying to refer to methods as if they were variables. You need to call the methods:

String a=scc.is();
double b=scc.id();
int c=scc.ii();

As a side-note, I would strongly discourage you from doing all of this to start with. Even if you want a convenience class, please give the methods sensible names - writeInt, readInt etc. Using abbreviations like this makes the code very hard to read.

Sign up to request clarification or add additional context in comments.

6 Comments

Actually it's 7377 more than that. ;)
Thank you for your answer! the thing is, i ran into another problem... please read the the Extra part i just wrote there... thanks in ahead!
@user1857509: Well it sounds like you want overloads for print to take double and float parameters...
ummm.... New to Java... Overloads? whats that and how do I do that? Thanks in ahead!
@user1857509: Did you try searching before asking? If you're new to Java, you should be learning from a book or well-structured tutorial - did you see what that had to say about overoading?
|
1

Thats because "is" is a method and you are attempting to access it as a public variable.

Try:

String a = scc.is();

Comments

0

In line

String a=scc.is;
double b=scc.id;

the () are missing:

correct woul dbe:

String a=scc.is();
double b=scc.id();

Further Tipp: Please remove the short cuts, that code is less readable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.