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
ADVERTISEMENT
Jan 24, 2015
So I need to be able to find the longest increasing sequence in a 2D grid of numbers. A sequence is simply a series of adjacent squares. Squares may not be used twice. Example here.
97 47 56 36 60 31 57 54 12 55
35 57 41 13 82 80 71 93 31 62
89 36 98 75 91 46 95 53 37 99
25 45 26 17 15 82 80 73 96 17
75 22 63 96 96 36 64 31 99 86
12 80 42 74 54 14 93 17 14 55
14 15 20 71 34 50 22 60 32 41
90 69 44 52 54 73 20 12 55 52
39 33 25 31 76 45 44 84 90 52
94 35 55 24 41 63 87 93 79 24
The longest sequence has length 10, consisting of entries (row, column) as follows;
(5,0) with cost 12
(6,0) with cost 14
(6,1) with cost 15
(6,2) with cost 20
(7,2) with cost 44
(7,3) with cost 52
(7,4) with cost 54
(6,3) with cost 71
(5,3) with cost 74
(4,3) with cost 96
I wouldn't be having a problem if I could use a 2D Array but the assignment requires the use of a List and StringTokenizer.
The way it works, a file is read and each row of the grid is an element of the List of Strings. So in the example above if I were to call myList.get(1) it would return 35 57 41 13 82 80 71 93 31 62 since it's the second row and second index value of the List.
If it was a 2D array I would check values at [x-1][x-1],[x-1][x]... to check all sides and diagonals ect ect. But with the List and Tokenizer there isn't a way that I know of that can search through the String using index values.
I wouldn't even know where to start with that. As of now I read the file fine and make the List of Strings and populate it properly, I'm just a little stuck. I should also mention that which ever method I use to search needs to be recursive.
This is what little I have. It reads in the file, creates the list and assigns the rows to the list.
import java.util.*;
import java.io.*;
public class MaxIncreasingSub {
public static void main(String []args) throws Exception{
File file = new File("Ass1Q2_test1");
List<String> myList = new ArrayList<String>();
[Code] .....
View Replies
View Related
Apr 23, 2015
I am advised to use a while loop the Scanner method hasNextLine() and in the while loop body, call the Scanner method nextLine(), and add the returned String to the ArrayList of Strings. what I have gotten so far:
Scanner input = new Scanner(new File(""));
while(input.hasNextLine()) {
String line = input.nextLine();
System.out.println(line);
View Replies
View Related
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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