0

So basically I want to ask for a time in both minutes and seconds. Instead of making the user input the answer in 2 separate lines, I was wondering if there was a way for it to stay on the same line and just put the time in with a colon in between.

This is what I have:

'Scanner input = new Scanner (System.in);
 System.out.print ("Enter Minutes: ");
 int min = input.nextInt();
 System.out.print ("Enter Seconds: ");
 int sec = input.nextInt()'

Then the output would look like this:

 Enter Minutes: 2
 Enter Seconds: 12

What i would like it to look like is this:

 Enter Time: 2:12

My guess I that there isn't a way to do this but if anyone has any suggestions I would appreciate it. Thanks.

Jake Annett
  • 1
  • 1
  • 1
  • The general issue is that the user enters a number and hits enter. Both are echo'd to the console by System.in. It seems to me that you could do: 1) Be ok with this, and set Scanner to look for `:` as a delimiter. 2) Allow input without echo feedback like in https://stackoverflow.com/questions/2893218/how-to-use-scanner-to-read-silently-from-stdin-in-java - and then parse. – jazeee Dec 01 '17 at 18:36

2 Answers2

1

First, you ask the user for the time.

System.out.print ("Enter Time: ");
String time = input.nextString();
String time = "2:12";

Then you split the time using a split function and use the colon as a divider.

String[] parts = time.split(":");

Doing this would result to two strings.

String time = parts[0]; // 2
String minutes = parts[1]; //12
John Valle
  • 23
  • 5
0

To do this, we could read in the user's input String by wrapping an InputStreamReader object in a BufferedReader object.

Then, we use the readLine() method of the BufferedReader to read the input String -- say, two integers separated by a space character. These can be parsed into two separate Strings using the String.split() method, and then their values may be assigned to a and b, using the parseInt method as you suggest above.

(We would likely want to verify the user input as well to ensure it is in the desired format, but that issue hasn't been addressed here.)

First, remember to import java.io.* for the Reader objects. The readLine() method throws an IOException, so the Main method can go like this:

public static void main(String[] args) throws IOException 
{ 
BufferedReader in = new BufferedReader( 
new InputStreamReader( 
System.in));

String[] input = new String[2]; 
int a; 
int b;

System.out.print("Please enter two integers: "); 
input = in.readLine().split(" ");

a = Integer.parseInt(input[0]); 
b = Integer.parseInt(input[1]);

System.out.println("You input: " + a + " and " + b); 
}

------------- ADDITIONAL INFO --------------

I forgot about the Scanner object. This can make things a lot simpler. Instead of importing the Reader objects, we import java.util.Scanner

The Scanner object can parse user input directly, so we don't have to split Strings or use parseInt. Also, we don't necessarily need to worry about catching an IOException. And, the user may input both integers on the same line, or even on different lines, as desired.

Using the Scanner object, our much simpler main function can go like this:

public static void main(String[] args) 
{ 
System.out.print("Please enter two integers: ");

Scanner sc = new Scanner(System.in);

int a = sc.nextInt(); 
int b = sc.nextInt();

System.out.println("You input: " + a + " and " + b); 
}
oxcakmak
  • 13
  • 7