Results 1 to 9 of 9
  1. #1
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default Calling a method in the same class

    I am able to do this with userInput = readData(userInput), but I don't know how to just do something like "call printData();) or something. Basically call it to run out of thin air.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Teclis View Post
    I am able to do this with userInput = readData(userInput), but I don't know how to just do something like "call printData();) or something. Basically call it to run out of thin air.
    When writing your questions here, it's always best to assume that we can't read your mind, that we don't know what your code looks like and that we haven't the foggiest clue as to the details of your problem if you don't tell them.

  3. #3
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    I apologize,


    Java Code:
    import java.util.Scanner;
    public class Lab10
    {
       public static void main(String[] args)
       {
          // Declare any need variables here
          int i;
          Scanner scan = new Scanner(System.in);
          String userInput, userOutput;
    
    
          // Write a loop that will loop through as many inputs as are in the file
          while(scan.hasNext())
          {
             // Call the readData method here.
             // Store the result into userInput.
             userInput = readData(userInput);
    
    
    
             // Call the printData method here.
    
    
    
          } // End of file loop
       } // End of main method
    
    
       public static String readData(String userInput)
       {
          Scanner scan = new Scanner(System.in);
          // Read in the users input.
          // Remember to use all defensive programming techniques.
          // We are reading an entire line of input.
          userInput = scan.nextLine();
          if(userInput.length() <= 1)
          {
             System.out.println("String not long enough. \n Quitting program");
             System.exit(0);
          }
    
          // Return the String that was read in.
          return userInput;
    
       } // End of readData method
    
       public static void printData(String userInput, String userOutput)
       {
          // Print out the user input with appropriate label.
          System.out.println("Input - " + userInput);
    
          // Create the reverse of the user's input.
          // Hint: You might want to review Homework 9 problem 2
          for (int i=0; i<userInput.length(); i++)
          {
             userOutput = userInput.substring(i, i+1) + userOutput;
          }
          // Print out the user input in reverse with appropriate label.
          System.out.println("Reverse - " + userOutput);
    }
    }
    Like I called readData with userInput = readData(userInput), I, sort of, need to call printData to run.

    How would I do that?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    userInput = readData(userInput);
    For starters, why do you pass userInput to the method? The whole point of the method is to get the user to enter something. If you do not have anything what is the point of passing nothing to the method. Same goes for the printData method. The point of the method is to generate the userOutput, so why are you passing it to the method?

    If you want to call the printData method then just call it. Since the method has a void return type you cannot assign it to anything. Think Nike: Just do it!
    Java Code:
    userInput = readData();
    printData(userInput);

  5. #5
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Java Code:
    userInput = readData(userInput);
    For starters, why do you pass userInput to the method? The whole point of the method is to get the user to enter something. If you do not have anything what is the point of passing nothing to the method. Same goes for the printData method. The point of the method is to generate the userOutput, so why are you passing it to the method?

    If you want to call the printData method then just call it. Since the method has a void return type you cannot assign it to anything. Think Nike: Just do it!
    Java Code:
    userInput = readData();
    printData(userInput);
    I honestly didn't exactly know what I was doing there. However the "printData(userInput)" isnt exactly sending what was read in by readData to printData. Im still tinkering but, not exactly sure. Is there something Im not doing in readData that will read it into a variable and send it back into the main method?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by Teclis View Post
    Is there something Im not doing in readData that will read it into a variable and send it back into the main method?
    We don't read minds. How are we supposed to answer that without seeing the latest version of your code?

  7. #7
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    We don't read minds. How are we supposed to answer that without seeing the latest version of your code?
    /facepalm; Sorry again!

    Java Code:
    import java.util.*;
    public class Lab10
    {
    
       public static void main(String[] args)
       {
          // Declare any need variables here
          int i;
          Scanner scan = new Scanner(System.in);
          String userInput;
    
    
          // Write a loop that will loop through as many inputs as are in the file
          while(scan.hasNext())
          {
             // Call the readData method here.
             // Store the result into userInput.
             userInput = readData();
    
    
    
             // Call the printData method here.
             printData(userInput);
    
    
          } // End of file loop
       } // End of main method
    
    
       public static String readData(Scanner scann)
       {
          Scanner scan = new Scanner(System.in);
          // Read in the users input.
          // Remember to use all defensive programming techniques.
          // We are reading an entire line of input.
          userInput = scan.nextLine();
          if(userInput.length() <= 1)
          {
             System.out.println("String not long enough. \n Quitting program");
             System.exit(0);
          }
    
          // Return the String that was read in.
          return userInput;
    
       } // End of readData method
       public static void printData(String line, String userInput)
       {
    
          // Print out the user input with appropriate label.
          System.out.println("Input - " + userInput);
    
          // Create the reverse of the user's input.
          // Hint: You might want to review Homework 9 problem 2
          for (int i=0; i<userInput.length(); i++)
          {
             line = userInput.substring(i, i+1) + line;
          }
          // Print out the user input in reverse with appropriate label.
          System.out.println("Reverse - " + line);
          return;
       } // End of printData method
    
    } // End of class Lab10

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    printData(userInput);
    public static void printData(String line, String userInput)
    Parameters do not match.

    Also, confused. Your comments say that you read the inputs from a file but your code gets user input from the command line.

  9. #9
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Java Code:
    printData(userInput);
    public static void printData(String line, String userInput)
    Parameters do not match.

    Also, confused. Your comments say that you read the inputs from a file but your code gets user input from the command line.
    Im supposed to be using UNIX for this, with doing a command of "java Lab10 < Lab10.data".

    Im thinking about scrapping what I've got for most of the program and restarting, keeping only the reversing code.

Similar Threads

  1. Calling a method in another class
    By uncopywritable in forum New To Java
    Replies: 9
    Last Post: 10-22-2012, 04:01 PM
  2. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  3. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  4. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 AM
  5. Calling method from another class
    By asahli in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 06:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •