Arrays and Output in Java - java

So I am trying to write a program that automates making an Operations Order (or OPORD). It is pretty straight forward, but I am having some issues with my arrays, more specifically how to get them to display properly in the output.
Here is my code:
import java.util.*;
import java.util.Scanner;
public class opord {
public static void main(String[] args){
//Variables
int opord_type, phase_a = 0, n = 1, tasks = 0, phase_b = 0, phase_c = 0;
//Strings for paragraph 1
String area_interest = " ", area_ops = " ", enemy_forces = " ", weather = " ", terrain = " ", friendly_forces = " ", civil_consid = " ", attach_detach = " ";
//Strings for paragraph 2
String who = " ", what = " ", where = " ", when = " ", why = " ";
//Strings for paragraph 3
String commander_intent = " ", phases;
Scanner keyboard = new Scanner(System.in);
//Array Lists
ArrayList alist = new ArrayList();
ArrayList blist = new ArrayList();
ArrayList clist = new ArrayList();
//Page One
System.out.println("Welcome to Automated OPORD");
System.out.println("Please choose which type of OPORD you want: (1:Garrison, 2:Tactical)");
opord_type = keyboard.nextInt();
if(opord_type == 1){
//Page Two
System.out.println("Paragraph One: ");
System.out.println("Situation: ");
//Indent One
System.out.println("Enter area of interest: ");
area_interest = keyboard.next();
System.out.println("Enter area of operations: ");
area_ops = keyboard.next();
//Indent Two
System.out.println("Enter weather: ");
weather = keyboard.next();
System.out.println("Enter terrain: ");
terrain = keyboard.next();
//End Indent Two
System.out.println("Enter enemy forces: ");
enemy_forces = keyboard.next();
System.out.println("Enter friendly forces: ");
friendly_forces = keyboard.next();
System.out.println("Enter civil considerations: ");
civil_consid = keyboard.next();
System.out.println("Enter attachments and detachments: ");
attach_detach = keyboard.next();
//End Indent One
//Page Three
System.out.println("Paragraph Two: ");
System.out.println("Mission");
//Indent One
System.out.println("Enter who: ");
who = keyboard.next();
System.out.println("Enter what: ");
what = keyboard.next();
System.out.println("Enter where: ");
where = keyboard.next();
System.out.println("Enter when: ");
when = keyboard.next();
System.out.println("Enter why: ");
why = keyboard.next();
//End Intent One
//Page Four
System.out.println("Paragraph Three: ");
System.out.println("Execution: ");
//Indent One
System.out.println("Enter commander's intent: ");
commander_intent = keyboard.next();
System.out.println("Concept of Operations");
//Indent Two
System.out.println("Enter number of phases: ");
phase_a = keyboard.nextInt();
for (int ph=0; ph<phase_a; ph++) {
System.out.println ("Enter phase " + (ph+1));
alist.add (keyboard.next());
}//End Indent Two
System.out.println("Scheme of Movement and Maneuver");
//Indent Three
System.out.println("Enter number of phases: ");
phase_b = keyboard.nextInt();
for (int p=0; p<phase_b; p++) {
System.out.println ("Enter phase " + (p+1));
blist.add (keyboard.next());
}//End Indent Three
System.out.println("Task to Subordinate Units");
//Indent Four
System.out.println("Enter number of tasks: ");
tasks = keyboard.nextInt();
for (int h=0; h<phase_b; h++) {
System.out.println ("Enter task " + (h+1));
clist.add (keyboard.next());
}
}else if(opord_type == 2){
}
//Output for Garrison
System.out.println("Output for Garrison");
for (int ph=0; ph<phase_a; ph++){
System.out.println("Phase " + n++ + ": " + alist.get(ph));
}
for (int p=0; p<phase_b; p++){
System.out.println("Phase " + n++ + ": " + blist.get(p));
}
for (int h=0; h<phase_c; h++){
System.out.println("Phase " + n++ + ": " + clist.get(h));
}
//Output for Tactical
}
}
I need the output of the phases and the tasks to look like this:
Concept of Operations:
Phase One: Here is some text that the user input
Phase Two: Here is some text that the user input
Phase Three: Here is some text that the user input
Phase (whatever the number the user input): Here is some text that the user input
Scheme of Movement and Maneuver:
Phase One: Here is some text that the user input
Phase Two: Here is some text that the user input
Phase Three: Here is some text that the user input
Phase (whatever the number the user input): Here is some text that the user input
Task to Subordinate Units:
Task One: Here is some text that the user input
Task Two: Here is some text that the user input
Task Three: Here is some text that the user input
Task (whatever the number the user input): Here is some text that the user input
Tactical is mostly mirrored with some changes so don't worry about that, I just need to fix this code so that. I just have to get this code finished, any help would be wonderful!
Thank you!

