Create for loop in Java to take in information - arrays

I want to use a for loop to ask the user for a movie title, genre and rating and get for it to iterate three times and store in an array and display the information back to the user.
In the for loop in the code, I am not sure what to insert as "Movie 1" and how to get it to change to "Movie 2" and "Movie 3" after each iteration.
This is what I have so far :
import java.util.Scanner;
public class NewClass {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Creates a Movie array of size three.
Movie[] movies = new Movie[3];
for (int i=0; i < movies.length; i++) {
// Initialises the 3 movie objects in the array.
movies[i] = new Movie();
}
// Loop will execute 3 times.
for (int x = 0; x < 3; x += 1){
System.out.println("Please enter the title of Movie 1: ");
System.out.println("Please enter the genre of Movie 1: ");
System.out.println("Please enter the rating of Movie 1: ");
}
}

Please post your Movie class to help us suggest a more detailed answer.
you can try something like this ...
Scanner sc=new Scanner(system.in);
for (int x = 0; x < 3; x++){
System.out.println("Please enter the title of Movie 1: ");
movies[i].title=sc.next();
System.out.println("Please enter the genre of Movie 1: ");
movies[i].genre=sc.next();
System.out.println("Please enter the rating of Movie 1: ");
movies[i].genre=sc.nextInt();
}
Use x++ instead of x+=1 , try using enhanced for loop in Java , and import java.util.* for the Scanner to work

You did most of the stuff right, just that you need to figure out what to do when as explained in comments below:
Scanner input = new Scanner(System.in);
// Creates a Movie array of size three.
//here you are declaring your array of movies of size 3
Movie[] movies = new Movie[3];
String name;
//lets ask user to enter movie name three times and simultaneously populate movie object and store them in an array.
for (int i=0; i < movies.length; i++) {
System.out.println("Please enter the title of Movie " + (i+1));
name = input.nextLine();
movies[i] = new Movie(name);
}
// Loop will execute 3 times and just print out the movie names that we populated.
for (int x = 0; x < movies.length; x += 1){
System.out.println("movie " + (x+1) + " is " + movies[x].name);
}

Related

How can I check if a user input of 2D Array contains a given user input integer value? If found, how can I print its index?

For example, a given user input array is {{1,2,3},{4,5,6}} and the user input integer value is 6. If 6 is in the array, I want to print its index (for example [1][2]). I was able to write the code that will scan the given elements of the array however, I'm not sure what to do next in order to find the given integer value in the array.
Here's my code:
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
int toFind;
System.out.print("Enter the number of rows: ");
int row =scan.nextInt();
System.out.print("Enter the number of columns: ");
int column =scan.nextInt();
int input[][] = new int[row][column];
System.out.println("Elements: ");
for(int ctr1 = 0; ctr1 < input.length; ctr1++)
{
for(int ctr2 = 0; ctr2 < input[ctr1].length; ctr2++)
{
input[ctr1][ctr2] = scan.nextInt();
}
}
System.out.println("Enter the number you want to find: ");
toFind = scan.nextInt();
I'm not sure what to next here in order to initiate the checking of the number in the given array and print its index.

It should continue to accept a price of an item and displays the current total while the user input is not equals to zero

/* I need some help. I'm stuck with this code. It should continue to accept a price of an item and displays the current total while the user input is not equal to zero. However, output for the current total won't show the current amount. */
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String customerName;
char promtSenior;
int num = 1;
double itemPrice;
double currentTotal = 0;
ArrayList<Double> amounts = new ArrayList<Double>();
System.out.print("Please enter customer name: ");
customerName = scanner.nextLine();
while (true) {
System.out.print("Please enter item " + num++ + " price or 0 to quit: ");
itemPrice = scanner.nextDouble();
amounts.add(itemPrice);
scanner.nextLine();
for (double i : amounts) {
currentTotal = currentTotal + i ;
}
System.out.println(currentTotal);
if (itemPrice == 0) {
System.out.println("Are you a Senior Citizen? [Y/N]: " + customerName);
promtSenior = scanner.next().charAt(0);
if (promtSenior == 'N') {
System.out.println("No Discount");
}
}
}
}
}
while (true) {
System.out.print("Please enter item " + num++ + " price or 0 to quit: ");
itemPrice = scanner.nextDouble();
amounts.add(itemPrice);
scanner.nextLine();
for (double i : amounts) {
currentTotal = currentTotal + i ;
System.out.println(currentTotal);
}
should be this as previously you where printing the total after the for loop not during
i think you accidentally messed up your curly brackets and indented before

This loop to modify a arraylist of numbers based on arraylist of strings isnt working

So, in my program you input employee information and the salary you input depends on what you input for designation. If temporary, yo input how much they make per hour, if permanent you input what they make per year. At the end, you have the option to sort by salary, so if they choose that then I must multiply the temporary's per-hour by a set number (1920) in order to convert it to what they make per year. I tried this but I got the messages
" The type of the expression must be an array type but it resolved to java.util.ArrayList"
and "The type of the expression must be an array type but it resolved to java.util.ArrayList", I got that second one twice.
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String Continue = "y";
int Count = 0;
int SortingChoice;
ArrayList<String> Names = new ArrayList<String>();
ArrayList<String> Department = new ArrayList<String>();
ArrayList<String> Designation = new ArrayList<String>();
ArrayList<Float> Salary = new ArrayList<Float>();
//******************************************************//
do
{
System.out.println("Enter Employee Name: ");
String x = in.next();
Names.add(x);
System.out.println("Enter Employee Designation ('temporary or permanent'): ");
String y = in.next();
Designation.add(y);
System.out.println("Enter Employee Department: ");
String z = in.next();
Department.add(z);
System.out.println("Enter Employee Salary: ");
float i = in.nextFloat();
Salary.add(i);
System.out.println("Do you wish to add another employee? ('y'/'n'): ");
Continue = in.next();
Count = Count + 1;
}
while(Continue.equals("y"));
//***********************************************************************//
System.out.println("Enter sorting Criterion Number: 1. Name, 2.Department, 3. Salary. ");
SortingChoice = in.nextInt();
if(SortingChoice == 1)
{
Collections.sort(Names);
for(int i=0; i<Names.size(); i++)
{
System.out.println(Names.get(i));
}
}
if(SortingChoice == 2)
{
Collections.sort(Department);
for(int i=0; i<Department.size(); i++)
{
System.out.println(Department.get(i));
}
}
if(SortingChoice == 3)
{
for(int k=0; k<Salary.size(); k++)
{
if(Designation[k].equals("temporary"))
{
Salary[k] = Salary[k]*1920;
}
}
Collections.sort(Salary);
for(int i=0; i<Salary.size(); i++)
{
System.out.println(Salary.get(i));
}
}
}
What I am most concered about if the loop under if(SortingChoice==3)

Else-If / While Loop stuck

I'm trying to make a program that takes input of a minimum and a maximum number and then generates a random number in that range. Then the user guesses a number and if it's too high, it outputs a messege.. If it's too low, it outputs a message. The part that i'm stuck at is when you guess the number correctly, the user inputs "Y" or "N" to run the program again.
My code is as follows:
import java.util.Scanner;
import java.util.Random;
public class GuessingGame_V2
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print('\u000C');
int min;
int max;
int userGuess;
int numberGuesses = 1;
System.out.println("Enter the minimum number: ");
min = in.nextInt();
System.out.println("Enter the maximum number: ");
max = in.nextInt();
Random r = new Random();
int randomNumber = r.nextInt(max - min + 1) + min;
System.out.println("Enter your guess: ");
userGuess = in.nextInt();
String guessAgain = ("Y");
while(!guessAgain.equalsIgnoreCase("N"))
{
if( userGuess > randomNumber ){
System.out.println("Your guess was to high! Guess again!");
System.out.println("Input your new guess: ");
userGuess = in.nextInt();
numberGuesses++;
}
else if (userGuess < randomNumber ){
System.out.println("Your guess was to low! Guess again!");
System.out.println("Input your new guess: ");
userGuess = in.nextInt();
numberGuesses++;
}
else
{
System.out.println("Congratulations, you guessed the number!");
System.out.println("It took " + numberGuesses + " tries");
System.out.println("Guess another number? (Y/N)");
guessAgain = in.next();
}
}
System.out.println("Thank's for playing!");
}
}
The problem comes when the user hits "Y" to restart the program. It doesn't restart and just displays the final message again. (print statement, number of guesses, and Y/N). I need the program to restart when the user types "Y"
I'm new to posting on the site.. so forgive me if I messed up putting in the code -
Thanks in advance for the help -
*Changes - 11/13/13 10:58 Changed the code to take more than one input and to
keep taking input until the user gets it right.
You need nested loops because you are doing two repetitive actions--first is repeating the game and second is prompting for guesses within one game.
So use nested loops. One loop prompts the user if he or she wants to play and prompts the user again with the same question at the end. The nested loop should be as you have it.
Your code should be along the lines of
while (stillPlaying) {
while (stillGuessing) {
}
}
Hope that helps.

Replacing already printed numbers in C

I am writing a program in C language which is an India Game called Thambola(similar to Bingo).
In this game the user gets a 3x9 ticket and the computer asks the user to enter the number which the second program(this picks numbers randomly from 1-90) picked. If the entered number exists in the ticket, the number should be changed to 'x' meaning the number has been stricken off. I need help here. How to replace an already printed number with 'x'? i read this C - Remove and replace printed items but it doest help because i have 27 numbers to be changed.Please help me. Here is the part of the code:
int number,i,j;
const char x='x';
printf("\nEnter the number:");
scanf("%d",&number); // number entered by the user from the Picker
for (i=0;i<3;i++)
for (j=0;j<9;j++)
{if (ticket[i][j]==number)
ticket[i][j]=x;
printf("%d",ticket[i][j]); //if the input number is present in the ticket, this should change it to 'x
}
getchar();
}
Main problem of your code snippet is your logic behind assigning values to variables.
As anyone can see, you have an array of integers but you want to assign a char to one of its elements. Actually you can do it by casting, but the result will be the ASCII value of the 'x'.
{if (ticket[i][j]==number) // number is an integer
ticket[i][j]=x; // x is a char
Since 'x' has the ASCII value of 120,which is out of range from the possible numbers in the ticket, you can safely cast 'x' to its integer value and then assign it to its array element. In printing, if you see 120 print 'x'.
In the situations where your char's integer value is in the range of the other possible integer values, pick another integer value and treat this value as 'x' in logic flow (for example, pick 0 for the corresponding integer, if 0 comes print 'x').
You can use ncurses or simply print a ticket state then clear the screen just before printing modified ticket for output.
public class sample1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<int[]> outer = new ArrayList<int[]>(9);
List<int[]> inner = new ArrayList<int[]>(9);
int[][] final_arr = new int[9][18];
int[][][] final_arr2 = new int[6][3][9];
int[][] multi = new int[][]{
{2,3,5,7,9,12,13,15,17},
{1,3,4,5,6,7,11,14,16,18},
{1,2,4,6,8,9,12,13,17,18},
{3,5,7,9,10,11,12,14,15,16},
{1,4,6,7,8,10,11,13,14,18},
{2,3,5,8,9,12,13,15,16,17},
{1,2,4,6,7,10,11,13,17,18},
{1,3,5,8,9,12,13,15,16,18},
{2,4,6,7,9,10,11,14,15,16,17}
};
for( int i=0;i<9;i++){
outer.add(multi[i]);
}
for(int x=0;x<9;x++){
int [] temp = new int[18];
for(int k=0;k<outer.get(x).length;k++){
//System.out.print(outer.get(x)[k]-1 +" ");
int row = outer.get(x)[k]-1;
temp[row]=1;
}
//System.out.println();
inner.add(temp);
}
System.out.println();
int count=1;
for(int i=0;i<9;i++){
List<Integer> temp = new ArrayList<Integer>();
for(int k=0;k<outer.get(i).length;k++){
temp.add(count);
count++;
}
Collections.shuffle(temp);
int index_of_temp=0;
for(int j=0;j<18;j++){
if(inner.get(i)[j]==1){
inner.get(i)[j]=temp.get(index_of_temp);
index_of_temp++;
}
//System.out.print(inner.get(i)[j]+ " ");
}
//System.out.println();
}
for(int i=0;i<9;i++){
for(int j=0;j<18;j++){
final_arr[i][j]=inner.get(i)[j];
}
}
System.out.println();
for(int i=0;i<18;i++){
for(int j=0;j<9;j++){
//System.out.print(final_arr[j][i]+ " ");
final_arr2[i/3][i%3][j]=final_arr[j][i];
}
}
for(int k=0;k<6;k++){
for(int i=0;i<3;i++){
for(int j=0;j<9;j++){
System.out.print(final_arr2[k][i][j]+ " ");
}
System.out.println();
}
System.out.println();
}
}
}

Resources