4 Replies - 383 Views - Last Post: 03 August 2013 - 12:15 PM Rate Topic: -----

#1 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 12
  • View blog
  • Posts: 592
  • Joined: 10-March 13

Unknown amount of variable to enter for user

Posted 01 August 2013 - 07:59 PM

I was just wondering if there was a way to make a code that stop asking the user to input their answer by entering a "finish" button. For example, for the following the user is to customize their drink by entering X amount of variables. Is there a way for them to let the program know that they are done instead of pressing ENTER constantly?


System.out.println("What would you like in your custom caramel frap? Enter each item followed by pressing ENTER: ");
		
		String a1 = input.nextLine();
		String a2 = input.nextLine();
		String a3 = input.nextLine();
		String a4 = input.nextLine();
		String a5 = input.nextLine();
		String a6 = input.nextLine();
		String a7 = input.nextLine();
		
		// some kind of finishing key that the user can enter to let the program know they are done



Is This A Good Question/Topic? 0
  • +

Replies To: Unknown amount of variable to enter for user

#2 nandureddy  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 129
  • Joined: 31-January 11

Re: Unknown amount of variable to enter for user

Posted 01 August 2013 - 08:20 PM

Add something like this:
System.out.println("Please type exit to exit the program");
String exit = input.nextLine();
if(exit.equals("exit"))
{
exit();
}


This post has been edited by nandureddy: 01 August 2013 - 08:23 PM

Was This Post Helpful? 1
  • +
  • -

#3 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: Unknown amount of variable to enter for user

Posted 01 August 2013 - 08:26 PM

Yes, there is, you could just move the code into a function and test each input and if it happens to equal whatever you choose as a finshed button, say "finished", then it will return from the function. But this isn't very good. You'll probably want to return the number of items reads as well. Try learning about arrays and looping. It will make it a lot easier.
Was This Post Helpful? 0
  • +
  • -

#4 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: Unknown amount of variable to enter for user

Posted 01 August 2013 - 09:02 PM

This is a clever way of doing it. However, arrays would still be best.
System.out.println("What would you like in your custom caramel frap? Enter 		

		each item followed by pressing ENTER, Enter finshed to end");

		
                String a1, a2, a3, a4, a5, a6, a7, end;
                a1 = a2 = a3 = a4 = a5 = a6 = a7 = end = "finished";
		a1 = input.nextLine();
                if(!a1.equals(end))
		      a2 = input.nextLine();		
                if(!a2.equals(end))
                      a3 = input.nextLine();
                if(!a3.equals(end))
		      a4 = input.nextLine();
                if(!a4.equals(end))
		      a5 = input.nextLine();
                if(!a5.equals(end))
		      a6 = input.nextLine();
                if(!a6.equals(end))
		      a7 = input.nextLine();
		
		System.out.println(a1 + " " + a2 + " " + a3 + " " + a4 + " " + a5 + " " + a6 + " " 

				   + a7);


This post has been edited by salazar: 01 August 2013 - 09:02 PM

Was This Post Helpful? 2
  • +
  • -

#5 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 12
  • View blog
  • Posts: 592
  • Joined: 10-March 13

Re: Unknown amount of variable to enter for user

Posted 03 August 2013 - 12:15 PM

View Postnandureddy, on 01 August 2013 - 08:20 PM, said:

Add something like this:
System.out.println("Please type exit to exit the program");
String exit = input.nextLine();
if(exit.equals("exit"))
{
exit();
}


Thank you!

View Postsalazar, on 01 August 2013 - 09:02 PM, said:

This is a clever way of doing it. However, arrays would still be best.
System.out.println("What would you like in your custom caramel frap? Enter 		

		each item followed by pressing ENTER, Enter finshed to end");

		
                String a1, a2, a3, a4, a5, a6, a7, end;
                a1 = a2 = a3 = a4 = a5 = a6 = a7 = end = "finished";
		a1 = input.nextLine();
                if(!a1.equals(end))
		      a2 = input.nextLine();		
                if(!a2.equals(end))
                      a3 = input.nextLine();
                if(!a3.equals(end))
		      a4 = input.nextLine();
                if(!a4.equals(end))
		      a5 = input.nextLine();
                if(!a5.equals(end))
		      a6 = input.nextLine();
                if(!a6.equals(end))
		      a7 = input.nextLine();
		
		System.out.println(a1 + " " + a2 + " " + a3 + " " + a4 + " " + a5 + " " + a6 + " " 

				   + a7);



Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1