Help for 'Enter to Exit'

Help for 'Enter to Exit'

Page 1 of 1

12 Replies - 3656 Views - Last Post: 07 August 2009 - 11:27 PM Rate Topic: -----

#1 rainxrain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-August 09

Help for 'Enter to Exit'

Posted 07 August 2009 - 10:09 PM

HI! I would like to seek some help for 'Enter to EXIT', below is part of my coding that requires enter to exit.

This is the result that I have when I pressed 'Enter' :

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:819)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextInt(Scanner.java:2040)
at java.util.Scanner.nextInt(Scanner.java:2000)
at Income.select1(Income.java:138)
at Income.main(Income.java:40)

Process completed.

 
   			 System.out.println("\nPlease select only 1-12!\n");
   			 
   			 for (int i=0; i<8; i++)
   				{	
   					System.out.print("Enter person "+(i+1) +" <Press ENTER to exit>:");
				 		name[i]=input.next();
				 			
				 		if (name[i]=="") {
				 		 	System.exit(0);
				 		}
				 		
						System.out.print("Enter salary : $");
				 		salary[i] = input.nextInt();
						System.out.println();
						
				 	
   			 
   			}//end for 


Is This A Good Question/Topic? 0
  • +

Replies To: Help for 'Enter to Exit'

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Help for 'Enter to Exit'

Posted 07 August 2009 - 10:13 PM

You can't compare String values using the == operator. Use .equals()

if (name[i].equals(""))
    System.exit(0);


Hope this helps! :)

This post has been edited by Locke: 07 August 2009 - 10:13 PM

Was This Post Helpful? 0
  • +
  • -

#3 rainxrain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-August 09

Re: Help for 'Enter to Exit'

Posted 07 August 2009 - 10:27 PM

View PostLocke, on 7 Aug, 2009 - 09:13 PM, said:

You can't compare String values using the == operator. Use .equals()

if (name[i].equals(""))
    System.exit(0);


Hope this helps! :)



for (int i=0; i<8; i++)
   				{	
   					System.out.print("Enter person "+(i+1) +" <Press ENTER to exit>:");
				 		name[i]=input.next();
				 			
				 		if (name[i].equals(""))
				 		 	System.exit(0);
				 	
				 		
						System.out.print("Enter salary : $");
				 		salary[i] = input.nextInt();
						System.out.println();
						
				 	
   			 
   			}//end for




*********** Family Income ***********
1) Enter monthly salary
2) Display detailed slary by month
3) Quick glance at monthly salary
4) Exit
Please select your choice <1-4> : 1
*************************************
Enter month <1 for Jan - 12 for Dec>: 1
-------------------------------------
Jan salary <max 8 persons>
Enter person 1 <Press ENTER to exit>:1
Enter salary : $1

Enter person 2 <Press ENTER to exit>:
Enter salary : $
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:819)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextInt(Scanner.java:2040)
at java.util.Scanner.nextInt(Scanner.java:2000)
at Income.select1(Income.java:140)
at Income.main(Income.java:40)

Process completed.



HI! Thanks for you help. I tried using the method that you taught me, but I still get the same result =(
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Help for 'Enter to Exit'

Posted 07 August 2009 - 10:34 PM

Which statement is line number 140?
Was This Post Helpful? 0
  • +
  • -

#5 rainxrain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-August 09

Re: Help for 'Enter to Exit'

Posted 07 August 2009 - 10:38 PM

View PostLocke, on 7 Aug, 2009 - 09:34 PM, said:

Which statement is line number 140?


 salary[i] = input.nextInt(); 


This is my full code.

import java.text.*;//To import the java.text package
import java.util.*;//To import the java.util package

public class Income {
	
	static Scanner input=new Scanner(System.in).useDelimiter("\r\n");	//To create an input object
	static DecimalFormat fmt=new DecimalFormat("0.00");// To create an input object

static String[] name =new String[8]; // name

static String[] month= new String [10]; //month

static int[] salary =new int[8]; // salary

static int[] amt = new int [10]; //monthly salary 

static int choice; // menu choice

static int n1, n2, n3, s=0; //n1=enter salary, n2=details, n3=quick glance, s=array index


	public static void main (String[] args)
{												


	do
	{												
 
	  System.out.println("*********** Family Income ***********");
	  System.out.println("1) Enter monthly salary");
	  System.out.println("2) Display detailed slary by month");
	  System.out.println("3) Quick glance at monthly salary");
	  System.out.println("4) Exit");
	  System.out.print("Please select your choice <1-4> : "); 
	  
	  choice=input.nextInt();
	
	  switch(choice) {
		  
		  case 1: select1();
				break;
	//	case 2: select2();
	//			break;
	//	case 3: select3();
	//			break;
	//	case 4: select4();
	//			break;
	
		default: System.out.println("Please enter only 1, 2 or 3 only!\n");
	  
	  } // end switch (choice)
	} while(choice!= 4); // end do-while
} //end main


	  static void select1()
	  {
   				System.out.println("*************************************");
   				System.out.print("\tEnter month <1 for Jan - 12 for Dec>: ");
   				n1=input.nextInt();
   			//	boolean cont=true;
   				//while(cont=true){	
   				//	if(name[s]==null){
   				//			cont=false;
   				//			break;
   				//	}
   						if (n1==1){
   						System.out.println("\t-------------------------------------");
   						System.out.println("Jan salary <max 8 persons>");
   						}
   
   						else if (n1==2){
   						System.out.println("\t-------------------------------------");
   						System.out.println("Feb salary <max 8 persons>");
   						}
   				
   						else if (n1==3){
   						System.out.println("\t-------------------------------------");
   						System.out.println("Mar salary <max 8 persons>");
   						}
   						
   						else if (n1==4){
   						System.out.println("\t-------------------------------------");
   						System.out.println("Apr salary <max 8 persons>");
   						}
   						
   						else if (n1==5){
   						System.out.println("\t-------------------------------------");
   						System.out.println("May salary <max 8 persons>");
   						}
   						
   						else if (n1==6){
   						System.out.println("\t-------------------------------------");
   						System.out.println("Jun salary <max 8 persons>");
   						}
   						
   						else if (n1==7){
   						System.out.println("\t-------------------------------------");
   						System.out.println("Jul salary <max 8 persons>");
   						}
   						
   						else if (n1==8){
   						System.out.println("\t-------------------------------------");
   						System.out.println("Aug salary <max 8 persons>");
   						}
   						
   						else if (n1==9){
   						System.out.println("\t-------------------------------------");
   						System.out.println("Sept salary <max 8 persons>");
   						}
   						
   						else if (n1==10){
   						System.out.println("\t-------------------------------------");
   						System.out.println("Oct salary <max 8 persons>");
   						}
   						
   						else if (n1==11){
   						System.out.println("\t-------------------------------------");
   						System.out.println("Nov salary <max 8 persons>");
   						}
   						
   						else if (n1==12){
   						System.out.println("\t-------------------------------------");
   						System.out.println("Dec salary <max 8 persons>");
   						}
   			
   		else
   			 System.out.println("\nPlease select only 1-12!\n");
   			 
   			 for (int i=0; i<8; i++)
   				{	
   					System.out.print("Enter person "+(i+1) +" <Press ENTER to exit>:");
				 		name[i]=input.next();
				 			
				 		if (name[i].equals(""))
				 		 	System.exit(0);
				 	
				 		
						System.out.print("Enter salary : $");
				 		salary[i] = input.nextInt();
						System.out.println();
						
				 	
   			 
   			}//end for
   				}// end of select1
	  }//end of class

Was This Post Helpful? 0
  • +
  • -

#6 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Help for 'Enter to Exit'

Posted 07 August 2009 - 10:38 PM

From what I can see there is a type mismatch in your input, it seems that you are entering a double value(decimal) while your scanner is being directed to get an integer... Something like that, make sure you keep your data types the same as defined and as being inputted, hope this helps, good luck :)
Was This Post Helpful? 0
  • +
  • -

#7 rainxrain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-August 09

Re: Help for 'Enter to Exit'

Posted 07 August 2009 - 10:42 PM

View Postayman_mastermind, on 7 Aug, 2009 - 09:38 PM, said:

From what I can see there is a type mismatch in your input, it seems that you are entering a double value(decimal) while your scanner is being directed to get an integer... Something like that, make sure you keep your data types the same as defined and as being inputted, hope this helps, good luck :)



Hi! Thanks for the reply.
I tried going through my codes, but there isn't any double value.

((:
Was This Post Helpful? 0
  • +
  • -

#8 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Help for 'Enter to Exit'

Posted 07 August 2009 - 10:53 PM

alright got it, remove .useDelimiter("\r\n") from your line:
static Scanner input=new Scanner(System.in).useDelimiter("\r\n");
And everything should work fine, you don't really need the delimiter for this program anyways. Good luck :)
Was This Post Helpful? 0
  • +
  • -

#9 rainxrain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-August 09

Re: Help for 'Enter to Exit'

Posted 07 August 2009 - 11:01 PM

View Postayman_mastermind, on 7 Aug, 2009 - 09:53 PM, said:

alright got it, remove .useDelimiter("\r\n") from your line:
static Scanner input=new Scanner(System.in).useDelimiter("\r\n");
And everything should work fine, you don't really need the delimiter for this program anyways. Good luck :)



Hi! As followed, I removed .useDelimiter("\r\n")

But I still see the following error:

Enter person 2 <Press ENTER to exit>:
Enter salary : $Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:819)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextDouble(Scanner.java:2335)
at Income.select1(Income.java:133)
at Income.main(Income.java:39)

Process completed.

=((
Was This Post Helpful? 0
  • +
  • -

#10 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Help for 'Enter to Exit'

Posted 07 August 2009 - 11:10 PM

I compiled your code and ran it and everything works fine for me, are sure you re compiled your code after making the changes?
Was This Post Helpful? 0
  • +
  • -

#11 rainxrain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-August 09

Re: Help for 'Enter to Exit'

Posted 07 August 2009 - 11:13 PM

View Postayman_mastermind, on 7 Aug, 2009 - 10:10 PM, said:

I compiled your code and ran it and everything works fine for me, are sure you re compiled your code after making the changes?


Really! Its just removing .useDelimiter("\r\n") rights?

By the way are you using JCreator?

Sighss... Dont know whats wrong with my program. =((
Was This Post Helpful? 0
  • +
  • -

#12 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Help for 'Enter to Exit'

Posted 07 August 2009 - 11:23 PM

Quote

Really! Its just removing .useDelimiter("\r\n") rights?

By the way are you using JCreator?

Sighss... Dont know whats wrong with my program. =((


Yes I am using JCreator


Are you sure you are not entering a decimal for the salary? When I entered a decimal I got the same error as you are getting. :)

This post has been edited by ayman_mastermind: 07 August 2009 - 11:24 PM

Was This Post Helpful? 0
  • +
  • -

#13 rainxrain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-August 09

Re: Help for 'Enter to Exit'

Posted 07 August 2009 - 11:27 PM

View Postayman_mastermind, on 7 Aug, 2009 - 10:23 PM, said:

Quote

Really! Its just removing .useDelimiter("\r\n") rights?

By the way are you using JCreator?

Sighss... Dont know whats wrong with my program. =((


Yes I am using JCreator


Are you sure you are not entering a decimal for the salary? When I entered a decimal I got the same error as you are getting. :)



--------------------Configuration: <Default>--------------------
*********** Family Income ***********
1) Enter monthly salary
2) Display detailed slary by month
3) Quick glance at monthly salary
4) Exit
Please select your choice <1-4> : 1
*************************************
Enter month <1 for Jan - 12 for Dec>: 2
-------------------------------------
Feb salary <max 8 persons>
Enter person 1 <Press ENTER to exit>:abc
Enter salary : $800

Enter person 2 <Press ENTER to exit>:
Enter salary : $Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:819)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextDouble(Scanner.java:2335)
at Income.select1(Income.java:135)
at Income.main(Income.java:39)

Process completed.

Nope, I didnt enter decimal :blink:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1