2

Hello everyone I'm currently creating a simple program. I have 2 classes the first one is SampleReturn and the second one is GetValues. What I want to happen is that when I enter a name in GetValues class the name I entered will be stored in a variable and later on will be used by the SampleReturn class to display the name. Unfortunately, I can't run the program because it has an error. The code is below please help me with regards to this matter. I'm just self studying I really want to learn Java. Thanks! :)

Code in GetValues Class:

import java.util.Scanner;
public class GetValues{
    Scanner inp = new Scanner(System.in);

    public static void wew(){
        System.out.println("Enter name: ");
        String a = inp.nextLine();

        private static String z = a;

        public static String kuhaName(){
            return z;
        }
    }
}

Code in SampleReturn:

import java.util.Scanner;
    public class SampleReturn{
        public static void main(String[]args){

        String nameMo = GetValues.kuhaName();

        System.out.print("Your name is: " +nameMo);
    }
}
2
  • Check your code. You have not closed the wew() method body with curly brace } Commented Feb 22, 2013 at 17:08
  • it says illegal start of expression in the line private static String z = a; Commented Feb 22, 2013 at 17:09

3 Answers 3

6

Your Code should be something like this:

import java.util.Scanner;

public class GetValues
{
    private static Scanner inp = new Scanner(System.in);
    private static String z = "";
    public static void wew()
    {
        System.out.println("Enter name: ");
        String a = inp.nextLine();
        z = a;
    }
    public static String kuhaName()
    {
        return z;
    }
}

And then SampleRun.java should be like this:

//import java.util.Scanner;//no need to import
public class SampleReturn
{
    public static void main(String[] args)
    {
        GetValues.wew();//First input the name .
        String nameMo = GetValues.kuhaName();//Retrieve the name
        System.out.print("Your name is: " +nameMo);//Display the name
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

there is an error in line private static String z = a; it says cannot find symbol variable a
My mistake....missed that line. a is local variable in method wew() so value of a could not be assigned to z outside the method. See my update.
there is another error sir in the line String a = inp.nextLine(); it says non static variable inp cannot be referenced from a static context
Scanner object inp should be declared as static as non-static variables could not be called from static method ..
3

You have a few problems going on in this code.

First of all, you can't have a method within another method. Second of all, you're never calling wew which will actually read the input. Assuming you meant something like this:

public class GetValues{
    Scanner inp = new Scanner(System.in);
    private static String z;

    public static void wew(){
        System.out.println("Enter name: ");
        String a = inp.nextLine();

        z = a;
    }

    public static String kuhaName(){
        return z;
    }
}

All you have to do now is call your methods in order.

Comments

2

Well the first thing that comes to mind is that the wew method doesn't have an ending brace }

you also can't declare a field inside of your method. you probably want to declare private static String z outside of the wew method.


I've read your assignment, and althought I'm not going to entirely do your homework for you, I can give you advice on the architecture of your program.

First I'd make a class called Person. You can call your class whatever you want, but for the rest of this post, I'll refer to this class as the Person class.

now you'll want your class to have:

  • a field to store the name of your Person
  • a method to get input from the user, and to put that into the name field.
  • a method to print the content of the name field.

in your main method you'll want to

  • instantiate your Person
  • call it's input-getting method
  • call it's printing method.

10 Comments

how would I do that? I'm completely a newbie sir would you please give me the answer?
@AngelCasiMontoya well, what is your actual requirement? what is your program actually supposed to do?
Like what I said awhile ago, When the user input a name in GetValues class, that name will then be stored inside a variable and will be used by the class SampleReturn to display the name
@AngelCasiMontoya that's implementation details. You don't even need an extra class to read input from the command line. What is your real requirement?
What requirement? can you please specify sir?
|

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.