0
import java.io.*;
 import java.util.*;
 import java.io.IOException;
 import java.util.Scanner;
 
 public class Lab
{
 public static void main(String[]args)
 {
	  
  		System.out.println("MAIN MENU");
		System.out.println("<1> Bahasa Melayu");
		System.out.println("<2> English");
		System.out.println("<3> Exit");
		System.out.println("Your selection (1,2 or 3)?");

  Scanner read = new Scanner (System.in);

  int selection = read.nextInt();
  
  if(selection == 1)
  { System.out.println("Hello, apa khabar?");
  }
  else if(selection == 2)
  { System.out.println("Hai, how are you?");
  }
  else if(selection == 3)
  { System.out.println("System exit...");
    System.exit(0);
  }
  else
  { System.out.println("Invalid selection");
    System.out.println("null");
  }

    System.exit(0);
 }
}

i wan to fix the problem is if the user key in alpha like abcdefg, will show string "null".. how to solve it.. thx.. help me..

5
Contributors
6
Replies
9
Views
8 Years
Discussion Span
Last Post by ~s.o.s~
0

You need to call the read.nextLine() which returns a String, and use Strings to do the checks in the if

Try searching the API for the Scanner because I don't quite remember the method

0

but the problem is my 'selection is integer', so how i fix it to string... if i fix it to string.. then the 1,2,3 selection will corrupted..

0

but the problem is my 'selection is integer', so how i fix it to string... if i fix it to string.. then the 1,2,3 selection will corrupted..

No your selection is not an integer. 1 is not an integer
this is an integer: 1
this is a String: "1"

String input = read.nextLine();

if (input.equals("1")) {

} else {
....
}

And if you want to turn into an integer:

String input = read.nextLine();
int i = 0;
try {
   i = Integer.parseInt(input);
} catch(NumberFormatException nfe) {

}
0

You need to check if an integer can be read from the Scanner before actually reading it; the same applies to any kind of token, always. A proper mix of hasNext() and hasNextInt() provides a relatively better and appropriate way of implementing the given functionality. A sample snippet is as posted:

import java.util.*;

public class ScannerTest {

  public static void main(final String[] args) {
    new ScannerTest().startTest();
  }

  public void startTest() {
    Scanner in = new Scanner(System.in);
    int i = 0;
    while(true) {
      System.out.println("\n\n1. Say hello");
      System.out.println("2. Quit");
      System.out.print("Enter your choice: ");
      System.out.flush();
      if(in.hasNext()) {
        if(in.hasNextInt()) {
          // the next token can be safely converted to an int
          i = in.nextInt();
          if(i == 1) {
            System.out.println("\n*** Hey there! ***");
          } else if(i == 2) {
            System.out.println("\nThank you for wasting your time.");
            System.exit(0);
          } else {
            System.out.println("Invalid choice.");
          }
        } else {
          System.out.println("Enter a valid number [1 or 2].");
          in.next();  // not a valid integer, skip this token
        }
      }
    }
  }

}

You might consider using a BufferedReader with a custom string processing method to have greater/absolute control over your parsing logic or if you feel that the Scanner API is dragging you down.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.