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.
Printable View
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.
I apologize,
Like I called readData with userInput = readData(userInput), I, sort of, need to call printData to run.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);
}
}
How would I do that?
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?Code:userInput = readData(userInput);
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!
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?
/facepalm; Sorry again!
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
Parameters do not match.Code:printData(userInput);
public static void printData(String line, String userInput)
Also, confused. Your comments say that you read the inputs from a file but your code gets user input from the command line.