Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Help string not storing in Variable

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help string not storing in Variable

    hi i'm new to java programming and i have a problem with using the scanner, here is an extract of the code , i need help with storing a value into a, it is initialized with null first but when i enter a string from the terminal and print out the output it is still null. what is wrong with the code? as you can see from the code i have not used the scanner but i have modified the code to 'a=Input.next();' but still no luck

     
     public String processInput(String theInput) {
     
    		String theOutput = null;
    		String a=null;
    		Scanner Input = new Scanner(System.in);
     
       if (state == WAIT) {
    		       theOutput="type enter"
    		   if (theInput.equalsIgnoreCase("enter")) {
    			theOutput =question1[Question];
    	                theInput=a;
                            theOutput = "" a "";
    	                                   }
     
    			  else {
                theOutput = "Wrong password " ;
                       }
    		                               }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help string not storing in Variable

    print out the output it is still null.
    Where do you print out the value of a? I don't see any print statement in the posted code.

    Where does the code assign any values to a?

    Is there some other code you are talking about? The posted code doesn't show the problem you are having.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help string not storing in Variable

    this is a protocol file of a server theInput is what is entered into the terminal, and theOutput is what is sent back from the server to the client so i am not getting the value of 'a' sent back to me im still getting null

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help string not storing in Variable

    Can you post the code that shows the error? What you posted doesn't show the error.

    Where is the variable a assigned a value after it is assigned the value of null?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help string not storing in Variable

    The program has no errors it compiles and runs but its not storing what i enter into 'a' and stays as null
    -' theOutput =question1[Question];' server sends the question and waits for input from client(which is the input i want to store in a)
    - i then type input, for example '1'
    - 'theInput=a;' theInput is equal to 1 and stored in a (this is where i think the problem is maybe wrong line of code)
    -'theOutput = "" a "";' server sends back what i put into theInput and stored in a to the terminal but i still get null instead of 1

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help string not storing in Variable

    its not storing what i enter into 'a'
    There is no statement in the posted code that stores anything in a after a is assigned null.

    Do you know how an assignment statement works? The variable receiving the value is to the left of the =

    variableToReceiveValue = theValueToBeAssigned;
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help string not storing in Variable

    yes,sorry about that because i kept playing around with that line of code 'theInput=a;' it was previously 'a=theInput'. i changed the line to various other code such as 'a=Input.next();', 'a=Input.next(theInput);', it compiled and run but the connection between the server and client froze.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help string not storing in Variable

    Please post the current version of the program and explain what it is doing and what you want it to do.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Having trouble storing variable from JTextField
    By sibs in forum What's Wrong With My Code?
    Replies: 32
    Last Post: December 1st, 2012, 08:39 AM
  2. Compiler refuses to "accept" my string variable - need help
    By khelben1979 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 27th, 2012, 10:33 AM
  3. Reading XML content and storing in variable
    By learn_java in forum Java SE APIs
    Replies: 1
    Last Post: November 16th, 2012, 05:23 AM
  4. advice needed on storing string into arraylist and printing it out.
    By jong2009 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 23rd, 2012, 10:16 AM
  5. Saving string value of an iterator Object into a variable.
    By apocalipsis1982 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: December 28th, 2011, 01:12 PM