Ok, I'm trying to make a simple sink a boat game in java, but am running into a java.lang.numberformatexception and don't know what I'm doing wrong... Here's the actual code I'm using. Sorry very noob still.
It's a simple 7 tiles battlefield with a ship on 3 tiles.
import java.util.Scanner;
public class New4
{
int randnum;
static char[] field;
int numofhits;
String userinput;
private static Scanner userguess;
public static void main(String[] args)
{
new New4();
}
public New4()
{
String input = "";
int numofhits = 0;
int randnum = (int) (Math.random() * 5);
char[] field = new char[7];
for(int i=0;i<7;i++)
{
field[i] = '*';
}
for(int j=0;j<3;j++)
{
field[randnum] = 'O';
randnum++;
}
for(char c : field)
{
System.out.print("|" + c);
}
System.out.print("|");
while(numofhits < 3)
{
AskUser();
input = userinput;
if(field[Integer.parseInt(input)] == 'O'){System.out.println("A terrifc hit");numofhits++;}
else{System.out.println("A blasted miss");}
System.out.println("Great game");}
}
public String AskUser()
{
String userinput;
System.out.println("Guess a number: ");
userguess = new Scanner(System.in);
userinput = userguess.next();
return userinput;
}
}
input = askUser(); //instead of userinput = input;
Related
// I'm looking for any errors or mistakes I made in the code or another way to do it with if-then statements or usage of array. (very new to java)
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a string");
String letter = input.nextLine();
int n = letter.length();
int numberOfLetters = 0;
for (int i = 0; i < letter.length(); i++) {
numberOfLetters++;
}
if(letter.charAt(0) != letter.charAt(n-1)){
String letter2 = letter.toLowerCase();
if (letter2.charAt(0) == letter2.charAt(n - 1)) {
System.out.println("This is a palindrome " + letter);
}
else {
System.out.println("This is not a palindrome " + letter);
}
}
}
}
I have to get 4 user input from the user one by one on the next line like
Sample input:
65
66
67
68
Then the output has to displayed like
You have entered:
65-A
66-B
67-C
68-D
the program i have return is this:
import java.util.Scanner;
public class ASCII {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the digits:");
int no = sc.nextInt();
char ch= (char) no;
System.out.println(no + "-" + ch);
}
}
the one thing could not get is the 4 input for the user could someone help with that
You should loop it;
int[] numbers = new int[4];
for (int i = 0; i < 4; i++) {
numbers[i] = sc.nextInt();
}
numbers[n-1] will return number in your case 0 < n < 5;
and you can create another loop to print them.
chars[] characters = {'A','B','C','D'};
for (int i = 0; i < 4; i++) {
System.out.println(Integer.toString(numbers[i]) + characters[i]);
}
for loops works like;
for (DoAtStart; Condition; DoAtEndOfARepeat) {
}
This would work for you :
public class ASCII {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the digits:");
int array[]=new int[4];
for(int i=0; i<4;i++) {
int no = sc.nextInt();
array[i]=no;
}
System.out.println("You have entered:");
for(int j=0;j<array.length;j++) {
char ch= (char) array[j];
System.out.println(ch+"-"+array[j]);
}
}
}
hi guys I'm trying to make a lottery program And I'm trying to get the users input to after asking if they want to retry playing. But my program ends before reaching the while loop.
public class Main {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String yn = "";
Lottery.getTicket();
Lottery.generateWinningNumbers();
System.out.print("\nWould you like to try again? ");
while(input.hasNextLine())
{
yn = input.nextLine();
if(yn.equalsIgnoreCase("y"))
{
Lottery.getTicket();
Lottery.generateWinningNumbers();
}
else
{
System.out.println("Done");
}
}
input.close();
}
}
In my Lottery class:
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
import java.util.Scanner;
public class Lottery {
public static Set<Integer> generateWinningNumbers()
{
Random rndNumbers = new Random();
TreeSet<Integer> winningNumbers = new TreeSet<Integer>();
int max = 40;
int min = 1;
int range;
int sixNum;
for(int i = 0; i < 6; i++)
{
range = max - min + 1;
sixNum = rndNumbers.nextInt(range) + min;
while(winningNumbers.contains(sixNum))
{
sixNum = rndNumbers.nextInt(range) + min;
}
winningNumbers.add(sixNum);
}
System.out.print("Winning Numbers: " + winningNumbers);
return winningNumbers;
}
public static Set<Integer> getTicket()
{
int userInput;
TreeSet<Integer> getNumbers = new TreeSet<Integer>();
Scanner input = new Scanner(System.in);
System.out.println("Enter your 6 numbers between 1-40: ");
for (int i = 0; i<6 ; i++)
{
System.out.print(i+1 + ": ");
userInput = input.nextInt();
while( userInput <1 || userInput > 40 || getNumbers.contains(userInput))
{
if (getNumbers.contains(userInput))
{
System.out.println("Number already picked");
userInput = input.nextInt();
}
if(userInput < 1 || userInput > 40)
{
System.out.println("Invalid. Pick a number between 1-40");
userInput = input.nextInt();
}
}
getNumbers.add(userInput);
}
input.close();
System.out.println("Your ticket was: " + getNumbers);
return getNumbers;
}
}//end of Lottery class
You are using the Scanner Object
Scanner input = new Scanner(System.in);
in Lottery.getTicket and you do
input.close();
This means that System.in will be closed for the rest of the program
Try passing the Scanner object from main to other classes and method that need it.
I read an int numer from the user.
I should calculate all numbers which have mod1 until numer is reached.
The program should write them one by one.
public class meraba {
public static void main(String[] args) {
int number;
#SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.println("Sayı girin");
number = input.nextInt();
for (int i = 0; i < 0; i++) {
if (number == i) {
for(int d = 0;d<number;d++){
}
break;
}
}
}
}
I couldn't get it to do that.
I'm looking forward to a correct solution
in java.
public class meraba {
public static void main(String[] args) {
int number;
#SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.println("Sayı girin");
number = input.nextInt();
for (int i = 2; i < number; i++) {
if (number % i == 1) {
System.out.println("Mod 1 condition satisfies: " + i);
}
}
}
}
I hope, I understood your question very well ;)
Here is my code:
public class Exercixe09_04 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a string: ");
String s1 = input.next();
System.out.println("Enter a character: ");
String s2 = input.next();
char c = s2.charAt(0);
int num = count(s1, c);
System.out.print(num);
}
public static int count(String str, char a)
{
int count = 0;
for(int i =0; i < str.length(); i++)
if(str.charAt(i) == 'a'){
count++;
}
return count;
}
}
When I compile and run it, nothing prints. Can someone please help me figure out what is wrong?
import java.util.Scanner;
public class Exercixe09_04 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
String s1 = input.next();
System.out.print("Enter a character: ");
String s2 = input.next();
char c = s2.charAt(0);
int num = count(s1, c);
System.out.println(num);
}
public static int count(String str, char a) {
int count = 0;
for (int i = 0; i < str.length(); i++)
if (str.charAt(i) == a) {
count++;
}
return count;
}
}
Sample run:
falsetru#jmlee12:/tmp$ java Exercixe09_04
Enter a string: Hello
Enter a character: l
2