How To Use NextLine For Strings

May 12, 2015

I am asking the user a question and already have a correct answer. The answer is a string but is more than one word.

When the user types in the correct answer, it still comes up as wrong but I only have the input.next method.

Quote
Why are rabbits ears long?
United states of america
Incorrect!

View Replies


ADVERTISEMENT

Scanner NextLine Won't Enter Anything

Jan 26, 2014

My program is supposed to let me enter the following info for 3 employees: name, number of hours worked this week, and hourly pay rate. It lets me enter the name for the first employee, but for the second and third, it skips over the name and forces me to enter the number of hours worked and pay rate without a name. I have the code and the output posted below. First the program code:

Java Code:

import java.util.Scanner;
public class SalaryCalc {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String name;
double hours;
double rate;
double grossPay = 0;
double overtime;

[code]...

Enter hours worked this week: mh_sh_highlight_all('java'); Notice I can only enter the name for the first employee (Bigfoot). If I wanted to enter "Nessie" for my second employee's name, it skips over the name prompt and prompts me for the hours worked instead.

View Replies View Related

Going To NextLine In Java Buffered Reader

Jul 18, 2014

I have a text and I am reading each line in the text with the simple while loop:

BufferedReader br = new BufferedReader(new FileReader(new File(a.txt)));
string line = new String();
while((line = br.readLine())!=null){
if(line.equals("john"))
//skip to next line
else{
//continue something else..
}
}

My question is how do I skip to the next line ? Using apache.commons.io.FileUtils; one could easily have done something like this:

LineIterator it = FileUtils.lineIterator(file, "UTF-8");
String line = it.nextLine(); //this goes to the next line..

How can this be done using BufferedReader ?

View Replies View Related

Query About Use Of NextLine Method Of Scanner Class

Apr 10, 2014

My code is as follows:

import java.util.Scanner;
public class GetUserInput {
public static void main (String args[]) {
int i;
float f ;
String s;

[code]....

My Query is: input.nextLine() does not wait for user input. Instead it continues execution from next line. But if I move up input.nextLine(); before both input.nextInt(); and input.nextFloat() in the above code, the execution works fine, input.nextLine(); waits for user input. edited code and output are as follows.

import java.util.Scanner;
public class GetUserInput {
public static void main (String args[]) {
int i;
float f ;
String s;

[code]....

View Replies View Related

Failing To Read Input With Scanner NextLine

Jan 29, 2014

I Just made a simple code, but in any case it's not taking the input I'm trying to enter for the city.

View Replies View Related

How To Find Whether Strings Contained In Array Of Strings Constitute A Similar Character Or Not

Aug 25, 2014

I am having an array of strings and i want to find out whether these strings contained in the array contain a similar character or not.For example i am having following strings in the array of string:

aadafbd
dsfgdfbvc
sdfgyub
fhjgbjhjd

my program should provide following result: 3 because i have 3 characters which are similar in all the strings of the array(f,b,d).

View Replies View Related

Sorting Lowercase Strings Before Uppercase Strings

Mar 26, 2014

I can sort strings in a collection by uppercase and then lowercase though I was wondering if there is any way of doing it in reverse, sorting by lowercase then by uppercase.

View Replies View Related

Code Not Running Scanner NextLine After Using Scanner NextInt?

Feb 14, 2015

package jdbc;
import java.sql.*;
import javax.sql.*;
import java.util.*;
public class Jdbc {
public static void main(String[] args) {

[code]....

View Replies View Related

How To Compare Strings

Mar 13, 2014

Its supposed to notify the user if they have a palindrome, but keeps returning true even when I type, "hello".

import java.util.Scanner;
public class PalinDrome {
public static void main(String[] args) {
String line;
Scanner input = new Scanner(System.in);
System.out.print("Please enter a word ");

[code]....

View Replies View Related

Reading Only First Name From Strings?

Feb 12, 2014

I know that in c++ in order to read a string like

PHP Code:

. you need to use

getline(cin,string variable)

. In java, I have noticed that using

String name=keyboard.nextLine()

automatically reads the whole line...Suppose I wanted to read the first name only, how would I be able to accomplish that?

View Replies View Related

Extracting The Strings?

Jul 3, 2014

example input (#federer #great #game )->
output : [federer] [great][game], [federer, game],[federer,great], [game,great], [federer, game,great]

View Replies View Related

Top Three Strings In Array

May 18, 2015

I need my code to print out the top three most common IP Addresses in the ArrayList. Right now I have it printing out the top IP Address. I am a little confused as to how to get it to print out three. Every time I change something, It doesn't give me the correct results

My Code:
public class Log_File_Analysis {
private static ArrayList<String> ipAddress = new ArrayList<>();
private static String temp , mostCommon;
static int max = 0;
static int num = 0;

[Code] .....

View Replies View Related

Pass By Value And Strings

Jul 19, 2014

Below is the snippet of code

public static void main(String[] args) throws Exception {
String s = "oldString";
reverse(s);
System.out.println(s); // oldString
}
public static void modifyString(String s) {
s = "newString";
System.out.println(s); // newString
}

I thought the first print statement would print "newString" as String is an object, and when we pass objects between methods, changing state of the object in any method reflects across the methods.

View Replies View Related

While Loops And Strings

Jan 3, 2014

So the while loop I am trying to use is:
while( type != "EXIT" ) {
type = input.next();
}

The problem is that typing in EXIT doesn't end the loop like I want it to. I also tried input.nextLine(); but neither of them work. The loop is being used to fill an ArrayList so the number of elements can change based on how many they want. What am I doing wrong, or what alternatives do I have?

Seems I need to use !type.equals("EXIT")

View Replies View Related

Java Won't Allow To Use Strings

Sep 21, 2014

covers switch statements and if/else statements. Java doesn't like the Strings for some reason. My instructor does her strings just like this and it works for her. I can figure out the rest of the program if I can only get around the: "java error35: sSymbol variable might not have been initialized.

import java.util.*;
public class RockPaperScissors
{
public static void main(String[] args) {
//generate outcome
int symbol = (int)(Math.random() * 4);
String sSymbol;

[code]....

View Replies View Related

Operation With Strings

May 6, 2014

I have to write a java programm,where i have given string. Output should be like that:

1. print only once what characters are apearinng in string, and the last index of it

2. print how many characters are in the string

I have to do it only with loops,no special classes

So far:

public static void main( String[] args ) {
String besedilo = "Testiranje";
besedilo = besedilo.toLowerCase();
for (int i=0; i<besedilo.length();i++)

[code]...

View Replies View Related

If Statement With Strings?

May 1, 2015

I am making a simple calculator. I want the user to input either string "add" or "subtract" and then it will take two numbers and either add or subtract them. I cannot get the if statement to work with a string!

import java.util.Scanner;
public class newcalc {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter add or subtract");

String line = input.nextLine();

if input = "add";

View Replies View Related

Strings Are Not Printing As They Should?

Jun 12, 2014

Im trying to make a question game, much like a spin off from Trivial Pursuit. In this code, I call classes to get a random number. This number determines what category the question will be from. Coinciding with this number, the "if" statements go and pull the questions and answers from an alternate class. My problem is that when I try and output what should be the question and the 3 answers, its outputs "null" for each String?

This is my first class, which is just the class for the player.

Java Code: /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

[Code].....

View Replies View Related

Strings And Equality

Sep 24, 2014

Output of the program given below:

class A123
{
public static void main(String args[])
{
String s1="hello123";
String s2="hello"+String.valueOf(123);
String s3="hello"+"123";
String s5=new String("hello123");

[Code] ....

why s1 is equal to s3 and not s2

View Replies View Related

JSF :: How To Display List Of Strings

Apr 1, 2014

I have figured out, how to diplay basic datatypes of an entity / object in a dataTable, but I can't find a way to display a List<String>.

I also tried it with ui:repeat, but the space, where it should appear, stays empty. This is my code right now:

Product.java (the entity)

...
@ElementCollection
@CollectionTable(name="NeededRessources", joinColumns=@JoinColumn(name="product_id"))
@Column(name="resource")
private List<String> neededResources;
...

And the page: index.xhtml

<ui:repeat value="#{product.neededResources}" var="t">
#{t}
</ui:repeat>

View Replies View Related

How To Get Average Of Strings Using Array

Apr 21, 2014

I'm trying to calculate the average of grades that a user puts in. Here is my code so far:

public static void main(String[] args) {
Scanner input = new Scanner (System.in); 
System.out.println("Please enter an array of grades seperated bt a comma.");
input.nextLine();
String arrayOfGrades = "100,50,100";
String[] grades = arrayOfGrades.split(",");
 
[Code] .....

I think I'm on the right track, the only big error I'm really getting is the line: sum += grades[i]. It's saying string can not be converted into a double.

View Replies View Related

Comparing Lengths On Strings

Apr 25, 2015

I am making a program i want String length and compare it that it will be null or not but it gives me error.

View Replies View Related

Checking Two Strings In Java?

Feb 10, 2014

I want get a specific document from data set if a given string matches in that document.

If given string is 'what is java' and,

doc1='... what is full form of java ...'

doc2='... is java what ...'

doc3='... what is java ...'

then the output should contains doc1, and doc3.

I'm trying like this.

for(String key_word: key.split("W+")) {
for (String _word : line1.split("W+")) {
if(word.toLowerCase().equals(key_word.toLowerCase())) {
key_final=key_final+" "+key_word;
}
}
}

I'm getting wrong output containing doc1, doc2 and doc3.But I want only the exact match. Here each document is containing some paragraphs.

View Replies View Related

Assigning Random Int To Strings

Jun 16, 2014

while (gamesPlayed<gameQty) {
userChoice = JOptionPane.showInputDialog("Rock, Paper, or Scissors?");
//Determine winner for each game
if (compChoice.equalsIgnoreCase(userChoice));

[Code] ....

It seems to circle through the loop gameQty times, without comparing.

View Replies View Related

Compare First Characters Of Two Strings?

May 20, 2015

I am trying to compare the first characters of two strings.

import java.util.Scanner;
public class testIf {
public static void main (String [] args) {
String userInput = "";
char firstLetter = '-';

[Code] .....

in the if statement i get this error Cannot invoke charAt(int) on the primitive type char...

View Replies View Related

Sending Two Strings Into A Constructor

Jun 13, 2014

I did this using ArrayList<String> and my tests worked. Meaning I was able to read the strings in a different class through my constructor. However I want to use a string array because it will be easier to handle when I finish the program. I will send each players poker hand in and figure out who is the winner instead of putting it all onto a ArrayList and having to iterate through it. However whenever I did my check I am just printing null.

PokerFile class

void separateHands(String cards) {
//ArrayList<String>playerOne = new ArrayList<String>();
//ArrayList<String>playerTwo = new ArrayList<String>();
String[] playerOne = new String[10];
String[] playerTwo = new String[10];
 
[Code] .....

ignore the boolean methods I was just building the structure of the program. The print file is what is outputting this:

null
null
null
null
null
null

public class WinningHand extends PokerFile {
//ArrayList<String> p1 = new ArrayList<String>();
//ArrayList<String> p2 = new ArrayList<String>();
String[] p1 = new String[6];
String[] p2 = new String[6];
WinningHand(String[] p1,String[] p2)

[Code] ....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved