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


ADVERTISEMENT

Printing Two Strings As Output

Apr 22, 2015

WAP which takes two string as input from the user (a & b).This prog should print two strings as output (a1 & b1).

(a1) should contain all the characters which are present in (a).but not present in (b).
(b1)should contains all the characters which are present in (b).but not present in(a).

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

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

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 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 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

Exception Handling For Strings?

Nov 25, 2014

For one of my last labs for the semester, my professor is having the class go back to our very first program and apply some of the exception handling that we just recently learned about. Here's my improved code so far:

Java Code: import java.util.*;
import java.lang.*;
public class Lab2Part1 {
public static void main (String [] args) {
Scanner input = new Scanner (System.in);

[code]....

My code compiles fine, but even if I enter an integer or a double, it saves the number as a string, and prints that out as the name. Is there any way to get around this? Or do I need to use something besides a try-catch?

View Replies View Related

Unable To Add Two Strings To A Hash Map

Feb 11, 2014

I am trying to add two strings to a hash map. the first being the key, a 3 digit code that can have duplicates, and the value to store in an ArrayList. From what I've read, when add a key to the hashmap that is a duplicate, the previous gets overwritten and that is why I am trying to put the values in an array list. I was hoping that when the key is looked up, it would print all the values associated with that key:

class library{
HashMap<String, ArrayList<String>> checkoutBooks =
new HashMap<String, ArrayList<String>>() ;
ArrayList<String> patName = new ArrayList<String>() ;
public void checkoutBook(String isbn, String patron) {
patName.add(patron) ;

[Code]...

So when I try to add values such as:

library.checkoutBook("000", "test 1");
library.checkoutBook("000", "test 2");
library.checkoutBook("001", "test 3");
library.checkoutBook("001", "test 4");
library.checkoutBook("002", "test 5");
library.checkoutBook("002", "test 6");

[Code]...

View Replies View Related

Passing Strings In Methods

Mar 16, 2014

I am trying to call stringToFile in the main method but it throws error saying "incompatible types scanner cannot be converted to string."
 
public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in);
mask(keyboard);
printingString();
fileName(keyboard);
  String completeFileName = stringToFile(keyboard);

[Code] ....

View Replies View Related

Using BufferedReader To Put Strings In ArrayList

Sep 27, 2014

We haven't covered BufferedReader in this course yet. The assignmen is to get user input to fill arraylist with strings then when user hits Enter without any input, the console displays the contents of the arraylist.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Ex01 {
public static void main(String[] args) throws IOException {

[code]....

View Replies View Related







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