If I'm understanding you correctly, this is a question about how to create line breaks in the console output of a Java application.
The Character for this is "\n". Put that into print command when you want to do a line break.
//Page Three
System.out.println("\nParagraph Two: ");
System.out.println("Mission");
etc...
Further more, looking at your expected output (I don't know if this is important) but if you want to user input to be next to the text strings, rather than below, use System.out.print (without the ln at the end)
.println will do a line break after finishing the regular print operation

Related

Why System.out.println can't print out a string value (in my particular case)? [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 7 years ago.
My program should ask users to enter some value and evetually those value should be printed out depending on the menu option choosen (in my case it's 3). I have used System.out.println("Your name: " + name); to print out the name inserted by user, but unfortunately it can't print out the name. The line is just left empty. Why so? How can I fix it.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int userChoose;
String name = null;
int accNum = 0;
double initiateAmount = 0;
double newAmm = 0;
double depOrWith = 0;
System.out.println("WELCOME TO OUR BANK!\n\n"
+ "...................\n"
+ "...................\n\n"
+ "Choose your optiin:\n"
+ "1. Create new account\n"
+ "2. Deposit/withdraw\n"
+ "3. View details\n"
+ "4. Deleting an account\n"//not used yet
+ "5. View all the accounts\n"//not used yet
+ "6. Quit\n\n");
System.out.println("*************\n"
+ "************");
while (true) {
userChoose = sc.nextInt();
if (userChoose == 1) {
System.out.println("Enter your full name:");
name = sc.nextLine();
sc.nextLine();
System.out.println("Choose an account number:");
accNum = sc.nextInt();
System.out.println("Enter an initiating amount:");
initiateAmount = sc.nextDouble();
System.out.println("\n-----------\n"
+ "------------");
} else if (userChoose == 2) {
System.out.println("Enter negative value to withdraw and positive to deposit");
depOrWith = sc.nextInt();
if (depOrWith < 0) {
initiateAmount = initiateAmount + depOrWith;
} else {
initiateAmount = initiateAmount + depOrWith;
}
System.out.println("\n-----------\n"
+ "------------");
} else if (userChoose == 3) {
System.out.println("Your name: " + name);
System.out.println("Your account number: " + accNum);
System.out.println("Your current balance: " + initiateAmount);
System.out.println("\n-----------\n"
+ "------------");
} else if (userChoose == 6) {
System.exit(0);
}
}
}
After selecting the option and pressing Enter, the Scanner is not reading the newline. Afterwards when name = sc.nextLine(); is called it will only read the new line following the selected option and name will be assigned the empty string. To solve this, simply add a call nextLine after reading the selected option, and remove the duplicate nextLine when reading the name:
while (true) {
userChoose = sc.nextInt();
sc.nextLine();
if (userChoose == 1) {
System.out.println("Enter your full name:");
name = sc.nextLine();
System.out.println("Choose an account number:");
...
Interchange the Blank nextLine() and the assigning one.
System.out.println("Enter your full name:");
sc.nextLine();
name = sc.nextLine();
System.out.println("Choose an account number:");
accNum = sc.nextInt();
name is null. If the user selects three you haven't actually assigned a value to the name variable yet, that only happens if they choose 1
When you select the option 3 you're not setting the name value which defaults to null, so it's printing Your name: null
You need to read and assign name value if you want something to be printed.

Why is this program not running correctly?

I have the following code:
import java.util.Scanner;
public class Practice {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//System.out.println("Enter quantity:");
//int quantity = input.nextInt();
//System.out.println("You entered: " + quantity);
//System.out.println("Enter price: ");
//double price = input.nextDouble();
//System.out.println("You entered: " + price);
System.out.println("Enter city: ");
String city = input.nextLine();
System.out.println("You entered: " + city);
System.out.println("Enter state code: ");
String state = input.next();
System.out.println("You entered: " + state);
}
}
When I run the program with the middle section commented out like this, it works correctly. But when I uncomment it, it messes up the last block by printing the following lines simultaneously:
Enter city:
You entered:
Enter state code:
Why is this happening, and how can I fix it?
You typed something like this:
12<enter>1.3<enter>AZ
right?
When you call nextInt, it reads the next integer. So it reads "12" and what is left is:
<enter>1.3<enter>AZ<enter>
Now you call nextDouble. It skips past the first <enter> and reads "1.3" (a double). What is left is:
<enter>AZ<enter>
Now you call nextLine, which reads until the next <enter>. Oh look, you already pressed <enter>! So it reads the <enter> and returns a blank line. What is left is:
AZ<enter>
Now you call nextLine again, which reads until the next <enter>. It reads AZ<enter> and returns "AZ".
This is a quirk of how Scanners and streams work. The usual fix is to call nextLine immediately after nextInt and nextDouble, and ignore the result. Something like:
System.out.println("Enter quantity: ");
int quantity = input.nextInt();
input.nextLine(); // ignore newline
System.out.println("You entered: " + quantity);
System.out.println("Enter price: ");
double price = input.nextDouble();
input.nextLine(); // ignore newline
System.out.println("You entered: " + price);
input.nextDouble();
does not consume the line, insert a line:input.nextLine();
right after the commented block, don't assign it to any variable.
use ScnObj.next() instead of ScnObj.nextLine();
System.out.println("Enter price: ");
double price = ScnObj.nextDouble();
System.out.println("You entered: " + price);
System.out.println("Enter city: ");
String city = ScnObj.next();
System.out.println("You entered: " + city);
System.out.println("Enter state code: ");
String state = ScnObj.next();
System.out.println("You entered: " + state);

Listing multiples of user-inputted numbers

The task is to "Write a program that displays a user-indicated number of multiples for an integer entered by the user."
I suppose I do not need a completely direct answer (although I do want to know the methods/formula to use), as I want to use this as a learning experience in order to do and learn from the task myself. I really want to know about the process and which methods to use, along with finding a formula. :||
I'm really not sure how to write a code that displays a user-inputted number of a user-inputted integer. The hardest part seems to be writing the loop formula. Not sure where to start.
So far, I have:
import java.util.Scanner;
public class MultipleLooping
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
\\just stuff to base my code off of
int integer;
int numberMultiples;
System.out.println("Enter an integer: ");
integer = keyboard.nextInt();
System.out.println("How many multiples of " + integer + " would you like to know?");
numberMultiples = keyboard.nextInt();
System.out.println("Listing the first " + numberMultiples + " multiples of " + integer + ": ");
\\pretty much everything from here on out.. I'm not sure what to really do.
int n = integer;
int result = (integer * (numberMultiples));
while (result > 0){}
System.out.print(result);
}
} \\at the moment this code doesn't seem to have any running errors
I'm really not sure how to write a code that displays a user-inputted number of a user-inputted integer. The hardest part seems to be writing the loop formula. Not sure where to start.
NEW QUESTION
I need to loop my program as well. (By asking a question to the user first.) Mines isn't working, as it just keeps looping only the integer loop and doesn't let me type yes/no.
import java.util.Scanner;
public class MultipleLoops
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int integer, numberMultiples;
String repeat = "yes";
while (repeat != "no")
{
System.out.println("Enter an integer: ");
integer = keyboard.nextInt();
System.out.println("How many multiples of " + integer + " would you like to know?");
numberMultiples = keyboard.nextInt();
System.out.println("Listing the first " + numberMultiples + " multiples of " + integer + ": ");
for (int i=1; i<=numberMultiples; i++){
System.out.println(integer + " * " + i + " = " + i*integer );
}
System.out.println("Would you like to do this again? Enter yes or no: ");
repeat = keyboard.nextLine();
}
}
}
Ok so you need to understand the problem first to know how to solve it
x = First input
n = Second input
you need to calculate n multiple of x
example with x = 3 and n = 10
To calculate 10 multiple of 3 we need to do :
1st multiple = x*1
2nd multiple = x*2
3rd multiple = x*3
...
n multiple = x*n
you can notice that these operations can be replaced by one for loop (notice first and last character of every line, it can be index of your loop )
Back to java :)
for (int i=1; i<=numberMultiples; i++){
System.out.println("Listing multiple N# " + i + " = "+ i*integer );
}
Replace your code with the following and try this code :
import java.util.Scanner;
public class MultipleLooping{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
int integer,numberMultiples;
System.out.println("Enter an integer: ");
integer = keyboard.nextInt();
System.out.println("How many multiples of " + integer + " would you like to know?");
numberMultiples = keyboard.nextInt();
for (int i=1; i<=numberMultiples; i++){
System.out.println("Listing multiple N# " + i + " = "+ i*integer );
}
}
}
Enter an integer:
3
How many multiples of 3 would you like to know?
7
Listing multiple N# 1 = 3
Listing multiple N# 2 = 6
Listing multiple N# 3 = 9
Listing multiple N# 4 = 12
Listing multiple N# 5 = 15
Listing multiple N# 6 = 18
Listing multiple N# 7 = 21
Do you want like this ? Below is the code
package com.ge.cbm;
import java.util.Scanner;
public class MultipleLooping
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
//just stuff to base my code off of
int integer;
int firstEntered;
int numberMultiples;
System.out.println("Enter an integer: ");
integer = keyboard.nextInt();
firstEntered = integer;
System.out.println("How many multiples of " + integer + " would you like to know?");
numberMultiples = keyboard.nextInt();
System.out.println("Listing the first " + numberMultiples + " multiples of " + integer + ": ");
//pretty much everything from here on out.. I'm not sure what to really do.
for (int i=0;i<numberMultiples;i++){
integer=integer*firstEntered;
System.out.println(integer);
}
}
}
Output:
Enter an integer:
3
How many multiples of 3 would you like to know?
7
Listing the first 7 multiples of 3:
9
27
81
243
729
2187
6561
this should work
while(repeat.equals("yes"))
{
System.out.println("Enter an integer: ");
integer = keyboard.nextInt();
System.out.println("How many multiples of " + integer + " would you like to know?");
numberMultiples = keyboard.nextInt();
System.out.println("Listing the first " + numberMultiples + " multiples of " + integer + ": ");
for (int i=1; i<=numberMultiples; i++)
{
System.out.println(integer + " * " + i + " = " + i*integer );
}
System.out.println("Would you like to do this again? Enter yes or no: ");
repeat = keyboard.nextLine();
repeat = keyboard.nextLine();
}

Output line numbers parallel to output

thank you all for looking.
I am a beginner to Java and have been for a few years.
I am not looking for the answer but would like some to tips to finish my query.
I want to output line numbers with my out put of code in netbeans.
I am guessing a while loop would be sufficient.
I would like my output to look like the below example:
I am looking to add line numbers to my output like they are in bold below.
1: Enter a line
Some input
2: Enter another line
More input
3: Enter the last line
The end
The end,More input,Some input
HERE IS my code i would like to add to:
Scanner in = new Scanner(System.in);
String msg1, msg2, msg3;
System.out.println("Enter a line");
msg1 = in.nextLine();
System.out.println("Enter another line");
msg2 = in.nextLine();
System.out.println("Enter the last line");
msg3 = in.nextLine();
System.out.println(msg3 + "," + msg2 + "," + msg1);
This is what I am guessing i should add, however I may be totally wrong
int count = 0;
while (count ??????) {
count++;
System.out.println(count + "" + ?????????????());
}
Thank you in advance for any advice,
regards
Seems like your homework, but I would answer it anyway
Scanner in = new Scanner(System.in);
String msg1, msg2, msg3;
int count = 0;
System.out.println(++count + ": Enter a line");
msg1 = in.nextLine();
System.out.println(++count + ": Enter another line");
msg2 = in.nextLine();
System.out.println(++count + ": Enter the last line");
msg3 = in.nextLine();
System.out.println(msg3 + "," + msg2 + "," + msg1);

Suggestions with Strings from File

I am trying to read from a external file. I have successfully read from the file but now I have a little problem. The file contains around 88 verbs. The verbs are written in the file like this:
be was been
beat beat beaten
become became become
and so on...
What I need help with now is that I want a quiz like programe where only two random strings from the verb will come up and the user have to fill inn the one which is missing. Instead of the one missing, I want this("------"). My english is not so good so I hope you understand what I mean.
System.out.println("Welcome to the programe which will test you in english verbs!");
System.out.println("You can choose to be tested in up to 88.");
System.out.println("In the end of the programe you will get a percentage of total right answers.");
Scanner in = new Scanner(System.in);
System.out.println("Do you want to try??yes/no");
String a = in.nextLine();
if (a.equals("yes")) {
System.out.println("Please enter the name of the file you want to choose: ");
} else {
System.out.println("Programe is ended!");
}
String b = in.nextLine();
while(!b.equals("verb.txt")){
System.out.println("You entered wrong name, please try again!");
b = in.nextLine();
}
System.out.println("How many verbs do you want to be tested in?: ");
int totalVerb = in.nextInt();
in.nextLine();
String filename = "verb.txt";
File textFile = new File(filename);
Scanner input = new Scanner(textFile);
for (int i = 1; i <= totalVerb; i++){
String line = input.nextLine();
System.out.println(line);
System.out.println("Please fill inn the missing verb: ");
in.next();
}
System.out.println("Please enter your name: ");
in.next();
You can do something like this
import java.util.Scanner;
import java.io.*;
public class GuessVerb {
public static void main(String[] args) throws IOException{
Scanner in = new Scanner(System.in);
System.out.println("Enter a file name: ");
String fileName = in.nextLine();
File file = new File(fileName);
Scanner input = new Scanner(file);
String guess = null;
int correctCount = 0;
while(input.hasNextLine()) {
String line = input.nextLine(); // get the line
String[] tokens = line.split("\\s+"); // split it into 3 word
int randNum = (int)(Math.random() * 3); // get a random number 0, 1, 2
String newLine = null; // new line
int wordIndex = 0;
switch(randNum){ // case for random number
case 0: newLine = "------ " + tokens[1] + " " + tokens[2];
wordIndex = 0; break;
case 1: newLine = tokens[0] + " ------ " + tokens[2];
wordIndex = 1; break;
case 2: newLine = tokens[0] + " " + tokens[1] + " -------";
wordIndex = 2; break;
}
System.out.println(newLine);
System.out.println("Please fill inn the missing verb: ");
guess = in.nextLine();
if (guess.equals(tokens[wordIndex])){
correctCount++;
}
}
System.out.println("You got " + correctCount + " right");
}
}
Above is complete running program.

Categories