Question

My current code is: for (int i = 0; i < items; i++) { System.out.println(""); System.out.print("Enter...

My current code is:

for (int i = 0; i < items; i++) {

System.out.println("");
System.out.print("Enter name of assessment item #" + (i+1) + ": ");
if (input.hasNext()) {
name[i] = input.next();
input.nextLine();
}

System.out.print("Enter the weighting of assessment item #" + (i+1) + ": ");
weight[i] = input.nextInt();
input.nextLine();
}

System.out.println("");
System.out.print("Enter your mark for \"" + name[0] + "\" as a percentage: ");
int mark1 = input.nextInt();

However, if I Enter "Final Examination" - the output from calling "name[0]", only displays "Final". How do I get it to print the entire phrase?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In Java, the next() and nextLine() methods are available java.util.Scanner class. These methods are used to read input from the user. The next() method can read the text until encounters space. The nextLine() method can read the text until it encounters new line.

While you're reading the name of the assessment item, You're using the next() method . That's why it is not reading the entire text. It is only reading until it encounters a space. So you're getting output only "Final" instead of "Final Examination".

Change below lines:

name[i] = input.next(); // This line reads until a space encounters and store that text in name[i].
input.nextLine(); // This line reads until a new line encounters and it is not storing text in any variable

To:

name[i] = input.nextLine(); // This line reads until a new space encounters and store that text in name[i]

Now it works fine.

Add a comment
Know the answer?
Add Answer to:
My current code is: for (int i = 0; i < items; i++) { System.out.println(""); System.out.print("Enter...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895...

    Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895    {        public static void header()        {            System.out.println("\tWelcome to St. Joseph's College");        }        public static void main(String[] args) {            Scanner input = new Scanner(System.in);            int d;            header();            System.out.println("Enter number of items to process");            d = input.nextInt();      ...

  • Java Programming Question

    After pillaging for a few weeks with our new cargo bay upgrade, we decide to branch out into a new sector of space to explore and hopefully find new targets. We travel to the next star system over, another low-security sector. After exploring the new star system for a few hours, we are hailed by a strange vessel. He sends us a message stating that he is a traveling merchant looking to purchase goods, and asks us if we would...

  • public static void main(String[] args) { int option=0;    while(option<8){ Scanner input=new Scanner(System.in); System.out.println("Welcome! Please enter...

    public static void main(String[] args) { int option=0;    while(option<8){ Scanner input=new Scanner(System.in); System.out.println("Welcome! Please enter your name"); String name=input.next(); Person user= new Person(name); System.out.println("Your id: "+user.getID()); System.out.println("Login successful"); System.out.println("------------------------------"); while(option!=7){ System.out.println("1.Create and host a new meeting"); System.out.println("2.Cancel a meeting"); System.out.println("3.Attend an existing meeting"); System.out.println("4.Leave a meeting"); System.out.println("5.Display my meetings"); System.out.println("6.Display meetings organized by me"); System.out.println("7.Logout"); System.out.println("8.Exit the app");    option=input.nextInt(); switch(option){ case 1: break; case 2: break; case 3: break; case 4: break; case 5: break; case 6: break;...

  • ​I have to create two classes one class that accepts an object, stores the object in...

    ​I have to create two classes one class that accepts an object, stores the object in an array. I have another that has a constructor that creates an object. Here is my first class named "ShoppingList": import java.util.*; public class ShoppingList {    private ShoppingItem [] list;    private int amtItems = 0;       public ShoppingList()    {       list=new ShoppingItem[8];    }       public void add(ShoppingItem item)    {       if(amtItems<8)       {          list[amtItems] = ShoppingItem(item);...

  • I need help with this method (public static int computer_move(int board[][])) . When I run it...

    I need help with this method (public static int computer_move(int board[][])) . When I run it and play the compute.The computer enters the 'O' in a location that the user has all ready enter it sometimes. I need to fix it where the computer enters the 'O' in a location the user has not enter the "X' already package tictactoe; import java.util.Scanner; public class TicTacToeGame { static final int EMPTY = 0; static final int NONE = 0; static final...

  • Please fix my code so I can get this output: Enter the first 12-digit of an...

    Please fix my code so I can get this output: Enter the first 12-digit of an ISBN number as a string: 978013213080 The ISBN number is 9780132130806 This was my output: import java.util.Scanner; public class Isbn { private static int getChecksum(String s) { // Calculate checksum int sum = 0; for (int i = 0; i < s.length(); i++) if (i % 2 == 0) sum += (s.charAt(i) - '0') * 3; else sum += s.charAt(i) - '0'; return 10...

  • Hello Can you help to fix the program. When running, it still show Exception in thread...

    Hello Can you help to fix the program. When running, it still show Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol symbol: class Contact location: class ContactMap    at ContactMap.main(ContactMap.java:40) C:\Users\user\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 1 second) ************************ import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.TreeMap; public class ContactMap { public static void main(String args[]) throws IOException { Scanner input=new Scanner(System.in); //Create a TreeMap ,...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • Could you help me pleas , this is my code I want change it to insert...

    Could you help me pleas , this is my code I want change it to insert student by user , and i have problem when i want append name it just one time i can't append or present more one. >>>>>>>>>>>>>>>>>>>. import java.util.Iterator; import java.util.Scanner; public class studentDLLTest { static int getChoice() { Scanner in = new Scanner(System.in); int choice; do { System.out.print("\nYour choice? : "); choice = in.nextInt(); } while (choice < 1 || choice > 9); return choice;...

  • Here is everything I have on my codes, the compiler does not allow me to input...

    Here is everything I have on my codes, the compiler does not allow me to input my name, it will print out "Please enter your name: " but totally ignores it and does not allow me to input my name and just proceeds to my result of ID and Sale. Please help. I've bolded the part where it I should be able to input my name package salesperson; public class Salesperson { String Names; int ID; double Sales; // craeting...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT